OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2014 The Chromium Authors. All rights reserved. | 2 # Copyright 2014 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
| 6 from ast import literal_eval |
6 import os | 7 import os |
7 import tempfile | 8 import tempfile |
8 import unittest | 9 import unittest |
9 | 10 |
10 from compile import Checker | 11 from compile import Checker |
11 from processor import FileCache, Processor | 12 from processor import FileCache, Processor |
12 | 13 |
13 | 14 |
14 _SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) | 15 _SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) |
15 _SRC_DIR = os.path.join(_SCRIPT_DIR, os.pardir, os.pardir) | 16 _SRC_DIR = os.path.join(_SCRIPT_DIR, os.pardir, os.pardir) |
16 _RESOURCES_DIR = os.path.join(_SRC_DIR, "ui", "webui", "resources", "js") | 17 _RESOURCES_DIR = os.path.join(_SRC_DIR, "ui", "webui", "resources", "js") |
17 _ASSERT_JS = os.path.join(_RESOURCES_DIR, "assert.js") | 18 _ASSERT_JS = os.path.join(_RESOURCES_DIR, "assert.js") |
18 _CR_JS = os.path.join(_RESOURCES_DIR, "cr.js") | 19 _CR_JS = os.path.join(_RESOURCES_DIR, "cr.js") |
19 _CR_UI_JS = os.path.join(_RESOURCES_DIR, "cr", "ui.js") | 20 _CR_UI_JS = os.path.join(_RESOURCES_DIR, "cr", "ui.js") |
20 _POLYMER_EXTERNS = os.path.join(_SRC_DIR, "third_party", "polymer", "v1_0", | 21 _POLYMER_EXTERNS = os.path.join(_SRC_DIR, "third_party", "polymer", "v1_0", |
21 "components-chromium", "polymer-externs", | 22 "components-chromium", "polymer-externs", |
22 "polymer.externs.js") | 23 "polymer.externs.js") |
| 24 _GYPI_DICT = literal_eval(open(os.path.join(_SCRIPT_DIR, 'compile_js.gypi')).rea
d()) |
| 25 _COMMON_CLOSURE_ARGS =_GYPI_DICT['variables']['closure_args+'] |
| 26 _COMMON_CLOSURE_ARGS += _GYPI_DICT['actions'][0]['variables']['disabled_closure_
args%'] |
23 | 27 |
24 | 28 |
25 class CompilerTest(unittest.TestCase): | 29 class CompilerTest(unittest.TestCase): |
26 _ASSERT_DEFINITION = Processor(_ASSERT_JS).contents | 30 _ASSERT_DEFINITION = Processor(_ASSERT_JS).contents |
27 _CR_DEFINE_DEFINITION = Processor(_CR_JS).contents | 31 _CR_DEFINE_DEFINITION = Processor(_CR_JS).contents |
28 _CR_UI_DECORATE_DEFINITION = Processor(_CR_UI_JS).contents | 32 _CR_UI_DECORATE_DEFINITION = Processor(_CR_UI_JS).contents |
29 | 33 |
30 def setUp(self): | 34 def setUp(self): |
31 self._checker = Checker() | 35 self._checker = Checker() |
32 self._tmp_files = [] | 36 self._tmp_files = [] |
33 | 37 |
34 def tearDown(self): | 38 def tearDown(self): |
35 for file in self._tmp_files: | 39 for file in self._tmp_files: |
36 if os.path.exists(file): | 40 if os.path.exists(file): |
37 os.remove(file) | 41 os.remove(file) |
38 | 42 |
39 def _runChecker(self, source_code, output_wrapper=None): | 43 def _runChecker(self, source_code, closure_args=None): |
40 file_path = "/script.js" | 44 file_path = "/script.js" |
41 FileCache._cache[file_path] = source_code | 45 FileCache._cache[file_path] = source_code |
42 out_file, out_map = self._createOutFiles() | 46 out_file, out_map = self._createOutFiles() |
| 47 args = _COMMON_CLOSURE_ARGS + (closure_args or []) |
43 | 48 |
44 found_errors, stderr = self._checker.check(file_path, | 49 found_errors, stderr = self._checker.check(file_path, |
45 externs=[_POLYMER_EXTERNS], | 50 externs=[_POLYMER_EXTERNS], |
46 out_file=out_file, | 51 out_file=out_file, |
47 output_wrapper=output_wrapper) | 52 closure_args=args) |
48 return found_errors, stderr, out_file, out_map | 53 return found_errors, stderr, out_file, out_map |
49 | 54 |
50 def _runCheckerTestExpectError(self, source_code, expected_error): | 55 def _runCheckerTestExpectError(self, source_code, expected_error): |
51 _, stderr, out_file, out_map = self._runChecker(source_code) | 56 _, stderr, out_file, out_map = self._runChecker(source_code) |
52 | 57 |
53 self.assertTrue(expected_error in stderr, | 58 self.assertTrue(expected_error in stderr, |
54 msg="Expected chunk: \n%s\n\nOutput:\n%s\n" % ( | 59 msg="Expected chunk: \n%s\n\nOutput:\n%s\n" % ( |
55 expected_error, stderr)) | 60 expected_error, stderr)) |
56 self.assertFalse(os.path.exists(out_file)) | 61 self.assertFalse(os.path.exists(out_file)) |
57 self.assertFalse(os.path.exists(out_map)) | 62 self.assertFalse(os.path.exists(out_map)) |
58 | 63 |
59 def _runCheckerTestExpectSuccess(self, source_code, expected_output=None, | 64 def _runCheckerTestExpectSuccess(self, source_code, expected_output=None, |
60 output_wrapper=None): | 65 closure_args=None): |
61 found_errors, stderr, out_file, out_map = self._runChecker(source_code, | 66 found_errors, stderr, out_file, out_map = self._runChecker(source_code, |
62 output_wrapper) | 67 closure_args) |
63 | 68 |
64 self.assertFalse(found_errors, | 69 self.assertFalse(found_errors, |
65 msg="Expected success, but got failure\n\nOutput:\n%s\n" % stderr) | 70 msg="Expected success, but got failure\n\nOutput:\n%s\n" % stderr) |
66 | 71 |
67 self.assertTrue(os.path.exists(out_map)) | 72 self.assertTrue(os.path.exists(out_map)) |
68 self.assertTrue(os.path.exists(out_file)) | 73 self.assertTrue(os.path.exists(out_file)) |
69 if expected_output: | 74 if expected_output: |
70 with open(out_file, "r") as file: | 75 with open(out_file, "r") as file: |
71 self.assertEquals(file.read(), expected_output) | 76 self.assertEquals(file.read(), expected_output) |
72 | 77 |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
275 """'use strict';var testScript=function(){console.log("hello world")};\n""") | 280 """'use strict';var testScript=function(){console.log("hello world")};\n""") |
276 | 281 |
277 def testOutputWrapper(self): | 282 def testOutputWrapper(self): |
278 source_code = """ | 283 source_code = """ |
279 var testScript = function() { | 284 var testScript = function() { |
280 console.log("hello world"); | 285 console.log("hello world"); |
281 }; | 286 }; |
282 """ | 287 """ |
283 expected_output = ("""(function(){'use strict';var testScript=function()""" | 288 expected_output = ("""(function(){'use strict';var testScript=function()""" |
284 """{console.log("hello world")};})();\n""") | 289 """{console.log("hello world")};})();\n""") |
285 output_wrapper="(function(){%output%})();" | 290 closure_args=["output_wrapper='(function(){%output%})();'"] |
286 self._runCheckerTestExpectSuccess(source_code, expected_output, | 291 self._runCheckerTestExpectSuccess(source_code, expected_output, |
287 output_wrapper=output_wrapper) | 292 closure_args) |
288 | 293 |
289 def testCheckMultiple(self): | 294 def testCheckMultiple(self): |
290 source_file1 = tempfile.NamedTemporaryFile(delete=False) | 295 source_file1 = tempfile.NamedTemporaryFile(delete=False) |
291 with open(source_file1.name, "w") as f: | 296 with open(source_file1.name, "w") as f: |
292 f.write(""" | 297 f.write(""" |
293 goog.provide('testScript'); | 298 goog.provide('testScript'); |
294 | 299 |
295 var testScript = function() {}; | 300 var testScript = function() {}; |
296 """) | 301 """) |
297 self._tmp_files.append(source_file1.name) | 302 self._tmp_files.append(source_file1.name) |
298 | 303 |
299 source_file2 = tempfile.NamedTemporaryFile(delete=False) | 304 source_file2 = tempfile.NamedTemporaryFile(delete=False) |
300 with open(source_file2.name, "w") as f: | 305 with open(source_file2.name, "w") as f: |
301 f.write(""" | 306 f.write(""" |
302 goog.require('testScript'); | 307 goog.require('testScript'); |
303 | 308 |
304 testScript(); | 309 testScript(); |
305 """) | 310 """) |
306 self._tmp_files.append(source_file2.name) | 311 self._tmp_files.append(source_file2.name) |
307 | 312 |
308 out_file, out_map = self._createOutFiles() | 313 out_file, out_map = self._createOutFiles() |
309 sources = [source_file1.name, source_file2.name] | 314 sources = [source_file1.name, source_file2.name] |
310 externs = [_POLYMER_EXTERNS] | 315 externs = [_POLYMER_EXTERNS] |
311 found_errors, stderr = self._checker.check_multiple(sources, | 316 found_errors, stderr = self._checker.check_multiple( |
312 externs=externs, | 317 sources, externs=externs, out_file=out_file, |
313 out_file=out_file) | 318 closure_args=_COMMON_CLOSURE_ARGS) |
314 self.assertFalse(found_errors, | 319 self.assertFalse(found_errors, |
315 msg="Expected success, but got failure\n\nOutput:\n%s\n" % stderr) | 320 msg="Expected success, but got failure\n\nOutput:\n%s\n" % stderr) |
316 | 321 |
317 expected_output = "'use strict';var testScript=function(){};testScript();\n" | 322 expected_output = "'use strict';var testScript=function(){};testScript();\n" |
318 self.assertTrue(os.path.exists(out_map)) | 323 self.assertTrue(os.path.exists(out_map)) |
319 self.assertTrue(os.path.exists(out_file)) | 324 self.assertTrue(os.path.exists(out_file)) |
320 with open(out_file, "r") as file: | 325 with open(out_file, "r") as file: |
321 self.assertEquals(file.read(), expected_output) | 326 self.assertEquals(file.read(), expected_output) |
322 | 327 |
323 | 328 |
324 if __name__ == "__main__": | 329 if __name__ == "__main__": |
325 unittest.main() | 330 unittest.main() |
OLD | NEW |