| 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") |
| 23 _CHROME_SEND_EXTERNS = os.path.join(_SRC_DIR, "third_party", "closure_compiler", | 24 _CHROME_SEND_EXTERNS = os.path.join(_SRC_DIR, "third_party", "closure_compiler", |
| 24 "externs", "chrome_send_externs.js") | 25 "externs", "chrome_send_externs.js") |
| 26 _GYPI_DICT = literal_eval(open(os.path.join(_SCRIPT_DIR, 'compile_js.gypi')).rea
d()) |
| 27 _COMMON_CLOSURE_ARGS =_GYPI_DICT['variables']['closure_args+'] |
| 28 _COMMON_CLOSURE_ARGS += _GYPI_DICT['actions'][0]['variables']['disabled_closure_
args%'] |
| 25 | 29 |
| 26 | 30 |
| 27 class CompilerTest(unittest.TestCase): | 31 class CompilerTest(unittest.TestCase): |
| 28 _ASSERT_DEFINITION = Processor(_ASSERT_JS).contents | 32 _ASSERT_DEFINITION = Processor(_ASSERT_JS).contents |
| 29 _CR_DEFINE_DEFINITION = Processor(_CR_JS).contents | 33 _CR_DEFINE_DEFINITION = Processor(_CR_JS).contents |
| 30 _CR_UI_DECORATE_DEFINITION = Processor(_CR_UI_JS).contents | 34 _CR_UI_DECORATE_DEFINITION = Processor(_CR_UI_JS).contents |
| 31 | 35 |
| 32 def setUp(self): | 36 def setUp(self): |
| 33 self._checker = Checker() | 37 self._checker = Checker() |
| 34 self._tmp_files = [] | 38 self._tmp_files = [] |
| 35 | 39 |
| 36 def tearDown(self): | 40 def tearDown(self): |
| 37 for file in self._tmp_files: | 41 for file in self._tmp_files: |
| 38 if os.path.exists(file): | 42 if os.path.exists(file): |
| 39 os.remove(file) | 43 os.remove(file) |
| 40 | 44 |
| 41 def _runChecker(self, source_code, output_wrapper=None): | 45 def _runChecker(self, source_code, closure_args=None): |
| 42 file_path = "/script.js" | 46 file_path = "/script.js" |
| 43 FileCache._cache[file_path] = source_code | 47 FileCache._cache[file_path] = source_code |
| 44 out_file, out_map = self._createOutFiles() | 48 out_file, out_map = self._createOutFiles() |
| 49 args = _COMMON_CLOSURE_ARGS + (closure_args or []) |
| 45 | 50 |
| 46 externs = [_POLYMER_EXTERNS, _CHROME_SEND_EXTERNS] | 51 externs = [_POLYMER_EXTERNS, _CHROME_SEND_EXTERNS] |
| 47 found_errors, stderr = self._checker.check(file_path, | 52 found_errors, stderr = self._checker.check(file_path, |
| 48 externs=externs, | 53 externs=externs, |
| 49 out_file=out_file, | 54 out_file=out_file, |
| 50 output_wrapper=output_wrapper) | 55 closure_args=args) |
| 51 return found_errors, stderr, out_file, out_map | 56 return found_errors, stderr, out_file, out_map |
| 52 | 57 |
| 53 def _runCheckerTestExpectError(self, source_code, expected_error): | 58 def _runCheckerTestExpectError(self, source_code, expected_error): |
| 54 _, stderr, out_file, out_map = self._runChecker(source_code) | 59 _, stderr, out_file, out_map = self._runChecker(source_code) |
| 55 | 60 |
| 56 self.assertTrue(expected_error in stderr, | 61 self.assertTrue(expected_error in stderr, |
| 57 msg="Expected chunk: \n%s\n\nOutput:\n%s\n" % ( | 62 msg="Expected chunk: \n%s\n\nOutput:\n%s\n" % ( |
| 58 expected_error, stderr)) | 63 expected_error, stderr)) |
| 59 self.assertFalse(os.path.exists(out_file)) | 64 self.assertFalse(os.path.exists(out_file)) |
| 60 self.assertFalse(os.path.exists(out_map)) | 65 self.assertFalse(os.path.exists(out_map)) |
| 61 | 66 |
| 62 def _runCheckerTestExpectSuccess(self, source_code, expected_output=None, | 67 def _runCheckerTestExpectSuccess(self, source_code, expected_output=None, |
| 63 output_wrapper=None): | 68 closure_args=None): |
| 64 found_errors, stderr, out_file, out_map = self._runChecker(source_code, | 69 found_errors, stderr, out_file, out_map = self._runChecker(source_code, |
| 65 output_wrapper) | 70 closure_args) |
| 66 | 71 |
| 67 self.assertFalse(found_errors, | 72 self.assertFalse(found_errors, |
| 68 msg="Expected success, but got failure\n\nOutput:\n%s\n" % stderr) | 73 msg="Expected success, but got failure\n\nOutput:\n%s\n" % stderr) |
| 69 | 74 |
| 70 self.assertTrue(os.path.exists(out_map)) | 75 self.assertTrue(os.path.exists(out_map)) |
| 71 self.assertTrue(os.path.exists(out_file)) | 76 self.assertTrue(os.path.exists(out_file)) |
| 72 if expected_output: | 77 if expected_output: |
| 73 with open(out_file, "r") as file: | 78 with open(out_file, "r") as file: |
| 74 self.assertEquals(file.read(), expected_output) | 79 self.assertEquals(file.read(), expected_output) |
| 75 | 80 |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 """'use strict';var testScript=function(){console.log("hello world")};\n""") | 283 """'use strict';var testScript=function(){console.log("hello world")};\n""") |
| 279 | 284 |
| 280 def testOutputWrapper(self): | 285 def testOutputWrapper(self): |
| 281 source_code = """ | 286 source_code = """ |
| 282 var testScript = function() { | 287 var testScript = function() { |
| 283 console.log("hello world"); | 288 console.log("hello world"); |
| 284 }; | 289 }; |
| 285 """ | 290 """ |
| 286 expected_output = ("""(function(){'use strict';var testScript=function()""" | 291 expected_output = ("""(function(){'use strict';var testScript=function()""" |
| 287 """{console.log("hello world")};})();\n""") | 292 """{console.log("hello world")};})();\n""") |
| 288 output_wrapper="(function(){%output%})();" | 293 closure_args=["output_wrapper='(function(){%output%})();'"] |
| 289 self._runCheckerTestExpectSuccess(source_code, expected_output, | 294 self._runCheckerTestExpectSuccess(source_code, expected_output, |
| 290 output_wrapper=output_wrapper) | 295 closure_args) |
| 291 | 296 |
| 292 def testCheckMultiple(self): | 297 def testCheckMultiple(self): |
| 293 source_file1 = tempfile.NamedTemporaryFile(delete=False) | 298 source_file1 = tempfile.NamedTemporaryFile(delete=False) |
| 294 with open(source_file1.name, "w") as f: | 299 with open(source_file1.name, "w") as f: |
| 295 f.write(""" | 300 f.write(""" |
| 296 goog.provide('testScript'); | 301 goog.provide('testScript'); |
| 297 | 302 |
| 298 var testScript = function() {}; | 303 var testScript = function() {}; |
| 299 """) | 304 """) |
| 300 self._tmp_files.append(source_file1.name) | 305 self._tmp_files.append(source_file1.name) |
| 301 | 306 |
| 302 source_file2 = tempfile.NamedTemporaryFile(delete=False) | 307 source_file2 = tempfile.NamedTemporaryFile(delete=False) |
| 303 with open(source_file2.name, "w") as f: | 308 with open(source_file2.name, "w") as f: |
| 304 f.write(""" | 309 f.write(""" |
| 305 goog.require('testScript'); | 310 goog.require('testScript'); |
| 306 | 311 |
| 307 testScript(); | 312 testScript(); |
| 308 """) | 313 """) |
| 309 self._tmp_files.append(source_file2.name) | 314 self._tmp_files.append(source_file2.name) |
| 310 | 315 |
| 311 out_file, out_map = self._createOutFiles() | 316 out_file, out_map = self._createOutFiles() |
| 312 sources = [source_file1.name, source_file2.name] | 317 sources = [source_file1.name, source_file2.name] |
| 313 externs = [_POLYMER_EXTERNS] | 318 externs = [_POLYMER_EXTERNS] |
| 314 found_errors, stderr = self._checker.check_multiple(sources, | 319 found_errors, stderr = self._checker.check_multiple( |
| 315 externs=externs, | 320 sources, externs=externs, out_file=out_file, |
| 316 out_file=out_file) | 321 closure_args=_COMMON_CLOSURE_ARGS) |
| 317 self.assertFalse(found_errors, | 322 self.assertFalse(found_errors, |
| 318 msg="Expected success, but got failure\n\nOutput:\n%s\n" % stderr) | 323 msg="Expected success, but got failure\n\nOutput:\n%s\n" % stderr) |
| 319 | 324 |
| 320 expected_output = "'use strict';var testScript=function(){};testScript();\n" | 325 expected_output = "'use strict';var testScript=function(){};testScript();\n" |
| 321 self.assertTrue(os.path.exists(out_map)) | 326 self.assertTrue(os.path.exists(out_map)) |
| 322 self.assertTrue(os.path.exists(out_file)) | 327 self.assertTrue(os.path.exists(out_file)) |
| 323 with open(out_file, "r") as file: | 328 with open(out_file, "r") as file: |
| 324 self.assertEquals(file.read(), expected_output) | 329 self.assertEquals(file.read(), expected_output) |
| 325 | 330 |
| 326 | 331 |
| 327 if __name__ == "__main__": | 332 if __name__ == "__main__": |
| 328 unittest.main() | 333 unittest.main() |
| OLD | NEW |