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