jscompiler_test.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2013 The Closure Library Authors. All Rights Reserved.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS-IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """Unit test for depstree."""
  17. __author__ = 'nnaze@google.com (Nathan Naze)'
  18. import os
  19. import unittest
  20. import jscompiler
  21. class JsCompilerTestCase(unittest.TestCase):
  22. """Unit tests for jscompiler module."""
  23. def testGetFlagFile(self):
  24. flags_file = jscompiler._GetFlagFile(['path/to/src1.js', 'path/to/src2.js'],
  25. ['--test_compiler_flag'])
  26. def file_get_contents(filename):
  27. with open(filename) as f:
  28. content = f.read()
  29. f.close()
  30. return content
  31. flags_file_content = file_get_contents(flags_file.name)
  32. os.remove(flags_file.name)
  33. self.assertEqual(
  34. '--js path/to/src1.js --js path/to/src2.js --test_compiler_flag',
  35. flags_file_content)
  36. def testGetJsCompilerArgs(self):
  37. original_check = jscompiler._JavaSupports32BitMode
  38. jscompiler._JavaSupports32BitMode = lambda: False
  39. args = jscompiler._GetJsCompilerArgs('path/to/jscompiler.jar', (1, 7),
  40. ['--test_jvm_flag'])
  41. self.assertEqual([
  42. 'java', '-client', '--test_jvm_flag', '-jar', 'path/to/jscompiler.jar'
  43. ], args)
  44. def CheckJava15RaisesError():
  45. jscompiler._GetJsCompilerArgs('path/to/jscompiler.jar', (1, 5),
  46. ['--test_jvm_flag'])
  47. self.assertRaises(jscompiler.JsCompilerError, CheckJava15RaisesError)
  48. jscompiler._JavaSupports32BitMode = original_check
  49. def testGetJsCompilerArgs32BitJava(self):
  50. original_check = jscompiler._JavaSupports32BitMode
  51. # Should include the -d32 flag only if 32-bit Java is supported by the
  52. # system.
  53. jscompiler._JavaSupports32BitMode = lambda: True
  54. args = jscompiler._GetJsCompilerArgs('path/to/jscompiler.jar', (1, 7),
  55. ['--test_jvm_flag'])
  56. self.assertEqual([
  57. 'java', '-d32', '-client', '--test_jvm_flag', '-jar',
  58. 'path/to/jscompiler.jar'
  59. ], args)
  60. # Should exclude the -d32 flag if 32-bit Java is not supported by the
  61. # system.
  62. jscompiler._JavaSupports32BitMode = lambda: False
  63. args = jscompiler._GetJsCompilerArgs('path/to/jscompiler.jar', (1, 7),
  64. ['--test_jvm_flag'])
  65. self.assertEqual([
  66. 'java', '-client', '--test_jvm_flag', '-jar', 'path/to/jscompiler.jar'
  67. ], args)
  68. jscompiler._JavaSupports32BitMode = original_check
  69. def testGetJavaVersion(self):
  70. def assertVersion(expected, version_string):
  71. self.assertEquals(expected, jscompiler._ParseJavaVersion(version_string))
  72. assertVersion((9, 0), _TEST_JAVA_JEP_223_VERSION_STRING)
  73. assertVersion((1, 7), _TEST_JAVA_VERSION_STRING)
  74. assertVersion((1, 6), _TEST_JAVA_NESTED_VERSION_STRING)
  75. assertVersion((1, 4), 'java version "1.4.0_03-ea"')
  76. _TEST_JAVA_VERSION_STRING = """\
  77. openjdk version "1.7.0-google-v5"
  78. OpenJDK Runtime Environment (build 1.7.0-google-v5-64327-39803485)
  79. OpenJDK Server VM (build 22.0-b10, mixed mode)
  80. """
  81. _TEST_JAVA_NESTED_VERSION_STRING = """\
  82. Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8
  83. java version "1.6.0_35"
  84. Java(TM) SE Runtime Environment (build 1.6.0_35-b10-428-11M3811)
  85. Java HotSpot(TM) Client VM (build 20.10-b01-428, mixed mode)
  86. """
  87. _TEST_JAVA_JEP_223_VERSION_STRING = """\
  88. openjdk version "9-Ubuntu"
  89. OpenJDK Runtime Environment (build 9-Ubuntu+0-9b134-2ubuntu1)
  90. OpenJDK 64-Bit Server VM (build 9-Ubuntu+0-9b134-2ubuntu1, mixed mode)
  91. """
  92. if __name__ == '__main__':
  93. unittest.main()