| 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 compile2 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") | |
| 23 _CHROME_SEND_EXTERNS = os.path.join(_SRC_DIR, "third_party", "closure_compiler", | 22 _CHROME_SEND_EXTERNS = os.path.join(_SRC_DIR, "third_party", "closure_compiler", |
| 24 "externs", "chrome_send.js") | 23 "externs", "chrome_send.js") |
| 25 _CLOSURE_ARGS_GYPI = os.path.join(_SCRIPT_DIR, "closure_args.gypi") | 24 _CLOSURE_ARGS_GYPI = os.path.join(_SCRIPT_DIR, "closure_args.gypi") |
| 26 _GYPI_DICT = literal_eval(open(_CLOSURE_ARGS_GYPI).read()) | 25 _GYPI_DICT = literal_eval(open(_CLOSURE_ARGS_GYPI).read()) |
| 27 _COMMON_CLOSURE_ARGS = _GYPI_DICT["default_closure_args"] + \ | 26 _COMMON_CLOSURE_ARGS = _GYPI_DICT["default_closure_args"] + \ |
| 28 _GYPI_DICT["default_disabled_closure_args"] | 27 _GYPI_DICT["default_disabled_closure_args"] |
| 29 | 28 |
| 30 class CompilerTest(unittest.TestCase): | 29 class CompilerTest(unittest.TestCase): |
| 31 _ASSERT_DEFINITION = Processor(_ASSERT_JS).contents | 30 _ASSERT_DEFINITION = Processor(_ASSERT_JS).contents |
| 32 _PROMISE_RESOLVER_DEFINITION = (_ASSERT_DEFINITION + | 31 _PROMISE_RESOLVER_DEFINITION = (_ASSERT_DEFINITION + |
| (...skipping 12 matching lines...) Expand all Loading... |
| 45 os.remove(file) | 44 os.remove(file) |
| 46 | 45 |
| 47 def _runChecker(self, source_code, needs_output, closure_args=None): | 46 def _runChecker(self, source_code, needs_output, closure_args=None): |
| 48 file_path = "/script.js" | 47 file_path = "/script.js" |
| 49 FileCache._cache[file_path] = source_code | 48 FileCache._cache[file_path] = source_code |
| 50 out_file, out_map = self._createOutFiles() | 49 out_file, out_map = self._createOutFiles() |
| 51 args = _COMMON_CLOSURE_ARGS + (closure_args or []) | 50 args = _COMMON_CLOSURE_ARGS + (closure_args or []) |
| 52 if needs_output: | 51 if needs_output: |
| 53 args.remove("checks_only") | 52 args.remove("checks_only") |
| 54 | 53 |
| 55 externs = [_POLYMER_EXTERNS, _CHROME_SEND_EXTERNS] | 54 sources = [file_path, _CHROME_SEND_EXTERNS] |
| 56 found_errors, stderr = self._checker.check(file_path, | 55 found_errors, stderr = self._checker.check(sources, |
| 57 externs=externs, | |
| 58 out_file=out_file, | 56 out_file=out_file, |
| 59 closure_args=args) | 57 closure_args=args) |
| 60 return found_errors, stderr, out_file, out_map | 58 return found_errors, stderr, out_file, out_map |
| 61 | 59 |
| 62 def _runCheckerTestExpectError(self, source_code, expected_error, | 60 def _runCheckerTestExpectError(self, source_code, expected_error, |
| 63 closure_args=None): | 61 closure_args=None): |
| 64 _, stderr, out_file, out_map = self._runChecker( | 62 _, stderr, out_file, out_map = self._runChecker( |
| 65 source_code, needs_output=False, closure_args=closure_args) | 63 source_code, needs_output=False, closure_args=closure_args) |
| 66 | 64 |
| 67 self.assertTrue(expected_error in stderr, | 65 self.assertTrue(expected_error in stderr, |
| (...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 var testScript = function() { | 291 var testScript = function() { |
| 294 console.log("hello world"); | 292 console.log("hello world"); |
| 295 }; | 293 }; |
| 296 """ | 294 """ |
| 297 expected_output = ("""(function(){'use strict';var testScript=function()""" | 295 expected_output = ("""(function(){'use strict';var testScript=function()""" |
| 298 """{console.log("hello world")};})();\n""") | 296 """{console.log("hello world")};})();\n""") |
| 299 closure_args=["output_wrapper='(function(){%output%})();'"] | 297 closure_args=["output_wrapper='(function(){%output%})();'"] |
| 300 self._runCheckerTestExpectSuccess(source_code, expected_output, | 298 self._runCheckerTestExpectSuccess(source_code, expected_output, |
| 301 closure_args) | 299 closure_args) |
| 302 | 300 |
| 303 def testCheckMultiple(self): | 301 def testCustomSources(self): |
| 304 source_file1 = tempfile.NamedTemporaryFile(delete=False) | 302 source_file1 = tempfile.NamedTemporaryFile(delete=False) |
| 305 with open(source_file1.name, "w") as f: | 303 with open(source_file1.name, "w") as f: |
| 306 f.write(""" | 304 f.write(""" |
| 307 goog.provide('testScript'); | 305 goog.provide('testScript'); |
| 308 | 306 |
| 309 var testScript = function() {}; | 307 var testScript = function() {}; |
| 310 """) | 308 """) |
| 311 self._tmp_files.append(source_file1.name) | 309 self._tmp_files.append(source_file1.name) |
| 312 | 310 |
| 313 source_file2 = tempfile.NamedTemporaryFile(delete=False) | 311 source_file2 = tempfile.NamedTemporaryFile(delete=False) |
| 314 with open(source_file2.name, "w") as f: | 312 with open(source_file2.name, "w") as f: |
| 315 f.write(""" | 313 f.write(""" |
| 316 goog.require('testScript'); | 314 goog.require('testScript'); |
| 317 | 315 |
| 318 testScript(); | 316 testScript(); |
| 319 """) | 317 """) |
| 320 self._tmp_files.append(source_file2.name) | 318 self._tmp_files.append(source_file2.name) |
| 321 | 319 |
| 322 out_file, out_map = self._createOutFiles() | 320 out_file, out_map = self._createOutFiles() |
| 323 sources = [source_file1.name, source_file2.name] | 321 sources = [source_file1.name, source_file2.name] |
| 324 externs = [_POLYMER_EXTERNS] | |
| 325 closure_args = [a for a in _COMMON_CLOSURE_ARGS if a != "checks_only"] | 322 closure_args = [a for a in _COMMON_CLOSURE_ARGS if a != "checks_only"] |
| 326 found_errors, stderr = self._checker.check_multiple( | 323 found_errors, stderr = self._checker.check(sources, out_file=out_file, |
| 327 sources, externs=externs, out_file=out_file, closure_args=closure_args) | 324 closure_args=closure_args, |
| 325 custom_sources=True) |
| 328 self.assertFalse(found_errors, | 326 self.assertFalse(found_errors, |
| 329 msg="Expected success, but got failure\n\nOutput:\n%s\n" % stderr) | 327 msg="Expected success, but got failure\n\nOutput:\n%s\n" % stderr) |
| 330 | 328 |
| 331 expected_output = "'use strict';var testScript=function(){};testScript();\n" | 329 expected_output = "'use strict';var testScript=function(){};testScript();\n" |
| 332 self.assertTrue(os.path.exists(out_map)) | 330 self.assertTrue(os.path.exists(out_map)) |
| 333 self.assertTrue(os.path.exists(out_file)) | 331 self.assertTrue(os.path.exists(out_file)) |
| 334 with open(out_file, "r") as file: | 332 with open(out_file, "r") as file: |
| 335 self.assertEquals(file.read(), expected_output) | 333 self.assertEquals(file.read(), expected_output) |
| 336 | 334 |
| 337 def testExportPath(self): | 335 def testExportPath(self): |
| (...skipping 30 matching lines...) Expand all Loading... |
| 368 """ | 366 """ |
| 369 args = ['warning_level=VERBOSE'] | 367 args = ['warning_level=VERBOSE'] |
| 370 self._runCheckerTestExpectError(template % '', 'Missing return', | 368 self._runCheckerTestExpectError(template % '', 'Missing return', |
| 371 closure_args=args) | 369 closure_args=args) |
| 372 self._runCheckerTestExpectSuccess(template % 'assertNotReached();', | 370 self._runCheckerTestExpectSuccess(template % 'assertNotReached();', |
| 373 closure_args=args) | 371 closure_args=args) |
| 374 | 372 |
| 375 | 373 |
| 376 if __name__ == "__main__": | 374 if __name__ == "__main__": |
| 377 unittest.main() | 375 unittest.main() |
| OLD | NEW |