| 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 from ast import literal_eval |
| 7 import os | 7 import os |
| 8 import tempfile | 8 import tempfile |
| 9 import unittest | 9 import unittest |
| 10 | 10 |
| 11 from compile import Checker | 11 from compile import Checker |
| 12 from processor import FileCache, Processor | 12 from processor import FileCache, Processor |
| 13 | 13 |
| 14 | 14 |
| 15 _SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) | 15 _SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 16 _SRC_DIR = os.path.join(_SCRIPT_DIR, os.pardir, os.pardir) | 16 _SRC_DIR = os.path.join(_SCRIPT_DIR, os.pardir, os.pardir) |
| 17 _RESOURCES_DIR = os.path.join(_SRC_DIR, "ui", "webui", "resources", "js") | 17 _RESOURCES_DIR = os.path.join(_SRC_DIR, "ui", "webui", "resources", "js") |
| 18 _ASSERT_JS = os.path.join(_RESOURCES_DIR, "assert.js") | 18 _ASSERT_JS = os.path.join(_RESOURCES_DIR, "assert.js") |
| 19 _CR_JS = os.path.join(_RESOURCES_DIR, "cr.js") | 19 _CR_JS = os.path.join(_RESOURCES_DIR, "cr.js") |
| 20 _CR_UI_JS = os.path.join(_RESOURCES_DIR, "cr", "ui.js") | 20 _CR_UI_JS = os.path.join(_RESOURCES_DIR, "cr", "ui.js") |
| 21 _PROMISE_RESOLVER_JS = os.path.join(_RESOURCES_DIR, "promise_resolver.js") | 21 _PROMISE_RESOLVER_JS = os.path.join(_RESOURCES_DIR, "promise_resolver.js") |
| 22 _POLYMER_EXTERNS = os.path.join(_SCRIPT_DIR, "externs", "polymer-1.0.js") | 22 _POLYMER_EXTERNS = os.path.join(_SCRIPT_DIR, "externs", "polymer-1.0.js") |
| 23 _CHROME_SEND_EXTERNS = os.path.join(_SRC_DIR, "third_party", "closure_compiler", | 23 _CHROME_SEND_EXTERNS = os.path.join(_SRC_DIR, "third_party", "closure_compiler", |
| 24 "externs", "chrome_send.js") | 24 "externs", "chrome_send.js") |
| 25 _CLOSURE_ARGS_GYPI = os.path.join(_SCRIPT_DIR, "closure_args.gypi") | 25 _CLOSURE_ARGS_GYPI = os.path.join(_SCRIPT_DIR, "closure_args.gypi") |
| 26 _GYPI_DICT = literal_eval(open(_CLOSURE_ARGS_GYPI).read()) | 26 _GYPI_DICT = literal_eval(open(_CLOSURE_ARGS_GYPI).read()) |
| 27 _COMMON_CLOSURE_ARGS = _GYPI_DICT["default_closure_args"] + \ | 27 _COMMON_CLOSURE_ARGS = _GYPI_DICT["default_closure_args"] + \ |
| 28 _GYPI_DICT["default_disabled_closure_args"] | 28 _GYPI_DICT["default_disabled_closure_args"] |
| 29 _RUNNER_ARGS = ["enable-chrome-pass"] | |
| 30 | 29 |
| 31 class CompilerTest(unittest.TestCase): | 30 class CompilerTest(unittest.TestCase): |
| 32 _ASSERT_DEFINITION = Processor(_ASSERT_JS).contents | 31 _ASSERT_DEFINITION = Processor(_ASSERT_JS).contents |
| 33 _PROMISE_RESOLVER_DEFINITION = (_ASSERT_DEFINITION + | 32 _PROMISE_RESOLVER_DEFINITION = (_ASSERT_DEFINITION + |
| 34 Processor(_PROMISE_RESOLVER_JS).contents) | 33 Processor(_PROMISE_RESOLVER_JS).contents) |
| 35 _CR_DEFINE_DEFINITION = (_PROMISE_RESOLVER_DEFINITION + | 34 _CR_DEFINE_DEFINITION = (_PROMISE_RESOLVER_DEFINITION + |
| 36 Processor(_CR_JS).contents) | 35 Processor(_CR_JS).contents) |
| 37 _CR_UI_DECORATE_DEFINITION = Processor(_CR_UI_JS).contents | 36 _CR_UI_DECORATE_DEFINITION = Processor(_CR_UI_JS).contents |
| 38 | 37 |
| 39 def setUp(self): | 38 def setUp(self): |
| 40 self._checker = Checker() | 39 self._checker = Checker() |
| 41 self._tmp_files = [] | 40 self._tmp_files = [] |
| 42 | 41 |
| 43 def tearDown(self): | 42 def tearDown(self): |
| 44 for file in self._tmp_files: | 43 for file in self._tmp_files: |
| 45 if os.path.exists(file): | 44 if os.path.exists(file): |
| 46 os.remove(file) | 45 os.remove(file) |
| 47 | 46 |
| 48 def _runChecker(self, source_code, closure_args=None): | 47 def _runChecker(self, source_code, needs_output, closure_args=None): |
| 49 file_path = "/script.js" | 48 file_path = "/script.js" |
| 50 FileCache._cache[file_path] = source_code | 49 FileCache._cache[file_path] = source_code |
| 51 out_file, out_map = self._createOutFiles() | 50 out_file, out_map = self._createOutFiles() |
| 52 args = _COMMON_CLOSURE_ARGS + (closure_args or []) | 51 args = _COMMON_CLOSURE_ARGS + (closure_args or []) |
| 52 if needs_output: |
| 53 args.remove("checks_only") |
| 53 | 54 |
| 54 externs = [_POLYMER_EXTERNS, _CHROME_SEND_EXTERNS] | 55 externs = [_POLYMER_EXTERNS, _CHROME_SEND_EXTERNS] |
| 55 found_errors, stderr = self._checker.check(file_path, | 56 found_errors, stderr = self._checker.check(file_path, |
| 56 externs=externs, | 57 externs=externs, |
| 57 out_file=out_file, | 58 out_file=out_file, |
| 58 runner_args=_RUNNER_ARGS, | |
| 59 closure_args=args) | 59 closure_args=args) |
| 60 return found_errors, stderr, out_file, out_map | 60 return found_errors, stderr, out_file, out_map |
| 61 | 61 |
| 62 def _runCheckerTestExpectError(self, source_code, expected_error, | 62 def _runCheckerTestExpectError(self, source_code, expected_error, |
| 63 closure_args=None): | 63 closure_args=None): |
| 64 _, stderr, out_file, out_map = self._runChecker(source_code, closure_args) | 64 _, stderr, out_file, out_map = self._runChecker( |
| 65 source_code, needs_output=False, closure_args=closure_args) |
| 65 | 66 |
| 66 self.assertTrue(expected_error in stderr, | 67 self.assertTrue(expected_error in stderr, |
| 67 msg="Expected chunk: \n%s\n\nOutput:\n%s\n" % ( | 68 msg="Expected chunk: \n%s\n\nOutput:\n%s\n" % ( |
| 68 expected_error, stderr)) | 69 expected_error, stderr)) |
| 69 self.assertFalse(os.path.exists(out_file)) | 70 self.assertFalse(os.path.exists(out_file)) |
| 70 self.assertFalse(os.path.exists(out_map)) | 71 self.assertFalse(os.path.exists(out_map)) |
| 71 | 72 |
| 72 def _runCheckerTestExpectSuccess(self, source_code, expected_output=None, | 73 def _runCheckerTestExpectSuccess(self, source_code, expected_output=None, |
| 73 closure_args=None): | 74 closure_args=None): |
| 74 found_errors, stderr, out_file, out_map = self._runChecker(source_code, | 75 found_errors, stderr, out_file, out_map = self._runChecker( |
| 75 closure_args) | 76 source_code, needs_output=True, closure_args=closure_args) |
| 76 | 77 |
| 77 self.assertFalse(found_errors, | 78 self.assertFalse(found_errors, |
| 78 msg="Expected success, but got failure\n\nOutput:\n%s\n" % stderr) | 79 msg="Expected success, but got failure\n\nOutput:\n%s\n" % stderr) |
| 79 | 80 |
| 80 self.assertTrue(os.path.exists(out_map)) | 81 self.assertTrue(os.path.exists(out_map)) |
| 81 self.assertTrue(os.path.exists(out_file)) | 82 self.assertTrue(os.path.exists(out_file)) |
| 82 if expected_output: | 83 if expected_output: |
| 83 with open(out_file, "r") as file: | 84 with open(out_file, "r") as file: |
| 84 self.assertEquals(file.read(), expected_output) | 85 self.assertEquals(file.read(), expected_output) |
| 85 | 86 |
| (...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 314 f.write(""" | 315 f.write(""" |
| 315 goog.require('testScript'); | 316 goog.require('testScript'); |
| 316 | 317 |
| 317 testScript(); | 318 testScript(); |
| 318 """) | 319 """) |
| 319 self._tmp_files.append(source_file2.name) | 320 self._tmp_files.append(source_file2.name) |
| 320 | 321 |
| 321 out_file, out_map = self._createOutFiles() | 322 out_file, out_map = self._createOutFiles() |
| 322 sources = [source_file1.name, source_file2.name] | 323 sources = [source_file1.name, source_file2.name] |
| 323 externs = [_POLYMER_EXTERNS] | 324 externs = [_POLYMER_EXTERNS] |
| 325 closure_args = [a for a in _COMMON_CLOSURE_ARGS if a != "checks_only"] |
| 324 found_errors, stderr = self._checker.check_multiple( | 326 found_errors, stderr = self._checker.check_multiple( |
| 325 sources, externs=externs, out_file=out_file, | 327 sources, externs=externs, out_file=out_file, closure_args=closure_args) |
| 326 closure_args=_COMMON_CLOSURE_ARGS) | |
| 327 self.assertFalse(found_errors, | 328 self.assertFalse(found_errors, |
| 328 msg="Expected success, but got failure\n\nOutput:\n%s\n" % stderr) | 329 msg="Expected success, but got failure\n\nOutput:\n%s\n" % stderr) |
| 329 | 330 |
| 330 expected_output = "'use strict';var testScript=function(){};testScript();\n" | 331 expected_output = "'use strict';var testScript=function(){};testScript();\n" |
| 331 self.assertTrue(os.path.exists(out_map)) | 332 self.assertTrue(os.path.exists(out_map)) |
| 332 self.assertTrue(os.path.exists(out_file)) | 333 self.assertTrue(os.path.exists(out_file)) |
| 333 with open(out_file, "r") as file: | 334 with open(out_file, "r") as file: |
| 334 self.assertEquals(file.read(), expected_output) | 335 self.assertEquals(file.read(), expected_output) |
| 335 | 336 |
| 336 def testExportPath(self): | 337 def testExportPath(self): |
| (...skipping 30 matching lines...) Expand all Loading... |
| 367 """ | 368 """ |
| 368 args = ['warning_level=VERBOSE'] | 369 args = ['warning_level=VERBOSE'] |
| 369 self._runCheckerTestExpectError(template % '', 'Missing return', | 370 self._runCheckerTestExpectError(template % '', 'Missing return', |
| 370 closure_args=args) | 371 closure_args=args) |
| 371 self._runCheckerTestExpectSuccess(template % 'assertNotReached();', | 372 self._runCheckerTestExpectSuccess(template % 'assertNotReached();', |
| 372 closure_args=args) | 373 closure_args=args) |
| 373 | 374 |
| 374 | 375 |
| 375 if __name__ == "__main__": | 376 if __name__ == "__main__": |
| 376 unittest.main() | 377 unittest.main() |
| OLD | NEW |