31
31
32
32
"""Tests for Google Test's --gtest_fail_if_no_test_linked flag."""
33
33
34
+ import os
34
35
from googletest .test import gtest_test_utils
35
36
36
37
# The command line flag for enabling the fail-if-no-test-linked behavior.
37
38
FAIL_IF_NO_TEST_LINKED_FLAG = "gtest_fail_if_no_test_linked"
38
39
40
+ # The environment variable for the test output warnings file.
41
+ TEST_WARNINGS_OUTPUT_FILE = "TEST_WARNINGS_OUTPUT_FILE"
42
+
39
43
40
44
class GTestFailIfNoTestLinkedTest (gtest_test_utils .TestCase ):
41
45
"""Tests the --gtest_fail_if_no_test_linked flag."""
42
46
43
- def Run (self , program_name , flag = None ):
47
+ def Run (self , program_name , flag = None , env = None ):
44
48
"""Run the given program with the given flag.
45
49
46
50
Args:
47
51
program_name: Name of the program to run.
48
52
flag: The command line flag to pass to the program, or None.
53
+ env: Dictionary with environment to pass to the subprocess.
49
54
50
55
Returns:
51
56
True if the program exits with code 0, false otherwise.
@@ -55,59 +60,109 @@ def Run(self, program_name, flag=None):
55
60
args = [exe_path ]
56
61
if flag is not None :
57
62
args += [flag ]
58
- process = gtest_test_utils .Subprocess (args , capture_stderr = False )
63
+ process = gtest_test_utils .Subprocess (args , capture_stderr = False , env = env )
59
64
return process .exited and process .exit_code == 0
60
65
61
66
def testSucceedsIfNoTestLinkedAndFlagNotSpecified (self ):
62
67
"""Tests the behavior of no test linked and flag not specified."""
63
-
64
68
self .assertTrue (
65
69
self .Run ("googletest-fail-if-no-test-linked-test-without-test_" )
66
70
)
67
71
72
+ def testSucceedsIfNoTestLinkedAndFlagNotSpecifiedWithWarningFile (self ):
73
+ """Tests that no test linked results in warning file output."""
74
+
75
+ warning_file = os .path .join (gtest_test_utils .GetTempDir (), "NO_TEST_LINKED" )
76
+ self .assertTrue (
77
+ self .Run (
78
+ "googletest-fail-if-no-test-linked-test-without-test_" ,
79
+ env = {TEST_WARNINGS_OUTPUT_FILE : warning_file },
80
+ )
81
+ )
82
+ warning_file_contents = open (warning_file , "r" ).read ()
83
+ self .assertEqual (
84
+ warning_file_contents ,
85
+ "This test program does NOT link in any test case. Please make sure"
86
+ " this is intended.\n " ,
87
+ )
88
+
68
89
def testFailsIfNoTestLinkedAndFlagSpecified (self ):
69
90
"""Tests the behavior of no test linked and flag specified."""
70
91
92
+ warning_file = os .path .join (
93
+ gtest_test_utils .GetTempDir (), "SHOULD_NOT_EXIST"
94
+ )
71
95
self .assertFalse (
72
96
self .Run (
73
97
"googletest-fail-if-no-test-linked-test-without-test_" ,
74
98
f"--{ FAIL_IF_NO_TEST_LINKED_FLAG } " ,
99
+ env = {TEST_WARNINGS_OUTPUT_FILE : warning_file },
75
100
)
76
101
)
102
+ with self .assertRaises (FileNotFoundError ):
103
+ open (warning_file , "r" )
77
104
78
105
def testSucceedsIfEnabledTestLinkedAndFlagNotSpecified (self ):
79
106
"""Tests the behavior of enabled test linked and flag not specified."""
80
107
108
+ warning_file = os .path .join (
109
+ gtest_test_utils .GetTempDir (), "SHOULD_NOT_EXIST"
110
+ )
81
111
self .assertTrue (
82
- self .Run ("googletest-fail-if-no-test-linked-test-with-enabled-test_" )
112
+ self .Run (
113
+ "googletest-fail-if-no-test-linked-test-with-enabled-test_" ,
114
+ env = {TEST_WARNINGS_OUTPUT_FILE : warning_file },
115
+ )
83
116
)
117
+ with self .assertRaises (FileNotFoundError ):
118
+ open (warning_file , "r" )
84
119
85
120
def testSucceedsIfEnabledTestLinkedAndFlagSpecified (self ):
86
121
"""Tests the behavior of enabled test linked and flag specified."""
87
122
123
+ warning_file = os .path .join (
124
+ gtest_test_utils .GetTempDir (), "SHOULD_NOT_EXIST"
125
+ )
88
126
self .assertTrue (
89
127
self .Run (
90
128
"googletest-fail-if-no-test-linked-test-with-enabled-test_" ,
91
129
f"--{ FAIL_IF_NO_TEST_LINKED_FLAG } " ,
130
+ env = {TEST_WARNINGS_OUTPUT_FILE : warning_file },
92
131
)
93
132
)
133
+ with self .assertRaises (FileNotFoundError ):
134
+ open (warning_file , "r" )
94
135
95
136
def testSucceedsIfDisabledTestLinkedAndFlagNotSpecified (self ):
96
137
"""Tests the behavior of disabled test linked and flag not specified."""
97
138
139
+ warning_file = os .path .join (
140
+ gtest_test_utils .GetTempDir (), "SHOULD_NOT_EXIST"
141
+ )
98
142
self .assertTrue (
99
- self .Run ("googletest-fail-if-no-test-linked-test-with-disabled-test_" )
143
+ self .Run (
144
+ "googletest-fail-if-no-test-linked-test-with-disabled-test_" ,
145
+ env = {TEST_WARNINGS_OUTPUT_FILE : warning_file },
146
+ )
100
147
)
148
+ with self .assertRaises (FileNotFoundError ):
149
+ open (warning_file , "r" )
101
150
102
151
def testSucceedsIfDisabledTestLinkedAndFlagSpecified (self ):
103
152
"""Tests the behavior of disabled test linked and flag specified."""
104
153
154
+ warning_file = os .path .join (
155
+ gtest_test_utils .GetTempDir (), "SHOULD_NOT_EXIST"
156
+ )
105
157
self .assertTrue (
106
158
self .Run (
107
159
"googletest-fail-if-no-test-linked-test-with-disabled-test_" ,
108
160
f"--{ FAIL_IF_NO_TEST_LINKED_FLAG } " ,
161
+ env = {TEST_WARNINGS_OUTPUT_FILE : warning_file },
109
162
)
110
163
)
164
+ with self .assertRaises (FileNotFoundError ):
165
+ open (warning_file , "r" )
111
166
112
167
113
168
if __name__ == "__main__" :
0 commit comments