Skip to content

Commit e506d7d

Browse files
committed
annotate env changed better. pgo runs
1 parent b561060 commit e506d7d

4 files changed

Lines changed: 49 additions & 15 deletions

File tree

‎Lib/test/libregrtest/main.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -541,11 +541,12 @@ def display_summary(self) -> None:
541541

542542
def create_run_tests(self, tests: TestTuple) -> RunTests:
543543
# Annotate test failures in the GitHub Actions job log of the last
544-
# run (the re-run, if any), if it reports failures
544+
# run (the re-run, if any), if it reports failures: -v, -W or --pgo
545545
will_rerun = self.want_rerun and not self.python_cmd
546546
github_annotations = (bool(os.environ.get("GITHUB_STEP_SUMMARY"))
547547
and not will_rerun
548-
and bool(self.verbose or self.output_on_failure))
548+
and bool(self.verbose or self.output_on_failure
549+
or self.pgo))
549550
return RunTests(
550551
tests,
551552
fail_fast=self.fail_fast,

‎Lib/test/libregrtest/run_workers.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -613,8 +613,6 @@ def _process_result(self, item: QueueOutput) -> TestResult:
613613
result = mp_result.result
614614
self.results.accumulate_result(result, self.runtests)
615615
self.display_result(mp_result)
616-
if self.runtests.github_annotations:
617-
result.print_github_annotation(self.runtests)
618616

619617
# Display worker stdout
620618
if not self.runtests.output_on_failure:
@@ -626,6 +624,9 @@ def _process_result(self, item: QueueOutput) -> TestResult:
626624
stdout = mp_result.worker_stdout
627625
if stdout:
628626
print(stdout, flush=True)
627+
# Annotate after the output: env changed warnings, crash traceback
628+
if self.runtests.github_annotations:
629+
result.print_github_annotation(self.runtests)
629630

630631
return result
631632

‎Lib/test/libregrtest/save_env.py‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,18 @@ def restore_os_environ(self, saved_environ):
139139
os.environ = saved_environ[1]
140140
os.environ.clear()
141141
os.environ.update(saved_environ[2])
142+
@staticmethod
143+
def describe_os_environ(original, current):
144+
# Only list the keys: values can be secrets
145+
before, after = original[2], current[2]
146+
changes = (
147+
('added', after.keys() - before.keys()),
148+
('removed', before.keys() - after.keys()),
149+
('changed', {key for key in before.keys() & after.keys()
150+
if before[key] != after[key]}),
151+
)
152+
return '; '.join(f'{change} {", ".join(sorted(keys))}'
153+
for change, keys in changes if keys)
142154

143155
def get_sys_path(self):
144156
return id(sys.path), sys.path, sys.path[:]
@@ -347,7 +359,12 @@ def __exit__(self, exc_type, exc_val, exc_tb):
347359
current = get()
348360
# Check for changes to the resource's value
349361
if current != original:
350-
support.set_environment_altered(f"{name} was modified")
362+
reason = f"{name} was modified"
363+
if name == 'os.environ':
364+
delta = self.describe_os_environ(original, current)
365+
if delta:
366+
reason = f"{reason}: {delta}"
367+
support.set_environment_altered(reason)
351368
restore(original)
352369
if not self.quiet and not self.pgo:
353370
print_warning(

‎Lib/test/test_regrtest.py‎

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
from test.libregrtest import utils
3939
from test.libregrtest.filter import get_match_tests, match_test
4040
from test.libregrtest.result import TestStats
41+
from test.libregrtest.save_env import saved_test_environment
4142
from test.libregrtest.utils import normalize_test_name
4243

4344
if not support.has_subprocess_support:
@@ -1782,7 +1783,7 @@ def path(name):
17821783
'parallel': ['-j2', '-W'],
17831784
# iOS and WASI run tests in the main process: a crash kills it
17841785
'single-process': ['--fast-ci', '--single-process'],
1785-
# Profile task of PGO and BOLT builds: no annotations
1786+
# Profile task of PGO and BOLT builds: failures stop the build
17861787
'pgo': ['--pgo'],
17871788
}
17881789
for name, args in command_lines.items():
@@ -1793,14 +1794,11 @@ def path(name):
17931794
output, summary = self.run_tests_github(
17941795
*args, *tests, exitcode=EXITCODE_BAD_TEST)
17951796

1796-
annotations = []
1797-
if '--pgo' not in args:
1798-
annotations += test_cases
1799-
if '--fast-ci' in args:
1800-
annotations.append((env_changed, path(env_changed),
1801-
None))
1802-
if crash in tests:
1803-
annotations.append((crash, path(crash), None))
1797+
annotations = list(test_cases)
1798+
if '--fast-ci' in args:
1799+
annotations.append((env_changed, path(env_changed), None))
1800+
if crash in tests:
1801+
annotations.append((crash, path(crash), None))
18041802
self.assertCountEqual(self.parse_github_annotations(output),
18051803
annotations, output)
18061804
# Each test case is annotated right after its failure report
@@ -1810,6 +1808,13 @@ def path(name):
18101808
output,
18111809
rf'(?m)^(?:ERROR|FAIL): {title}\n'
18121810
rf'(?:(?!={{70}}$).*\n)*?::error .*title={title}::')
1811+
# The env changed annotation comes right after the warnings
1812+
if '--fast-ci' in args:
1813+
self.assertRegex(
1814+
output,
1815+
rf'(?m)^Warning -- os\.environ was modified by '
1816+
rf'{env_changed}\n(?:Warning -- .*\n)*'
1817+
rf'::error .*title={env_changed}::')
18131818

18141819
# The job summary lists the failed tests in completion order
18151820
summary_lines = summary.splitlines()
@@ -1820,7 +1825,9 @@ def path(name):
18201825
[line for line in summary_lines
18211826
if line.startswith('### ')],
18221827
[test_files[name] for name in tests])
1823-
self.assertIn('- os.environ was modified', summary_lines)
1828+
self.assertIn('- os.environ was modified: '
1829+
'added REGRTEST_GITHUB_ENV_CHANGED',
1830+
summary_lines)
18241831
self.assertCountEqual(
18251832
re.findall(r'<summary>(.*)</summary>', summary),
18261833
[title for title, _, _ in test_cases])
@@ -2950,6 +2957,14 @@ def worker():
29502957

29512958

29522959
class TestUtils(unittest.TestCase):
2960+
def test_describe_os_environ(self):
2961+
describe = saved_test_environment.describe_os_environ
2962+
before = {'A': '1', 'B': '2', 'C': '3'}
2963+
after = {'A': '1', 'B': 'secret', 'D': '4'}
2964+
self.assertEqual(describe((0, None, before), (0, None, after)),
2965+
'added D; removed C; changed B')
2966+
self.assertEqual(describe((0, None, before), (1, None, before)), '')
2967+
29532968
def test_format_duration(self):
29542969
self.assertEqual(utils.format_duration(0),
29552970
'0 ms')

0 commit comments

Comments
 (0)