Skip to content

Commit 78af725

Browse files
yileicopybara-github
authored andcommitted
Do not fail on Python 3.12 when there are tests but all skipped.
This follows the same change in Python itself from python/cpython#113661. PiperOrigin-RevId: 597838343
1 parent 2af184b commit 78af725

File tree

5 files changed

+104
-24
lines changed

5 files changed

+104
-24
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com).
2121
* (flags) `absl.flags.argparse_flags.ArgumentParser` now correctly inherits
2222
an empty instance of `FlagValues` to ensure that absl flags, such as
2323
`--flagfile`, `--undefok` are supported.
24+
* (testing) Do not exit 5 if tests were skipped on Python 3.12. This follows
25+
the CPython change in https://github.com/python/cpython/pull/113856.
2426

2527
### Fixed
2628

absl/testing/BUILD

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,10 @@ py_test(
197197
name = "tests/absltest_test",
198198
size = "small",
199199
srcs = ["tests/absltest_test.py"],
200-
data = [":tests/absltest_test_helper"],
200+
data = [
201+
":tests/absltest_test_helper",
202+
":tests/absltest_test_helper_skipped",
203+
],
201204
python_version = "PY3",
202205
srcs_version = "PY3",
203206
deps = [
@@ -221,6 +224,13 @@ py_binary(
221224
],
222225
)
223226

227+
py_binary(
228+
name = "tests/absltest_test_helper_skipped",
229+
testonly = 1,
230+
srcs = ["tests/absltest_test_helper_skipped.py"],
231+
deps = [":absltest"],
232+
)
233+
224234
py_test(
225235
name = "tests/flagsaver_test",
226236
srcs = ["tests/flagsaver_test.py"],

absl/testing/absltest.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2689,9 +2689,9 @@ def run_tests(
26892689
result, fail_when_no_tests_ran = _run_and_get_tests_result(
26902690
argv, args, kwargs, xml_reporter.TextAndXMLTestRunner
26912691
)
2692-
if fail_when_no_tests_ran and result.testsRun == 0:
2693-
# Python 3.12 unittest exits with 5 when no tests ran. The code comes from
2694-
# pytest which does the same thing.
2692+
if fail_when_no_tests_ran and result.testsRun == 0 and not result.skipped:
2693+
# Python 3.12 unittest exits with 5 when no tests ran. The exit code 5 comes
2694+
# from pytest which does the same thing.
26952695
sys.exit(5)
26962696
sys.exit(not result.wasSuccessful())
26972697

absl/testing/tests/absltest_test.py

Lines changed: 71 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import stat
2525
import string
2626
import subprocess
27+
import sys
2728
import tempfile
2829
import textwrap
2930
from typing import Optional
@@ -37,11 +38,18 @@
3738

3839
class BaseTestCase(absltest.TestCase):
3940

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
4243
return _bazelize_command.get_executable_path(helper)
4344

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+
):
4553
env = absltest_env.inherited_env()
4654
for key, value in env_overrides.items():
4755
if value is None:
@@ -50,31 +58,48 @@ def run_helper(self, test_id, args, env_overrides, expect_success):
5058
else:
5159
env[key] = value
5260

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)
5567
process = subprocess.Popen(
5668
command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env,
5769
universal_newlines=True)
5870
stdout, stderr = process.communicate()
5971
if expect_success:
6072
self.assertEqual(
61-
0, process.returncode,
62-
'Expected success, but failed with '
63-
'stdout:\n{}\nstderr:\n{}\n'.format(stdout, stderr))
73+
0,
74+
process.returncode,
75+
'Expected success, but failed with exit code {},'
76+
' stdout:\n{}\nstderr:\n{}\n'.format(
77+
process.returncode, stdout, stderr
78+
),
79+
)
6480
else:
65-
self.assertEqual(
66-
1, process.returncode,
81+
self.assertGreater(
82+
process.returncode,
83+
0,
6784
'Expected failure, but succeeded with '
68-
'stdout:\n{}\nstderr:\n{}\n'.format(stdout, stderr))
69-
return stdout, stderr
85+
'stdout:\n{}\nstderr:\n{}\n'.format(stdout, stderr),
86+
)
87+
return stdout, stderr, process.returncode
7088

7189

7290
class TestCaseTest(BaseTestCase):
7391
longMessage = True
7492

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+
)
78103

79104
def test_flags_no_env_var_no_flags(self):
80105
self.run_helper(
@@ -190,11 +215,12 @@ def test_xml_output_file_from_flag(self):
190215
expect_success=True)
191216

192217
def test_app_run(self):
193-
stdout, _ = self.run_helper(
218+
stdout, _, _ = self.run_helper(
194219
7,
195220
['--name=cat', '--name=dog'],
196221
{'ABSLTEST_TEST_HELPER_USE_APP_RUN': '1'},
197-
expect_success=True)
222+
expect_success=True,
223+
)
198224
self.assertIn('Names in main() are: cat dog', stdout)
199225
self.assertIn('Names in test_name_flag() are: cat dog', stdout)
200226

@@ -226,7 +252,7 @@ def test_expected_failure_if(self):
226252
self.assertEqual(1, 2) # the expected failure
227253

228254
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)
230256
self.assertRegex(stderr, r'FAILED \(.*unexpected successes=1\)')
231257

232258
def test_assert_equal(self):
@@ -2099,8 +2125,9 @@ def run_tempfile_helper(self, cleanup, expected_paths):
20992125
'ABSLTEST_TEST_HELPER_TEMPFILE_CLEANUP': cleanup,
21002126
'TEST_TMPDIR': tmpdir.full_path,
21012127
}
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+
)
21042131
output = ('\n=== Helper output ===\n'
21052132
'----- stdout -----\n{}\n'
21062133
'----- end stdout -----\n'
@@ -2473,6 +2500,30 @@ def test_foo(self):
24732500
self.assertEmpty(res.errors)
24742501

24752502

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+
24762527
def _listdir_recursive(path):
24772528
for dirname, _, filenames in os.walk(path):
24782529
yield dirname
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""Test helper for ExitCodeTest in absltest_test.py."""
2+
3+
import os
4+
from absl.testing import absltest
5+
6+
7+
if os.environ.get("ABSLTEST_TEST_HELPER_DEFINE_CLASS") == "1":
8+
9+
class MyTest(absltest.TestCase):
10+
11+
@absltest.skip("Skipped for testing the exit code behavior")
12+
def test_foo(self):
13+
pass
14+
15+
16+
if __name__ == "__main__":
17+
absltest.main()

0 commit comments

Comments
 (0)