test_manifest.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. # This Source Code Form is subject to the terms of the Mozilla Public
  2. # License, v. 2.0. If a copy of the MPL was not distributed with this
  3. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4. import unittest
  5. from StringIO import StringIO
  6. from cuddlefish.manifest import scan_module
  7. class Extra:
  8. def failUnlessKeysAre(self, d, keys):
  9. self.failUnlessEqual(sorted(d.keys()), sorted(keys))
  10. class Require(unittest.TestCase, Extra):
  11. def scan(self, text):
  12. lines = StringIO(text).readlines()
  13. requires, problems, locations = scan_module("fake.js", lines)
  14. self.failUnlessEqual(problems, False)
  15. return requires
  16. def scan_locations(self, text):
  17. lines = StringIO(text).readlines()
  18. requires, problems, locations = scan_module("fake.js", lines)
  19. self.failUnlessEqual(problems, False)
  20. return requires, locations
  21. def test_modules(self):
  22. mod = """var foo = require('one');"""
  23. requires = self.scan(mod)
  24. self.failUnlessKeysAre(requires, ["one"])
  25. mod = """var foo = require(\"one\");"""
  26. requires = self.scan(mod)
  27. self.failUnlessKeysAre(requires, ["one"])
  28. mod = """var foo=require( 'one' ) ; """
  29. requires = self.scan(mod)
  30. self.failUnlessKeysAre(requires, ["one"])
  31. mod = """var foo = require('o'+'ne'); // tricky, denied"""
  32. requires = self.scan(mod)
  33. self.failUnlessKeysAre(requires, [])
  34. mod = """require('one').immediately.do().stuff();"""
  35. requires, locations = self.scan_locations(mod)
  36. self.failUnlessKeysAre(requires, ["one"])
  37. self.failUnlessEqual(locations, {"one": 1})
  38. # these forms are commented out, and thus ignored
  39. mod = """// var foo = require('one');"""
  40. requires = self.scan(mod)
  41. self.failUnlessKeysAre(requires, [])
  42. mod = """/* var foo = require('one');"""
  43. requires = self.scan(mod)
  44. self.failUnlessKeysAre(requires, [])
  45. mod = """ * var foo = require('one');"""
  46. requires = self.scan(mod)
  47. self.failUnlessKeysAre(requires, [])
  48. mod = """ ' var foo = require('one');"""
  49. requires = self.scan(mod)
  50. self.failUnlessKeysAre(requires, ["one"])
  51. mod = """ \" var foo = require('one');"""
  52. requires = self.scan(mod)
  53. self.failUnlessKeysAre(requires, ["one"])
  54. # multiple requires
  55. mod = """const foo = require('one');
  56. const foo = require('two');"""
  57. requires, locations = self.scan_locations(mod)
  58. self.failUnlessKeysAre(requires, ["one", "two"])
  59. self.failUnlessEqual(locations["one"], 1)
  60. self.failUnlessEqual(locations["two"], 2)
  61. mod = """const foo = require('repeated');
  62. const bar = require('repeated');
  63. const baz = require('repeated');"""
  64. requires, locations = self.scan_locations(mod)
  65. self.failUnlessKeysAre(requires, ["repeated"])
  66. self.failUnlessEqual(locations["repeated"], 1) # first occurrence
  67. mod = """const foo = require('one'); const foo = require('two');"""
  68. requires = self.scan(mod)
  69. self.failUnlessKeysAre(requires, ["one", "two"])
  70. # define calls
  71. mod = """define('one', ['two', 'numbers/three'], function(t, th) {});"""
  72. requires = self.scan(mod)
  73. self.failUnlessKeysAre(requires, ["two", "numbers/three"])
  74. mod = """define(
  75. ['odd',
  76. "numbers/four"], function() {});"""
  77. requires = self.scan(mod)
  78. self.failUnlessKeysAre(requires, ["odd", "numbers/four"])
  79. mod = """define(function(require, exports, module) {
  80. var a = require("some/module/a"),
  81. b = require('b/v1');
  82. exports.a = a;
  83. //This is a fakeout: require('bad');
  84. /* And another var bad = require('bad2'); */
  85. require('foo').goFoo();
  86. });"""
  87. requires = self.scan(mod)
  88. self.failUnlessKeysAre(requires, ["some/module/a", "b/v1", "foo"])
  89. mod = """define (
  90. "foo",
  91. ["bar"], function (bar) {
  92. var me = require("me");
  93. }
  94. )"""
  95. requires = self.scan(mod)
  96. self.failUnlessKeysAre(requires, ["bar", "me"])
  97. mod = """define(['se' + 'ven', 'eight', nine], function () {});"""
  98. requires = self.scan(mod)
  99. self.failUnlessKeysAre(requires, ["eight"])
  100. # async require calls
  101. mod = """require(['one'], function(one) {var o = require("one");});"""
  102. requires = self.scan(mod)
  103. self.failUnlessKeysAre(requires, ["one"])
  104. mod = """require([ 'one' ], function(one) {var t = require("two");});"""
  105. requires = self.scan(mod)
  106. self.failUnlessKeysAre(requires, ["one", "two"])
  107. mod = """require ( ['two', 'numbers/three'], function(t, th) {});"""
  108. requires = self.scan(mod)
  109. self.failUnlessKeysAre(requires, ["two", "numbers/three"])
  110. mod = """require (
  111. ["bar", "fa" + 'ke' ], function (bar) {
  112. var me = require("me");
  113. // require("bad").doBad();
  114. }
  115. )"""
  116. requires = self.scan(mod)
  117. self.failUnlessKeysAre(requires, ["bar", "me"])
  118. def scan2(text, fn="fake.js"):
  119. stderr = StringIO()
  120. lines = StringIO(text).readlines()
  121. requires, problems, locations = scan_module(fn, lines, stderr)
  122. stderr.seek(0)
  123. return requires, problems, stderr.readlines()
  124. class Chrome(unittest.TestCase, Extra):
  125. def test_ignore_loader(self):
  126. # we specifically ignore the loader itself
  127. mod = """let {Cc,Ci} = require('chrome');"""
  128. requires, problems, err = scan2(mod, "blah/cuddlefish.js")
  129. self.failUnlessKeysAre(requires, ["chrome"])
  130. self.failUnlessEqual(problems, False)
  131. self.failUnlessEqual(err, [])
  132. def test_chrome(self):
  133. mod = """let {Cc,Ci} = require('chrome');"""
  134. requires, problems, err = scan2(mod)
  135. self.failUnlessKeysAre(requires, ["chrome"])
  136. self.failUnlessEqual(problems, False)
  137. self.failUnlessEqual(err, [])
  138. mod = """var foo = require('foo');
  139. let {Cc,Ci} = require('chrome');"""
  140. requires, problems, err = scan2(mod)
  141. self.failUnlessKeysAre(requires, ["foo", "chrome"])
  142. self.failUnlessEqual(problems, False)
  143. self.failUnlessEqual(err, [])
  144. mod = """let c = require('chrome');"""
  145. requires, problems, err = scan2(mod)
  146. self.failUnlessKeysAre(requires, ["chrome"])
  147. self.failUnlessEqual(problems, False)
  148. self.failUnlessEqual(err, [])
  149. mod = """var foo = require('foo');
  150. let c = require('chrome');"""
  151. requires, problems, err = scan2(mod)
  152. self.failUnlessKeysAre(requires, ["foo", "chrome"])
  153. self.failUnlessEqual(problems, False)
  154. self.failUnlessEqual(err, [])
  155. def test_not_chrome(self):
  156. # from bug 596595
  157. mod = r'soughtLines: new RegExp("^\\s*(\\[[0-9 .]*\\])?\\s*\\(\\((EE|WW)\\)|.* [Cc]hipsets?: \\)|\\s*Backtrace")'
  158. requires, problems, err = scan2(mod)
  159. self.failUnlessKeysAre(requires, [])
  160. self.failUnlessEqual((problems,err), (False, []))
  161. def test_not_chrome2(self):
  162. # from bug 655788
  163. mod = r"var foo = 'some stuff Cr';"
  164. requires, problems, err = scan2(mod)
  165. self.failUnlessKeysAre(requires, [])
  166. self.failUnlessEqual((problems,err), (False, []))
  167. class BadChrome(unittest.TestCase, Extra):
  168. def test_bad_alias(self):
  169. # using Components.* gets you an error, with a message that teaches
  170. # you the correct approach.
  171. mod = """let Cc = Components.classes;
  172. let Cu = Components.utils;
  173. """
  174. requires, problems, err = scan2(mod)
  175. self.failUnlessKeysAre(requires, [])
  176. self.failUnlessEqual(problems, True)
  177. self.failUnlessEqual(err[1], "The following lines from file fake.js:\n")
  178. self.failUnlessEqual(err[2], " 1: let Cc = Components.classes;\n")
  179. self.failUnlessEqual(err[3], " 2: let Cu = Components.utils;\n")
  180. self.failUnlessEqual(err[4], "use 'Components' to access chrome authority. To do so, you need to add a\n")
  181. self.failUnlessEqual(err[5], "line somewhat like the following:\n")
  182. self.failUnlessEqual(err[7], ' const {Cc,Cu} = require("chrome");\n')
  183. self.failUnlessEqual(err[9], "Then you can use any shortcuts to its properties that you import from the\n")
  184. def test_bad_misc(self):
  185. # If it looks like you're using something that doesn't have an alias,
  186. # the warning also suggests a better way.
  187. mod = """if (Components.isSuccessCode(foo))
  188. """
  189. requires, problems, err = scan2(mod)
  190. self.failUnlessKeysAre(requires, [])
  191. self.failUnlessEqual(problems, True)
  192. self.failUnlessEqual(err[1], "The following lines from file fake.js:\n")
  193. self.failUnlessEqual(err[2], " 1: if (Components.isSuccessCode(foo))\n")
  194. self.failUnlessEqual(err[3], "use 'Components' to access chrome authority. To do so, you need to add a\n")
  195. self.failUnlessEqual(err[4], "line somewhat like the following:\n")
  196. self.failUnlessEqual(err[6], ' const {components} = require("chrome");\n')
  197. self.failUnlessEqual(err[8], "Then you can use any shortcuts to its properties that you import from the\n")
  198. def test_chrome_components(self):
  199. # Bug 636145/774636: We no longer tolerate usages of "Components",
  200. # even when adding `require("chrome")` to your module.
  201. mod = """require("chrome");
  202. var ios = Components.classes['@mozilla.org/network/io-service;1'];"""
  203. requires, problems, err = scan2(mod)
  204. self.failUnlessKeysAre(requires, ["chrome"])
  205. self.failUnlessEqual(problems, True)
  206. self.failUnlessEqual(err[1], "The following lines from file fake.js:\n")
  207. self.failUnlessEqual(err[2], " 2: var ios = Components.classes['@mozilla.org/network/io-service;1'];\n")
  208. self.failUnlessEqual(err[3], "use 'Components' to access chrome authority. To do so, you need to add a\n")
  209. self.failUnlessEqual(err[4], "line somewhat like the following:\n")
  210. self.failUnlessEqual(err[6], ' const {Cc} = require("chrome");\n')
  211. self.failUnlessEqual(err[8], "Then you can use any shortcuts to its properties that you import from the\n")
  212. if __name__ == '__main__':
  213. unittest.main()