3131
3232"""Tests for Google Test's --gtest_fail_if_no_test_linked flag."""
3333
34+ import os
3435from googletest .test import gtest_test_utils
3536
3637# The command line flag for enabling the fail-if-no-test-linked behavior.
3738FAIL_IF_NO_TEST_LINKED_FLAG = "gtest_fail_if_no_test_linked"
3839
40+ # The environment variable for the test output warnings file.
41+ TEST_WARNINGS_OUTPUT_FILE = "TEST_WARNINGS_OUTPUT_FILE"
42+
3943
4044class GTestFailIfNoTestLinkedTest (gtest_test_utils .TestCase ):
4145 """Tests the --gtest_fail_if_no_test_linked flag."""
4246
43- def Run (self , program_name , flag = None ):
47+ def Run (self , program_name , flag = None , env = None ):
4448 """Run the given program with the given flag.
4549
4650 Args:
4751 program_name: Name of the program to run.
4852 flag: The command line flag to pass to the program, or None.
53+ env: Dictionary with environment to pass to the subprocess.
4954
5055 Returns:
5156 True if the program exits with code 0, false otherwise.
@@ -55,59 +60,109 @@ def Run(self, program_name, flag=None):
5560 args = [exe_path ]
5661 if flag is not None :
5762 args += [flag ]
58- process = gtest_test_utils .Subprocess (args , capture_stderr = False )
63+ process = gtest_test_utils .Subprocess (args , capture_stderr = False , env = env )
5964 return process .exited and process .exit_code == 0
6065
6166 def testSucceedsIfNoTestLinkedAndFlagNotSpecified (self ):
6267 """Tests the behavior of no test linked and flag not specified."""
63-
6468 self .assertTrue (
6569 self .Run ("googletest-fail-if-no-test-linked-test-without-test_" )
6670 )
6771
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+
6889 def testFailsIfNoTestLinkedAndFlagSpecified (self ):
6990 """Tests the behavior of no test linked and flag specified."""
7091
92+ warning_file = os .path .join (
93+ gtest_test_utils .GetTempDir (), "SHOULD_NOT_EXIST"
94+ )
7195 self .assertFalse (
7296 self .Run (
7397 "googletest-fail-if-no-test-linked-test-without-test_" ,
7498 f"--{ FAIL_IF_NO_TEST_LINKED_FLAG } " ,
99+ env = {TEST_WARNINGS_OUTPUT_FILE : warning_file },
75100 )
76101 )
102+ with self .assertRaises (FileNotFoundError ):
103+ open (warning_file , "r" )
77104
78105 def testSucceedsIfEnabledTestLinkedAndFlagNotSpecified (self ):
79106 """Tests the behavior of enabled test linked and flag not specified."""
80107
108+ warning_file = os .path .join (
109+ gtest_test_utils .GetTempDir (), "SHOULD_NOT_EXIST"
110+ )
81111 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+ )
83116 )
117+ with self .assertRaises (FileNotFoundError ):
118+ open (warning_file , "r" )
84119
85120 def testSucceedsIfEnabledTestLinkedAndFlagSpecified (self ):
86121 """Tests the behavior of enabled test linked and flag specified."""
87122
123+ warning_file = os .path .join (
124+ gtest_test_utils .GetTempDir (), "SHOULD_NOT_EXIST"
125+ )
88126 self .assertTrue (
89127 self .Run (
90128 "googletest-fail-if-no-test-linked-test-with-enabled-test_" ,
91129 f"--{ FAIL_IF_NO_TEST_LINKED_FLAG } " ,
130+ env = {TEST_WARNINGS_OUTPUT_FILE : warning_file },
92131 )
93132 )
133+ with self .assertRaises (FileNotFoundError ):
134+ open (warning_file , "r" )
94135
95136 def testSucceedsIfDisabledTestLinkedAndFlagNotSpecified (self ):
96137 """Tests the behavior of disabled test linked and flag not specified."""
97138
139+ warning_file = os .path .join (
140+ gtest_test_utils .GetTempDir (), "SHOULD_NOT_EXIST"
141+ )
98142 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+ )
100147 )
148+ with self .assertRaises (FileNotFoundError ):
149+ open (warning_file , "r" )
101150
102151 def testSucceedsIfDisabledTestLinkedAndFlagSpecified (self ):
103152 """Tests the behavior of disabled test linked and flag specified."""
104153
154+ warning_file = os .path .join (
155+ gtest_test_utils .GetTempDir (), "SHOULD_NOT_EXIST"
156+ )
105157 self .assertTrue (
106158 self .Run (
107159 "googletest-fail-if-no-test-linked-test-with-disabled-test_" ,
108160 f"--{ FAIL_IF_NO_TEST_LINKED_FLAG } " ,
161+ env = {TEST_WARNINGS_OUTPUT_FILE : warning_file },
109162 )
110163 )
164+ with self .assertRaises (FileNotFoundError ):
165+ open (warning_file , "r" )
111166
112167
113168if __name__ == "__main__" :
0 commit comments