24
24
import stat
25
25
import string
26
26
import subprocess
27
+ import sys
27
28
import tempfile
28
29
import textwrap
29
30
from typing import Optional
37
38
38
39
class BaseTestCase (absltest .TestCase ):
39
40
40
- def _get_helper_exec_path (self ):
41
- helper = 'absl/testing/tests/absltest_test_helper'
41
+ def _get_helper_exec_path (self , helper_name ):
42
+ helper = 'absl/testing/tests/' + helper_name
42
43
return _bazelize_command .get_executable_path (helper )
43
44
44
- def run_helper (self , test_id , args , env_overrides , expect_success ):
45
+ def run_helper (
46
+ self ,
47
+ test_id ,
48
+ args ,
49
+ env_overrides ,
50
+ expect_success ,
51
+ helper_name = None ,
52
+ ):
45
53
env = absltest_env .inherited_env ()
46
54
for key , value in env_overrides .items ():
47
55
if value is None :
@@ -50,31 +58,48 @@ def run_helper(self, test_id, args, env_overrides, expect_success):
50
58
else :
51
59
env [key ] = value
52
60
53
- command = [self ._get_helper_exec_path (),
54
- '--test_id={}' .format (test_id )] + args
61
+ if helper_name is None :
62
+ helper_name = 'absltest_test_helper'
63
+ command = [self ._get_helper_exec_path (helper_name )]
64
+ if test_id is not None :
65
+ command .append ('--test_id={}' .format (test_id ))
66
+ command .extend (args )
55
67
process = subprocess .Popen (
56
68
command , stdout = subprocess .PIPE , stderr = subprocess .PIPE , env = env ,
57
69
universal_newlines = True )
58
70
stdout , stderr = process .communicate ()
59
71
if expect_success :
60
72
self .assertEqual (
61
- 0 , process .returncode ,
62
- 'Expected success, but failed with '
63
- 'stdout:\n {}\n stderr:\n {}\n ' .format (stdout , stderr ))
73
+ 0 ,
74
+ process .returncode ,
75
+ 'Expected success, but failed with exit code {},'
76
+ ' stdout:\n {}\n stderr:\n {}\n ' .format (
77
+ process .returncode , stdout , stderr
78
+ ),
79
+ )
64
80
else :
65
- self .assertEqual (
66
- 1 , process .returncode ,
81
+ self .assertGreater (
82
+ process .returncode ,
83
+ 0 ,
67
84
'Expected failure, but succeeded with '
68
- 'stdout:\n {}\n stderr:\n {}\n ' .format (stdout , stderr ))
69
- return stdout , stderr
85
+ 'stdout:\n {}\n stderr:\n {}\n ' .format (stdout , stderr ),
86
+ )
87
+ return stdout , stderr , process .returncode
70
88
71
89
72
90
class TestCaseTest (BaseTestCase ):
73
91
longMessage = True
74
92
75
- def run_helper (self , test_id , args , env_overrides , expect_success ):
76
- return super (TestCaseTest , self ).run_helper (test_id , args + ['HelperTest' ],
77
- env_overrides , expect_success )
93
+ def run_helper (
94
+ self , test_id , args , env_overrides , expect_success , helper_name = None
95
+ ):
96
+ return super (TestCaseTest , self ).run_helper (
97
+ test_id ,
98
+ args + ['HelperTest' ],
99
+ env_overrides ,
100
+ expect_success ,
101
+ helper_name ,
102
+ )
78
103
79
104
def test_flags_no_env_var_no_flags (self ):
80
105
self .run_helper (
@@ -190,11 +215,12 @@ def test_xml_output_file_from_flag(self):
190
215
expect_success = True )
191
216
192
217
def test_app_run (self ):
193
- stdout , _ = self .run_helper (
218
+ stdout , _ , _ = self .run_helper (
194
219
7 ,
195
220
['--name=cat' , '--name=dog' ],
196
221
{'ABSLTEST_TEST_HELPER_USE_APP_RUN' : '1' },
197
- expect_success = True )
222
+ expect_success = True ,
223
+ )
198
224
self .assertIn ('Names in main() are: cat dog' , stdout )
199
225
self .assertIn ('Names in test_name_flag() are: cat dog' , stdout )
200
226
@@ -226,7 +252,7 @@ def test_expected_failure_if(self):
226
252
self .assertEqual (1 , 2 ) # the expected failure
227
253
228
254
def test_expected_failure_success (self ):
229
- _ , stderr = self .run_helper (5 , ['--' , '-v' ], {}, expect_success = False )
255
+ _ , stderr , _ = self .run_helper (5 , ['--' , '-v' ], {}, expect_success = False )
230
256
self .assertRegex (stderr , r'FAILED \(.*unexpected successes=1\)' )
231
257
232
258
def test_assert_equal (self ):
@@ -2099,8 +2125,9 @@ def run_tempfile_helper(self, cleanup, expected_paths):
2099
2125
'ABSLTEST_TEST_HELPER_TEMPFILE_CLEANUP' : cleanup ,
2100
2126
'TEST_TMPDIR' : tmpdir .full_path ,
2101
2127
}
2102
- stdout , stderr = self .run_helper (0 , ['TempFileHelperTest' ], env ,
2103
- expect_success = False )
2128
+ stdout , stderr , _ = self .run_helper (
2129
+ 0 , ['TempFileHelperTest' ], env , expect_success = False
2130
+ )
2104
2131
output = ('\n === Helper output ===\n '
2105
2132
'----- stdout -----\n {}\n '
2106
2133
'----- end stdout -----\n '
@@ -2473,6 +2500,30 @@ def test_foo(self):
2473
2500
self .assertEmpty (res .errors )
2474
2501
2475
2502
2503
+ class ExitCodeTest (BaseTestCase ):
2504
+
2505
+ def test_exits_5_when_no_tests (self ):
2506
+ expect_success = sys .version_info < (3 , 12 )
2507
+ _ , _ , exit_code = self .run_helper (
2508
+ None ,
2509
+ [],
2510
+ {},
2511
+ expect_success = expect_success ,
2512
+ helper_name = 'absltest_test_helper_skipped' ,
2513
+ )
2514
+ if not expect_success :
2515
+ self .assertEqual (exit_code , 5 )
2516
+
2517
+ def test_exits_5_when_all_skipped (self ):
2518
+ self .run_helper (
2519
+ None ,
2520
+ [],
2521
+ {'ABSLTEST_TEST_HELPER_DEFINE_CLASS' : '1' },
2522
+ expect_success = True ,
2523
+ helper_name = 'absltest_test_helper_skipped' ,
2524
+ )
2525
+
2526
+
2476
2527
def _listdir_recursive (path ):
2477
2528
for dirname , _ , filenames in os .walk (path ):
2478
2529
yield dirname
0 commit comments