rust_main_program_unittests.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #!/usr/bin/env vpython3
  2. # Copyright 2021 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. import os
  6. from pyfakefs import fake_filesystem_unittest
  7. import tempfile
  8. import unittest
  9. from test_results import TestResult
  10. from rust_main_program import _format_test_name
  11. from rust_main_program import _parse_test_name
  12. from rust_main_program import _get_exe_specific_tests
  13. from rust_main_program import _scrape_test_list
  14. from rust_main_program import _scrape_test_results
  15. from rust_main_program import _parse_args
  16. from rust_main_program import _TestExecutableWrapper
  17. class Tests(fake_filesystem_unittest.TestCase):
  18. def test_format_test_name(self):
  19. self.assertEqual('test_exe//test_bar',
  20. _format_test_name('test_exe', 'test_bar'))
  21. self.assertEqual('test_exe//foo/test_foo',
  22. _format_test_name('test_exe', 'foo::test_foo'))
  23. def test_parse_test_name(self):
  24. self.assertEqual(('test_exe', 'test_bar'),
  25. _parse_test_name('test_exe//test_bar'))
  26. self.assertEqual(('test_exe', 'foo::test_foo'),
  27. _parse_test_name('test_exe//foo/test_foo'))
  28. def test_scrape_test_list(self):
  29. test_input = """
  30. test_foo: test
  31. test_bar: test
  32. foo::test_in_mod: test
  33. test_benchmark: benchmark
  34. """.strip()
  35. actual_results = _scrape_test_list(test_input, 'test_exe_name')
  36. expected_results = [
  37. 'test_exe_name//test_foo', 'test_exe_name//test_bar',
  38. 'test_exe_name//foo/test_in_mod'
  39. ]
  40. self.assertEqual(actual_results, expected_results)
  41. # https://crbug.com/1281664 meant that Rust executables might
  42. # incorrectly think that they were invoked with no cmdline args.
  43. # Back then we didn't realize that out test wrappers broken :-(.
  44. # The test below tries to ensure this won't happen again.
  45. def test_scrape_test_list_with_unexpected_lines(self):
  46. test_input = """
  47. running 1 test
  48. test test_hello ... ok
  49. test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
  50. """.strip()
  51. with self.assertRaises(ValueError):
  52. _scrape_test_list(test_input, 'test_exe_name')
  53. def test_scrape_test_results(self):
  54. test_input = """
  55. running 3 tests
  56. test test_foo ... ok
  57. test test_bar ... ok
  58. test foo::test_in_mod ... ok
  59. test test_foobar ... FAILED
  60. failures:
  61. ---- test_foobar stdout ----
  62. thread 'test_foobar' panicked at 'assertion failed: `(left == right)`
  63. left: `7`,
  64. right: `124`', ../../build/rust/tests/test_rust_static_library/src/lib.rs:29:5
  65. note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
  66. failures:
  67. test_foobar
  68. test result: FAILED. 3 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
  69. """.strip()
  70. list_of_expected_test_names = [
  71. 'test_foo', 'test_bar', 'foo::test_in_mod', 'test_foobar'
  72. ]
  73. actual_results = _scrape_test_results(test_input, 'test_exe_name',
  74. list_of_expected_test_names)
  75. expected_results = [
  76. TestResult('test_exe_name//test_foo', 'PASS'),
  77. TestResult('test_exe_name//test_bar', 'PASS'),
  78. TestResult('test_exe_name//foo/test_in_mod', 'PASS'),
  79. TestResult('test_exe_name//test_foobar', 'FAIL')
  80. ]
  81. self.assertEqual(actual_results, expected_results)
  82. def test_parse_args(self):
  83. args = _parse_args(['--rust-test-executable=foo'])
  84. self.assertEqual(['foo'], args.rust_test_executables)
  85. args = _parse_args(
  86. ['--rust-test-executable=foo', '--rust-test-executable=bar'])
  87. self.assertEqual(['foo', 'bar'], args.rust_test_executables)
  88. def test_get_exe_specific_tests(self):
  89. result = _get_exe_specific_tests(
  90. "exe_name",
  91. ["exe_name//foo1", "exe_name//foo2", "other_exe//foo3"])
  92. self.assertEqual(['foo1', 'foo2'], result)
  93. def test_executable_wrapper_basic_construction(self):
  94. with tempfile.TemporaryDirectory() as tmpdirname:
  95. exe_filename = 'foo-bar.exe'
  96. exe_path = os.path.join(tmpdirname, exe_filename)
  97. with open(exe_path, 'w'):
  98. pass
  99. t = _TestExecutableWrapper(exe_path)
  100. self.assertEqual('foo-bar', t._name_of_test_executable)
  101. def test_executable_wrapper_missing_file(self):
  102. with self.assertRaises(ValueError):
  103. _TestExecutableWrapper('no-such-file.exe')
  104. if __name__ == '__main__':
  105. unittest.main()