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