version_test.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. # Copyright 2019 The Chromium Authors. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. import os
  5. import unittest
  6. import mock
  7. import version
  8. def _ReplaceArgs(args, *replacements):
  9. new_args = args[:]
  10. for flag, val in replacements:
  11. flag_index = args.index(flag)
  12. new_args[flag_index + 1] = val
  13. return new_args
  14. class _VersionTest(unittest.TestCase):
  15. """Unittests for the version module.
  16. """
  17. _CHROME_VERSION_FILE = os.path.join(
  18. os.path.dirname(__file__), os.pardir, os.pardir, 'chrome', 'VERSION')
  19. _SCRIPT = os.path.join(os.path.dirname(__file__), 'version.py')
  20. _EXAMPLE_VERSION = {
  21. 'MAJOR': '74',
  22. 'MINOR': '0',
  23. 'BUILD': '3720',
  24. 'PATCH': '0',
  25. }
  26. _EXAMPLE_TEMPLATE = (
  27. 'full = "@MAJOR@.@MINOR@.@BUILD@.@PATCH@" '
  28. 'major = "@MAJOR@" minor = "@MINOR@" '
  29. 'build = "@BUILD@" patch = "@PATCH@" version_id = @VERSION_ID@ ')
  30. _ANDROID_CHROME_VARS = [
  31. 'chrome_version_code',
  32. 'chrome_modern_version_code',
  33. 'monochrome_version_code',
  34. 'trichrome_version_code',
  35. 'webview_stable_version_code',
  36. 'webview_beta_version_code',
  37. 'webview_dev_version_code',
  38. ]
  39. _EXAMPLE_ANDROID_TEMPLATE = (
  40. _EXAMPLE_TEMPLATE + ''.join(
  41. ['%s = "@%s@" ' % (el, el.upper()) for el in _ANDROID_CHROME_VARS]))
  42. _EXAMPLE_ARGS = [
  43. '-f',
  44. _CHROME_VERSION_FILE,
  45. '-t',
  46. _EXAMPLE_TEMPLATE,
  47. ]
  48. _EXAMPLE_ANDROID_ARGS = _ReplaceArgs(_EXAMPLE_ARGS,
  49. ['-t', _EXAMPLE_ANDROID_TEMPLATE]) + [
  50. '-a',
  51. 'arm',
  52. '--os',
  53. 'android',
  54. ]
  55. @staticmethod
  56. def _RunBuildOutput(new_version_values={},
  57. get_new_args=lambda old_args: old_args):
  58. """Parameterized helper method for running the main testable method in
  59. version.py.
  60. Keyword arguments:
  61. new_version_values -- dict used to update _EXAMPLE_VERSION
  62. get_new_args -- lambda for updating _EXAMPLE_ANDROID_ARGS
  63. """
  64. with mock.patch('version.FetchValuesFromFile') as \
  65. fetch_values_from_file_mock:
  66. fetch_values_from_file_mock.side_effect = (lambda values, file :
  67. values.update(
  68. dict(_VersionTest._EXAMPLE_VERSION, **new_version_values)))
  69. new_args = get_new_args(_VersionTest._EXAMPLE_ARGS)
  70. return version.BuildOutput(new_args)
  71. def testFetchValuesFromFile(self):
  72. """It returns a dict in correct format - { <str>: <str> }, to verify
  73. assumption of other tests that mock this function
  74. """
  75. result = {}
  76. version.FetchValuesFromFile(result, self._CHROME_VERSION_FILE)
  77. for key, val in result.items():
  78. self.assertIsInstance(key, str)
  79. self.assertIsInstance(val, str)
  80. def testBuildOutputAndroid(self):
  81. """Assert it gives includes assignments of expected variables"""
  82. output = self._RunBuildOutput(
  83. get_new_args=lambda args: self._EXAMPLE_ANDROID_ARGS)
  84. contents = output['contents']
  85. self.assertRegex(contents, r'\bchrome_version_code = "\d+"\s')
  86. self.assertRegex(contents, r'\bchrome_modern_version_code = "\d+"\s')
  87. self.assertRegex(contents, r'\bmonochrome_version_code = "\d+"\s')
  88. self.assertRegex(contents, r'\btrichrome_version_code = "\d+"\s')
  89. self.assertRegex(contents, r'\bwebview_stable_version_code = "\d+"\s')
  90. self.assertRegex(contents, r'\bwebview_beta_version_code = "\d+"\s')
  91. self.assertRegex(contents, r'\bwebview_dev_version_code = "\d+"\s')
  92. def testBuildOutputAndroidArchVariantsArm64(self):
  93. """Assert 64-bit-specific version codes"""
  94. new_template = (
  95. self._EXAMPLE_ANDROID_TEMPLATE +
  96. "monochrome_64_32_version_code = \"@MONOCHROME_64_32_VERSION_CODE@\" "
  97. "monochrome_64_version_code = \"@MONOCHROME_64_VERSION_CODE@\" "
  98. "trichrome_64_32_version_code = \"@TRICHROME_64_32_VERSION_CODE@\" "
  99. "trichrome_64_version_code = \"@TRICHROME_64_VERSION_CODE@\" ")
  100. args_with_template = _ReplaceArgs(self._EXAMPLE_ANDROID_ARGS,
  101. ['-t', new_template])
  102. new_args = _ReplaceArgs(args_with_template, ['-a', 'arm64'])
  103. output = self._RunBuildOutput(get_new_args=lambda args: new_args)
  104. contents = output['contents']
  105. self.assertRegex(contents, r'\bmonochrome_64_32_version_code = "\d+"\s')
  106. self.assertRegex(contents, r'\bmonochrome_64_version_code = "\d+"\s')
  107. self.assertRegex(contents, r'\btrichrome_64_32_version_code = "\d+"\s')
  108. self.assertRegex(contents, r'\btrichrome_64_version_code = "\d+"\s')
  109. def testBuildOutputAndroidArchVariantsX64(self):
  110. """Assert 64-bit-specific version codes"""
  111. new_template = (
  112. self._EXAMPLE_ANDROID_TEMPLATE +
  113. "monochrome_64_32_version_code = \"@MONOCHROME_64_32_VERSION_CODE@\" "
  114. "monochrome_64_version_code = \"@MONOCHROME_64_VERSION_CODE@\" "
  115. "trichrome_64_32_version_code = \"@TRICHROME_64_32_VERSION_CODE@\" "
  116. "trichrome_64_version_code = \"@TRICHROME_64_VERSION_CODE@\" ")
  117. args_with_template = _ReplaceArgs(self._EXAMPLE_ANDROID_ARGS,
  118. ['-t', new_template])
  119. new_args = _ReplaceArgs(args_with_template, ['-a', 'x64'])
  120. output = self._RunBuildOutput(get_new_args=lambda args: new_args)
  121. contents = output['contents']
  122. self.assertRegex(contents, r'\bmonochrome_64_32_version_code = "\d+"\s')
  123. self.assertRegex(contents, r'\bmonochrome_64_version_code = "\d+"\s')
  124. self.assertRegex(contents, r'\btrichrome_64_32_version_code = "\d+"\s')
  125. self.assertRegex(contents, r'\btrichrome_64_version_code = "\d+"\s')
  126. def testBuildOutputAndroidChromeArchInput(self):
  127. """Assert it raises an exception when using an invalid architecture input"""
  128. new_args = _ReplaceArgs(self._EXAMPLE_ANDROID_ARGS, ['-a', 'foobar'])
  129. # Mock sys.stderr because argparse will print to stderr when we pass
  130. # the invalid '-a' value.
  131. with self.assertRaises(SystemExit) as cm, mock.patch('sys.stderr'):
  132. self._RunBuildOutput(get_new_args=lambda args: new_args)
  133. self.assertEqual(cm.exception.code, 2)
  134. if __name__ == '__main__':
  135. unittest.main()