File: rust_main_program_unittests.py

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (134 lines) | stat: -rwxr-xr-x 4,809 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env vpython3

# Copyright 2021 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import os
import tempfile
import unittest

# vpython-provided modules.
from pyfakefs import fake_filesystem_unittest  # pylint: disable=import-error

from test_results import TestResult

from rust_main_program import _format_test_name
from rust_main_program import _parse_test_name
from rust_main_program import _get_exe_specific_tests
from rust_main_program import _scrape_test_list
from rust_main_program import _scrape_test_results
from rust_main_program import _parse_args
from rust_main_program import _TestExecutableWrapper

# Protected access is allowed for unittests.
# pylint: disable=protected-access

class Tests(fake_filesystem_unittest.TestCase):
    def test_format_test_name(self):
        self.assertEqual('test_exe//test_bar',
                         _format_test_name('test_exe', 'test_bar'))
        self.assertEqual('test_exe//foo/test_foo',
                         _format_test_name('test_exe', 'foo::test_foo'))

    def test_parse_test_name(self):
        self.assertEqual(('test_exe', 'test_bar'),
                         _parse_test_name('test_exe//test_bar'))
        self.assertEqual(('test_exe', 'foo::test_foo'),
                         _parse_test_name('test_exe//foo/test_foo'))

    def test_scrape_test_list(self):
        test_input = """
test_foo: test
test_bar: test
foo::test_in_mod: test
test_benchmark: benchmark
        """.strip()
        actual_results = _scrape_test_list(test_input, 'test_exe_name')
        expected_results = [
            'test_exe_name//test_foo', 'test_exe_name//test_bar',
            'test_exe_name//foo/test_in_mod'
        ]
        self.assertEqual(actual_results, expected_results)

    # https://crbug.com/1281664 meant that Rust executables might
    # incorrectly think that they were invoked with no cmdline args.
    # Back then we didn't realize that out test wrappers broken :-(.
    # The test below tries to ensure this won't happen again.
    def test_scrape_test_list_with_unexpected_lines(self):
        test_input = """
running 1 test
test test_hello ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; \
finished in 0.00s
        """.strip()
        with self.assertRaises(ValueError):
            _scrape_test_list(test_input, 'test_exe_name')

    def test_scrape_test_results(self):
        test_input = """
running 3 tests
test test_foo ... ok
test test_bar ... ok
test foo::test_in_mod ... ok
test test_foobar ... FAILED

failures:

---- test_foobar stdout ----
thread 'test_foobar' panicked at 'assertion failed: `(left == right)`
  left: `7`,
 right: `124`', ../../build/rust/tests/test_rust_static_library/src/lib.rs:29:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


failures:
    test_foobar

test result: FAILED. 3 passed; 1 failed; 0 ignored; 0 measured; \
0 filtered out; finished in 0.00s
        """.strip()
        list_of_expected_test_names = [
            'test_foo', 'test_bar', 'foo::test_in_mod', 'test_foobar'
        ]
        actual_results = _scrape_test_results(test_input, 'test_exe_name',
                                              list_of_expected_test_names)
        expected_results = [
            TestResult('test_exe_name//test_foo', 'PASS'),
            TestResult('test_exe_name//test_bar', 'PASS'),
            TestResult('test_exe_name//foo/test_in_mod', 'PASS'),
            TestResult('test_exe_name//test_foobar', 'FAIL')
        ]
        self.assertEqual(actual_results, expected_results)

    def test_parse_args(self):
        args = _parse_args(['--rust-test-executable=foo'])
        self.assertEqual(['foo'], args.rust_test_executables)

        args = _parse_args(
            ['--rust-test-executable=foo', '--rust-test-executable=bar'])
        self.assertEqual(['foo', 'bar'], args.rust_test_executables)

    def test_get_exe_specific_tests(self):
        result = _get_exe_specific_tests(
            'exe_name',
            ['exe_name//foo1', 'exe_name//foo2', 'other_exe//foo3'])
        self.assertEqual(['foo1', 'foo2'], result)

    def test_executable_wrapper_basic_construction(self):
        with tempfile.TemporaryDirectory() as tmpdirname:
            exe_filename = 'foo-bar.exe'
            exe_path = os.path.join(tmpdirname, exe_filename)
            with open(exe_path, 'w'):
                pass
            t = _TestExecutableWrapper(exe_path)
            self.assertEqual('foo-bar', t._name_of_test_executable)

    def test_executable_wrapper_missing_file(self):
        with self.assertRaises(ValueError):
            _TestExecutableWrapper('no-such-file.exe')


if __name__ == '__main__':
    unittest.main()