Index: third_party/closure_compiler/compile_test.py |
diff --git a/third_party/closure_compiler/compile_test.py b/third_party/closure_compiler/compile_test.py |
new file mode 100755 |
index 0000000000000000000000000000000000000000..7f99f777d67e8c855b23a6e00e62ec146e975388 |
--- /dev/null |
+++ b/third_party/closure_compiler/compile_test.py |
@@ -0,0 +1,126 @@ |
+#!/usr/bin/env python |
+# Copyright 2015 The Chromium Authors. All rights reserved. |
+# Use of this source code is governed by a BSD-style license that can be |
+# found in the LICENSE file. |
+ |
+import os |
+import unittest |
+ |
+from compile import Checker |
+from processor import FileCache |
+ |
+_SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) |
+_SRC_DIR = os.path.join(_SCRIPT_DIR, os.pardir, os.pardir) |
+_POLYMER_EXTERNS = os.path.join(_SRC_DIR, "third_party", "polymer", "v0_8", |
+ "components-chromium", "polymer-externs", |
+ "polymer.externs.js") |
Dan Beam
2015/05/19 17:05:38
can we merge this in with the customization test?
Theresa
2015/05/19 17:48:46
Done.
|
+ |
+class CompileTest(unittest.TestCase): |
+ |
+ def setUp(self): |
+ self._checker = Checker() |
+ |
+ def _runChecker(self, source_code, output_wrapper=None): |
+ source_file = "/tmp/script.js" |
+ FileCache._cache[source_file] = source_code |
+ out_file, out_map = self._createOutFiles() |
+ |
+ found_errors, stderr = self._checker.check(source_file, |
+ externs=[_POLYMER_EXTERNS], |
+ out_file=out_file, |
+ output_wrapper=output_wrapper) |
+ return found_errors, stderr, out_file, out_map |
+ |
+ def _runCheckerTestExpectError(self, source_code, expected_error): |
+ _, stderr, out_file, out_map = self._runChecker(source_code) |
+ |
+ self.assertTrue(expected_error in stderr, |
+ msg="Expected chunk: \n%s\n\nOutput:\n%s\n" % ( |
+ expected_error, stderr)) |
+ self.assertFalse(os.path.exists(out_file)) |
+ self.assertFalse(os.path.exists(out_map)) |
+ |
+ def _runCheckerTestExpectSuccess(self, source_code, expected_output, |
+ output_wrapper=None): |
+ found_errors, stderr, out_file, out_map = self._runChecker(source_code, |
+ output_wrapper) |
+ |
+ self.assertFalse(found_errors, |
+ msg="Expected success, but got failure\n\nOutput:\n%s\n" % stderr) |
+ |
+ self.assertTrue(os.path.exists(out_map)) |
+ self.assertTrue(os.path.exists(out_file)) |
+ with open(out_file, 'r') as file: |
+ self.assertEquals(file.read(), expected_output) |
+ |
+ self._removeFiles([out_file, out_map]) |
+ |
+ def _createOutFiles(self): |
+ out_file = "/tmp/gen/script.js" |
+ out_map = "/tmp/gen/script.js.map" |
+ |
+ out_dir = os.path.dirname(out_file) |
+ if not os.path.exists(out_dir): |
+ os.makedirs(out_dir) |
+ |
+ return out_file, out_map |
+ |
+ def _removeFiles(self, files): |
+ for file in files: |
+ if(os.path.exists(file)): |
+ os.remove(file) |
+ |
+ def testValidScriptCompilation(self): |
+ self._runCheckerTestExpectSuccess(""" |
+var testScript = function() { console.log("hello world") };""", |
+"""'use strict';var testScript=function(){console.log("hello world")};\n""") |
+ |
+ def testInvalidScriptCompalition(self): |
+ self._runCheckerTestExpectError(""" |
+console.log(nonExistentVar); |
+""", "ERROR - variable nonExistentVar is undeclared") |
+ |
+ def testOutputWrapper(self): |
+ self._runCheckerTestExpectSuccess(""" |
+var testScript = function() { console.log("hello world") }; |
+""", """(function(){'use strict';var testScript=function() |
+{console.log("hello world")};})();\n""", |
+output_wrapper="(function(){%output%})();") |
+ |
+ def testCheckMultiple(self): |
+ source_file = "/tmp/script.js" |
+ with open(source_file, 'w') as f: |
+ f.write(""" |
+goog.require('testScript'); |
+ |
+testScript(); |
+""") |
+ |
+ source_file2 = "/tmp/script2.js" |
+ with open(source_file2, 'w') as f: |
+ f.write(""" |
+goog.provide('testScript'); |
+ |
+var testScript = function(){}; |
+""") |
+ |
+ out_file, out_map = self._createOutFiles() |
+ sources = [source_file, source_file2] |
+ externs = [_POLYMER_EXTERNS] |
+ found_errors, stderr = self._checker.check_multiple(sources, |
+ externs=externs, |
+ out_file=out_file) |
+ self.assertFalse(found_errors, |
+ msg="Expected success, but got failure\n\nOutput:\n%s\n" % stderr) |
+ |
+ expected_output = "'use strict';var testScript=function(){};testScript();\n" |
+ self.assertTrue(os.path.exists(out_map)) |
+ self.assertTrue(os.path.exists(out_file)) |
+ with open(out_file, 'r') as file: |
+ self.assertEquals(file.read(), expected_output) |
+ |
+ self._removeFiles([source_file, source_file2, out_file, out_map]) |
+ |
+ |
+if __name__ == "__main__": |
+ unittest.main() |