|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Focused regression tests for the cheap Clang production warning gate.""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import json |
| 7 | +import os |
| 8 | +from pathlib import Path |
| 9 | +import stat |
| 10 | +import subprocess |
| 11 | +import tempfile |
| 12 | +import textwrap |
| 13 | +import unittest |
| 14 | + |
| 15 | + |
| 16 | +REPOSITORY = Path(__file__).resolve().parents[1] |
| 17 | +SCRIPT = REPOSITORY / "scripts" / "check_clang_production_build.sh" |
| 18 | +LOCAL_CI = REPOSITORY / "scripts" / "local_ci_check.sh" |
| 19 | +WORKFLOW = REPOSITORY / ".github" / "workflows" / "components.yml" |
| 20 | +COMMON_CMAKE = REPOSITORY / "cmake" / "SharpRuntimeCommon.cmake" |
| 21 | + |
| 22 | + |
| 23 | +class ClangProductionBuildGateTests(unittest.TestCase): |
| 24 | + def setUp(self) -> None: |
| 25 | + self.temporary = tempfile.TemporaryDirectory() |
| 26 | + self.root = Path(self.temporary.name) |
| 27 | + self.bin = self.root / "bin" |
| 28 | + self.bin.mkdir() |
| 29 | + self.calls = self.root / "cmake-calls.jsonl" |
| 30 | + |
| 31 | + self.write_executable( |
| 32 | + "clang", |
| 33 | + """ |
| 34 | + #!/usr/bin/env bash |
| 35 | + if [ "${1:-}" = "--version" ]; then |
| 36 | + echo "mock clang version 1.0" |
| 37 | + exit 0 |
| 38 | + fi |
| 39 | + exit 0 |
| 40 | + """, |
| 41 | + ) |
| 42 | + self.write_executable( |
| 43 | + "clang++", |
| 44 | + """ |
| 45 | + #!/usr/bin/env bash |
| 46 | + if [ "${1:-}" = "--version" ]; then |
| 47 | + echo "mock clang++ version 1.0" |
| 48 | + exit 0 |
| 49 | + fi |
| 50 | + exit 0 |
| 51 | + """, |
| 52 | + ) |
| 53 | + self.write_executable( |
| 54 | + "cmake", |
| 55 | + """ |
| 56 | + #!/usr/bin/env python3 |
| 57 | + import json |
| 58 | + import os |
| 59 | + from pathlib import Path |
| 60 | + import sys |
| 61 | +
|
| 62 | + args = sys.argv[1:] |
| 63 | + with Path(os.environ["FAKE_CMAKE_CALLS"]).open("a", encoding="utf-8") as stream: |
| 64 | + stream.write(json.dumps(args) + "\\n") |
| 65 | +
|
| 66 | + mode = os.environ.get("FAKE_CMAKE_MODE", "ok") |
| 67 | + if "--build" in args: |
| 68 | + if mode == "build-warning": |
| 69 | + print("mock.cpp:1: warning: warning that escaped -Werror") |
| 70 | + if mode == "build-error": |
| 71 | + print("mock.cpp:1: error: compile failed") |
| 72 | + raise SystemExit(1) |
| 73 | + raise SystemExit(0) |
| 74 | +
|
| 75 | + build_dir = Path(args[args.index("-B") + 1]) |
| 76 | + build_dir.mkdir(parents=True, exist_ok=True) |
| 77 | + if mode == "no-production-commands": |
| 78 | + commands = [] |
| 79 | + else: |
| 80 | + warning_flag = "" if mode == "missing-werror" else " -Werror" |
| 81 | + commands = [{ |
| 82 | + "directory": str(build_dir), |
| 83 | + "command": ( |
| 84 | + "mock-clang++ -Wall -Wextra" + warning_flag |
| 85 | + + " -c /repo/modules/core/src/System/Mock.cpp" |
| 86 | + ), |
| 87 | + "file": "/repo/modules/core/src/System/Mock.cpp", |
| 88 | + }] |
| 89 | + (build_dir / "compile_commands.json").write_text( |
| 90 | + json.dumps(commands, indent=2), encoding="utf-8" |
| 91 | + ) |
| 92 | + """, |
| 93 | + ) |
| 94 | + |
| 95 | + def tearDown(self) -> None: |
| 96 | + self.temporary.cleanup() |
| 97 | + |
| 98 | + def write_executable(self, name: str, body: str) -> None: |
| 99 | + path = self.bin / name |
| 100 | + path.write_text(textwrap.dedent(body).lstrip(), encoding="utf-8") |
| 101 | + path.chmod(path.stat().st_mode | stat.S_IXUSR) |
| 102 | + |
| 103 | + def run_gate( |
| 104 | + self, |
| 105 | + *, |
| 106 | + mode: str = "ok", |
| 107 | + jobs: str = "2", |
| 108 | + extra_environment: dict[str, str] | None = None, |
| 109 | + ) -> subprocess.CompletedProcess[str]: |
| 110 | + environment = os.environ.copy() |
| 111 | + environment.update( |
| 112 | + { |
| 113 | + "PATH": f"{self.bin}{os.pathsep}{environment['PATH']}", |
| 114 | + "FAKE_CMAKE_CALLS": str(self.calls), |
| 115 | + "FAKE_CMAKE_MODE": mode, |
| 116 | + "SHARP_RUNTIME_BUILD_JOBS": jobs, |
| 117 | + } |
| 118 | + ) |
| 119 | + if extra_environment: |
| 120 | + environment.update(extra_environment) |
| 121 | + return subprocess.run( |
| 122 | + [str(SCRIPT)], |
| 123 | + cwd=REPOSITORY, |
| 124 | + env=environment, |
| 125 | + capture_output=True, |
| 126 | + text=True, |
| 127 | + check=False, |
| 128 | + ) |
| 129 | + |
| 130 | + def read_calls(self) -> list[list[str]]: |
| 131 | + if not self.calls.exists(): |
| 132 | + return [] |
| 133 | + return [json.loads(line) for line in self.calls.read_text(encoding="utf-8").splitlines()] |
| 134 | + |
| 135 | + def test_gate_configures_every_production_component_with_clang_and_no_tests(self) -> None: |
| 136 | + completed = self.run_gate() |
| 137 | + self.assertEqual(completed.returncode, 0, completed.stdout + completed.stderr) |
| 138 | + calls = self.read_calls() |
| 139 | + self.assertEqual(len(calls), 2) |
| 140 | + configure, build = calls |
| 141 | + |
| 142 | + self.assertIn("-DSHARP_RUNTIME_COMPONENTS=All", configure) |
| 143 | + self.assertIn("-DSHARP_RUNTIME_BUILD_TESTS=OFF", configure) |
| 144 | + self.assertIn("-DSHARP_RUNTIME_BUILD_BENCHMARKS=OFF", configure) |
| 145 | + self.assertIn(f"-DCMAKE_C_COMPILER={self.bin / 'clang'}", configure) |
| 146 | + self.assertIn(f"-DCMAKE_CXX_COMPILER={self.bin / 'clang++'}", configure) |
| 147 | + self.assertIn("-DCMAKE_C_COMPILER_LAUNCHER=", configure) |
| 148 | + self.assertIn("-DCMAKE_CXX_COMPILER_LAUNCHER=", configure) |
| 149 | + |
| 150 | + build_dir = Path(configure[configure.index("-B") + 1]) |
| 151 | + self.assertTrue(build_dir.is_relative_to(REPOSITORY / "build-tmp")) |
| 152 | + self.assertFalse(build_dir.exists(), "the temporary Clang tree was not removed") |
| 153 | + self.assertEqual(build[-2:], ["--parallel", "2"]) |
| 154 | + self.assertIn("1 translation unit(s), 0 warnings, 0 errors", completed.stdout) |
| 155 | + |
| 156 | + def test_gate_rejects_a_production_command_without_werror(self) -> None: |
| 157 | + completed = self.run_gate(mode="missing-werror") |
| 158 | + self.assertNotEqual(completed.returncode, 0) |
| 159 | + self.assertIn("does not carry -Werror", completed.stderr) |
| 160 | + self.assertEqual(len(self.read_calls()), 1, "build must not start after policy drift") |
| 161 | + |
| 162 | + def test_gate_rejects_a_warning_even_if_the_build_command_exits_zero(self) -> None: |
| 163 | + completed = self.run_gate(mode="build-warning") |
| 164 | + self.assertNotEqual(completed.returncode, 0) |
| 165 | + self.assertIn("produced 1 warning(s) and 0 error(s)", completed.stderr) |
| 166 | + |
| 167 | + def test_gate_rejects_more_than_two_jobs_before_configuring(self) -> None: |
| 168 | + completed = self.run_gate(jobs="3") |
| 169 | + self.assertEqual(completed.returncode, 2) |
| 170 | + self.assertIn("aggregate compilation ceiling is two jobs", completed.stderr) |
| 171 | + self.assertEqual(self.read_calls(), []) |
| 172 | + |
| 173 | + def test_gate_rejects_a_missing_explicit_clang_compiler(self) -> None: |
| 174 | + completed = self.run_gate( |
| 175 | + extra_environment={"SHARP_RUNTIME_CLANG_CXX_COMPILER": "missing-clang++"} |
| 176 | + ) |
| 177 | + self.assertEqual(completed.returncode, 2) |
| 178 | + self.assertIn("was not found", completed.stderr) |
| 179 | + self.assertEqual(self.read_calls(), []) |
| 180 | + |
| 181 | + def test_warning_policy_and_local_ci_workflow_wiring_are_pinned(self) -> None: |
| 182 | + common = COMMON_CMAKE.read_text(encoding="utf-8") |
| 183 | + local_ci = LOCAL_CI.read_text(encoding="utf-8") |
| 184 | + workflow = WORKFLOW.read_text(encoding="utf-8") |
| 185 | + |
| 186 | + self.assertIn( |
| 187 | + 'target_compile_options("${target}" PRIVATE -Wall -Wextra -Werror)', common |
| 188 | + ) |
| 189 | + self.assertIn("python3 test/check_clang_production_build_test.py", local_ci) |
| 190 | + self.assertIn("scripts/check_clang_production_build.sh", local_ci) |
| 191 | + self.assertIn("scripts/local_ci_check.sh build", workflow) |
| 192 | + |
| 193 | + |
| 194 | +if __name__ == "__main__": |
| 195 | + unittest.main() |
0 commit comments