File: 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 (99 lines) | stat: -rwxr-xr-x 3,353 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
#!/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 argparse
import json
import os
import tempfile
import unittest

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

from test_results import TestResult

import main_program


class FakeTestExecutableWrapper:
    def __init__(self, hardcoded_test_list, hardcoded_test_results):
        self._hardcoded_test_list = hardcoded_test_list
        self._hardcoded_test_results = hardcoded_test_results

    def list_all_tests(self):
        return self._hardcoded_test_list

    def run_tests(self, list_of_tests_to_run):
        results = []
        for test in self._hardcoded_test_results:
            if test.test_name in list_of_tests_to_run:
                results.append(test)
        return results


class EndToEndTests(fake_filesystem_unittest.TestCase):
    def test_basic_scenario(self):
        with tempfile.TemporaryDirectory() as tmpdirname:
            # Prepare simulated inputs.
            test_list = [
                'test_foo', 'test_bar', 'test_foobar', 'module/test_foo'
            ]
            test_results = [
                TestResult('test_foo', 'PASS'),
                TestResult('test_bar', 'PASS'),
                TestResult('test_foobar', 'FAILED'),
                TestResult('module/test_foo', 'PASS')
            ]
            fake_executable_wrapper = FakeTestExecutableWrapper(
                test_list, test_results)
            parser = argparse.ArgumentParser()
            main_program.add_cmdline_args(parser)
            output_file = os.path.join(tmpdirname, 'test.out')
            args = parser.parse_args(
                args=['--isolated-script-test-output={}'.format(output_file)])
            fake_env = {'GTEST_SHARD_INDEX': 0, 'GTEST_TOTAL_SHARDS': 1}

            # Run code under test.
            main_program.main([fake_executable_wrapper], args, fake_env)

            # Verify results.
            with open(output_file) as f:
                actual_json_output = json.load(f)
                del actual_json_output['seconds_since_epoch']
            # yapf: disable
            expected_json_output = {
                'interrupted': False,
                'path_delimiter': '//',
                #'seconds_since_epoch': 1635974313.8388052,
                'version': 3,
                'tests': {
                    'test_foo': {
                        'expected': 'PASS',
                        'actual': 'PASS'
                    },
                    'test_bar': {
                        'expected': 'PASS',
                        'actual': 'PASS'
                    },
                    'test_foobar': {
                        'expected': 'PASS',
                        'actual': 'FAILED'
                    },
                    'module/test_foo': {
                        'expected': 'PASS',
                        'actual': 'PASS'
                    }},
                'num_failures_by_type': {
                    'PASS': 3,
                    'FAILED': 1
                }
            }
            # yapf: enable
            self.assertEqual(actual_json_output, expected_json_output)


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