diff --git a/mypy/main.py b/mypy/main.py index c83ac180817b..9982d7a6eace 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -1719,6 +1719,9 @@ def maybe_write_junit_xml( options: Options, ) -> None: if options.junit_xml: + if options.bazel: + # Elapsed time would make the output non-reproducible. + td = 0.0 py_version = f"{options.python_version[0]}_{options.python_version[1]}" if options.junit_format == "global": util.write_junit_xml( diff --git a/mypy/test/testutil.py b/mypy/test/testutil.py index a7c3f1c00fee..e8b0257ff2eb 100644 --- a/mypy/test/testutil.py +++ b/mypy/test/testutil.py @@ -1,9 +1,12 @@ from __future__ import annotations import os +import tempfile from unittest import TestCase, mock from mypy.inspections import parse_location +from mypy.main import maybe_write_junit_xml +from mypy.options import Options from mypy.util import _generate_junit_contents, get_terminal_width @@ -109,3 +112,15 @@ def test_serious_error(self) -> None: platform="test-plat", ) assert result == expected + + def test_junit_time_is_zero_in_bazel_mode(self) -> None: + # Output must be reproducible under Bazel, so elapsed time is not included. + with tempfile.TemporaryDirectory() as tmpdir: + options = Options() + options.bazel = True + options.junit_xml = os.path.join(tmpdir, "junit.xml") + maybe_write_junit_xml(1.23, False, [], {}, options) + with open(options.junit_xml) as f: + result = f.read() + assert 'time="0.000"' in result + assert "1.230" not in result diff --git a/mypyc/build.py b/mypyc/build.py index 8c6eabead17c..a459d881939f 100644 --- a/mypyc/build.py +++ b/mypyc/build.py @@ -199,6 +199,9 @@ def fail(message: str) -> NoReturn: def emit_messages(options: Options, messages: list[str], dt: float, serious: bool = False) -> None: # ... you know, just in case. if options.junit_xml: + if options.bazel: + # Elapsed time would make the output non-reproducible. + dt = 0.0 py_version = f"{options.python_version[0]}_{options.python_version[1]}" write_junit_xml( dt, diff --git a/mypyc/test/test_misc.py b/mypyc/test/test_misc.py index 816875fcc23d..9e0426b2caef 100644 --- a/mypyc/test/test_misc.py +++ b/mypyc/test/test_misc.py @@ -4,7 +4,8 @@ import tempfile import unittest -from mypyc.build import get_header_deps, resolve_cfile_deps +from mypy.options import Options +from mypyc.build import emit_messages, get_header_deps, resolve_cfile_deps from mypyc.ir.ops import BasicBlock from mypyc.ir.pprint import format_blocks, generate_names_for_ir from mypyc.irbuild.ll_builder import LowLevelIRBuilder @@ -24,6 +25,18 @@ def test_debug_op(self) -> None: code = format_blocks([block], names, {}) assert code[:-1] == ["L0:", " r0 = 'foo'", " CPyDebug_PrintObject(r0)"] + def test_junit_time_is_zero_in_bazel_mode(self) -> None: + # Output must be reproducible under Bazel, so elapsed time is not included. + with tempfile.TemporaryDirectory() as tmpdir: + options = Options() + options.bazel = True + options.junit_xml = os.path.join(tmpdir, "junit.xml") + emit_messages(options, [], 1.23) + with open(options.junit_xml) as f: + result = f.read() + assert 'time="0.000"' in result + assert "1.230" not in result + class TestHeaderDeps(unittest.TestCase): """