xref: /llvm-project/llvm/utils/lit/tests/Inputs/googletest-no-sharding/DummySubDir/OneTest.py (revision 6eee238975e435ad3e5aa7f2b1817ca756911e04)
1#!/usr/bin/env python
2
3import os
4import sys
5
6if len(sys.argv) == 3 and sys.argv[1] == "--gtest_list_tests":
7    if sys.argv[2] != "--gtest_filter=-*DISABLED_*":
8        raise ValueError(f"unexpected argument: {sys.argv[2]}")
9    print(
10        """\
11FirstTest.
12  subTestA
13  subTestB
14  subTestC
15  subTestD
16ParameterizedTest/0.
17  subTest
18ParameterizedTest/1.
19  subTest"""
20    )
21    sys.exit(0)
22elif len(sys.argv) != 1:
23    # sharding and json output are specified using environment variables
24    raise ValueError(f"unexpected argument: {' '.join(sys.argv[1:])!r}")
25
26for e in ["GTEST_OUTPUT"]:
27    if e not in os.environ:
28        raise ValueError(f"missing environment variables: {e}")
29
30if not os.environ["GTEST_OUTPUT"].startswith("json:"):
31    raise ValueError(f"must emit json output: {os.environ['GTEST_OUTPUT']}")
32
33output = """\
34{
35"random_seed": 123,
36"testsuites": [
37    {
38        "name": "FirstTest",
39        "testsuite": [
40            {
41                "name": "subTestA",
42                "result": "COMPLETED",
43                "time": "0.001s"
44            },
45            {
46                "name": "subTestB",
47                "result": "COMPLETED",
48                "time": "0.001s",
49                "failures": [
50                    {
51                        "failure": "I am subTest B, I FAIL\\nAnd I have two lines of output",
52                        "type": ""
53                    }
54                ]
55            },
56            {
57                "name": "subTestC",
58                "result": "SKIPPED",
59                "time": "0.001s"
60            },
61            {
62                "name": "subTestD",
63                "result": "UNRESOLVED",
64                "time": "0.001s"
65            }
66        ]
67    },
68    {
69        "name": "ParameterizedTest/0",
70        "testsuite": [
71            {
72                "name": "subTest",
73                "result": "COMPLETED",
74                "time": "0.001s"
75            }
76        ]
77    },
78    {
79        "name": "ParameterizedTest/1",
80        "testsuite": [
81            {
82                "name": "subTest",
83                "result": "COMPLETED",
84                "time": "0.001s"
85            }
86        ]
87    }
88]
89}"""
90
91dummy_output = """\
92{
93"testsuites": [
94]
95}"""
96
97json_filename = os.environ["GTEST_OUTPUT"].split(":", 1)[1]
98with open(json_filename, "w", encoding="utf-8") as f:
99    print("[ RUN      ] FirstTest.subTestB", flush=True)
100    print("I am subTest B output", file=sys.stderr, flush=True)
101    print("[  FAILED  ] FirstTest.subTestB (8 ms)", flush=True)
102    f.write(output)
103    exit_code = 1
104
105sys.exit(exit_code)
106