generate_skylab_deps_test.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright 2022 The Chromium Authors. All rights reserved.
  4. # Use of this source code is governed by a BSD-style license that can be
  5. # found in the LICENSE file.
  6. import unittest
  7. from unittest import mock
  8. import generate_skylab_deps
  9. TAST_CONTROL = '''
  10. # Ignore comments
  11. tast_disabled_tests_from_chrome_all = [
  12. "example.all.test1",
  13. ]
  14. tast_disabled_tests_from_chrome_m100 = [
  15. "example.m100.test1",
  16. ]
  17. tast_disabled_tests_from_lacros_all = []
  18. '''
  19. TAST_EXPR = '"group:mainline" && "dep:chrome" && !informational'
  20. REQUIRED_ARGS = ['script', 'generate-filter', '--output', 'output.filter']
  21. class GenerateSkylabDepsTest(unittest.TestCase):
  22. def testTastExpr(self):
  23. file_mock = mock.mock_open(read_data=TAST_CONTROL)
  24. args = REQUIRED_ARGS + ['--tast-expr', TAST_EXPR]
  25. with mock.patch('sys.argv', args),\
  26. mock.patch('builtins.open', file_mock),\
  27. mock.patch('os.chmod'),\
  28. mock.patch("json.dump", mock.MagicMock()) as dump:
  29. generate_skylab_deps.main()
  30. filter_dict = dump.call_args[0][0]
  31. self.assertEqual(filter_dict['default'], '(%s)' % TAST_EXPR)
  32. def testTastExprAndDisableTests(self):
  33. file_mock = mock.mock_open(read_data=TAST_CONTROL)
  34. args = REQUIRED_ARGS + [
  35. '--tast-expr', TAST_EXPR, '--disabled-tests', 'disabled.test1',
  36. '--disabled-tests', 'disabled.test2'
  37. ]
  38. with mock.patch('sys.argv', args),\
  39. mock.patch('builtins.open', file_mock),\
  40. mock.patch('os.chmod'),\
  41. mock.patch("json.dump", mock.MagicMock()) as dump:
  42. generate_skylab_deps.main()
  43. filter_dict = dump.call_args[0][0]
  44. self.assertEqual(
  45. filter_dict['default'],
  46. '(%s && !"name:disabled.test1" && !"name:disabled.test2")' %
  47. TAST_EXPR)
  48. def testEnableTests(self):
  49. file_mock = mock.mock_open(read_data=TAST_CONTROL)
  50. args = REQUIRED_ARGS + [
  51. '--enabled-tests', 'enabled.test1', '--enabled-tests', 'enabled.test2'
  52. ]
  53. with mock.patch('sys.argv', args),\
  54. mock.patch('builtins.open', file_mock),\
  55. mock.patch('os.chmod'),\
  56. mock.patch("json.dump", mock.MagicMock()) as dump:
  57. generate_skylab_deps.main()
  58. filter_dict = dump.call_args[0][0]
  59. self.assertEqual(filter_dict['default'],
  60. '("name:enabled.test1" || "name:enabled.test2")')
  61. def testTastControlWithTastExpr(self):
  62. file_mock = mock.mock_open(read_data=TAST_CONTROL)
  63. args = REQUIRED_ARGS + [
  64. '--tast-expr',
  65. TAST_EXPR,
  66. '--tast-control',
  67. 'mocked_input',
  68. ]
  69. with mock.patch('sys.argv', args),\
  70. mock.patch('builtins.open', file_mock),\
  71. mock.patch('os.chmod'),\
  72. mock.patch("json.dump", mock.MagicMock()) as dump:
  73. generate_skylab_deps.main()
  74. filter_dict = dump.call_args[0][0]
  75. self.assertEqual(filter_dict['default'], '(%s)' % TAST_EXPR)
  76. self.assertEqual(filter_dict['tast_disabled_tests_from_chrome_m100'],
  77. '(%s && !"name:example.m100.test1")' % TAST_EXPR)
  78. def testTastControlWithTastExprAndDisabledTests(self):
  79. file_mock = mock.mock_open(read_data=TAST_CONTROL)
  80. args = REQUIRED_ARGS + [
  81. '--tast-expr', TAST_EXPR, '--tast-control', 'mocked_input',
  82. '--disabled-tests', 'disabled.test1', '--disabled-tests',
  83. 'disabled.test2'
  84. ]
  85. with mock.patch('sys.argv', args),\
  86. mock.patch('builtins.open', file_mock),\
  87. mock.patch('os.chmod'),\
  88. mock.patch("json.dump", mock.MagicMock()) as dump:
  89. generate_skylab_deps.main()
  90. filter_dict = dump.call_args[0][0]
  91. self.assertEqual(
  92. filter_dict['default'],
  93. '("group:mainline" && "dep:chrome" && !informational && !'\
  94. '"name:disabled.test1" && !"name:disabled.test2")'
  95. )
  96. # The list from a set is indeterminent
  97. self.assertIn('"group:mainline" && "dep:chrome" && !informational',
  98. filter_dict['tast_disabled_tests_from_chrome_m100'])
  99. self.assertIn('&& !"name:disabled.test1"',
  100. filter_dict['tast_disabled_tests_from_chrome_m100'])
  101. self.assertIn('&& !"name:disabled.test2"',
  102. filter_dict['tast_disabled_tests_from_chrome_m100'])
  103. self.assertIn('&& !"name:example.m100.test1"',
  104. filter_dict['tast_disabled_tests_from_chrome_m100'])
  105. def testTastControlWithTastExprAndEnabledTests(self):
  106. file_mock = mock.mock_open(read_data=TAST_CONTROL)
  107. args = REQUIRED_ARGS + [
  108. '--tast-expr', TAST_EXPR, '--tast-control', 'mocked_input',
  109. '--enabled-tests', 'enabled.test1', '--enabled-tests', 'enabled.test2'
  110. ]
  111. with mock.patch('sys.argv', args),\
  112. mock.patch('builtins.open', file_mock),\
  113. mock.patch('os.chmod'),\
  114. mock.patch("json.dump", mock.MagicMock()) as dump:
  115. generate_skylab_deps.main()
  116. filter_dict = dump.call_args[0][0]
  117. self.assertEqual(
  118. filter_dict['default'],
  119. '("group:mainline" && "dep:chrome" && !informational && '\
  120. '("name:enabled.test1" || "name:enabled.test2"))'
  121. )
  122. self.assertEqual(
  123. filter_dict['tast_disabled_tests_from_chrome_m100'],
  124. '("group:mainline" && "dep:chrome" && !informational && '\
  125. '!"name:example.m100.test1" && ("name:enabled.test1" '\
  126. '|| "name:enabled.test2"))'
  127. )
  128. def testTastControlWithEnabledTests(self):
  129. file_mock = mock.mock_open(read_data=TAST_CONTROL)
  130. args = REQUIRED_ARGS + [
  131. '--tast-control',
  132. 'mocked_input',
  133. '--enabled-tests',
  134. 'enabled.test1',
  135. '--enabled-tests',
  136. 'enabled.test2',
  137. ]
  138. with mock.patch('sys.argv', args),\
  139. mock.patch('builtins.open', file_mock),\
  140. mock.patch('os.chmod'),\
  141. mock.patch("json.dump", mock.MagicMock()) as dump:
  142. generate_skylab_deps.main()
  143. filter_dict = dump.call_args[0][0]
  144. # Should not include 'all' collection from TAST_CONTROL since that would
  145. # need to be passed in the --disabled-tests to be included
  146. self.assertEqual(filter_dict['default'],
  147. '("name:enabled.test1" || "name:enabled.test2")')
  148. self.assertEqual(
  149. filter_dict['tast_disabled_tests_from_chrome_m100'],
  150. '(!"name:example.m100.test1" && '\
  151. '("name:enabled.test1" || "name:enabled.test2"))'
  152. )
  153. if __name__ == '__main__':
  154. unittest.main()