| 1 | from contextlib import redirect_stdout |
| 2 | from io import StringIO |
| 3 | import json |
| 4 | from pathlib import Path |
| 5 | import tempfile |
| 6 | import subprocess |
| 7 | import unittest |
| 8 | from unittest.mock import patch |
| 9 | |
| 10 | import run_rust |
| 11 | |
| 12 | |
| 13 | class RunnerTests(unittest.TestCase): |
| 14 | def test_all_manifests_have_locks(self): |
| 15 | paths = run_rust.manifests(run_rust.ROOT) |
| 16 | expected = set(run_rust.ROOT.glob("experiments/*/Cargo.toml")) |
| 17 | expected |= set(run_rust.ROOT.glob("tests/*/Cargo.toml")) |
| 18 | self.assertEqual(set(paths), expected) |
| 19 | |
| 20 | def test_failure_does_not_hide_later_crates(self): |
| 21 | paths = [ |
| 22 | Path("/repo/experiments/a/Cargo.toml"), |
| 23 | Path("/repo/experiments/b/Cargo.toml"), |
| 24 | ] |
| 25 | with ( |
| 26 | patch.object(run_rust, "manifests", return_value=paths), |
| 27 | patch.object(run_rust.subprocess, "run", side_effect=[ |
| 28 | subprocess.CompletedProcess([], 7), |
| 29 | subprocess.CompletedProcess([], 0), |
| 30 | ]), |
| 31 | ): |
| 32 | results = run_rust.run(Path("/repo"), "build") |
| 33 | self.assertEqual([row["status"] for row in results], ["failed", "passed"]) |
| 34 | self.assertEqual(results[0]["exit_code"], 7) |
| 35 | |
| 36 | def test_windows_exclusion_is_explicit_and_only_for_linux_tests(self): |
| 37 | paths = [ |
| 38 | Path("/repo/tests/rust-file-publication/Cargo.toml"), |
| 39 | Path("/repo/experiments/rust-file-publication/Cargo.toml"), |
| 40 | ] |
| 41 | with ( |
| 42 | patch.object(run_rust, "manifests", return_value=paths), |
| 43 | patch.object(run_rust.sys, "platform", "win32"), |
| 44 | patch.object( |
| 45 | run_rust.subprocess, "run", |
| 46 | return_value=subprocess.CompletedProcess([], 0), |
| 47 | ) as execute, |
| 48 | ): |
| 49 | results = run_rust.run(Path("/repo"), "test") |
| 50 | self.assertEqual( |
| 51 | [row["status"] for row in results], ["not-applicable", "passed"] |
| 52 | ) |
| 53 | self.assertEqual(execute.call_count, 1) |
| 54 | |
| 55 | def test_fixture_bytes_match_git_blobs(self): |
| 56 | names = subprocess.check_output( |
| 57 | ["git", "ls-files", "-z", "tests/**/fixtures/*"], cwd=run_rust.ROOT |
| 58 | ).decode("utf-8").split("\0") |
| 59 | names = [name for name in names if name] |
| 60 | self.assertTrue(names, "검증할 fixture가 없음") |
| 61 | for name in names: |
| 62 | with self.subTest(path=name): |
| 63 | expected = subprocess.check_output( |
| 64 | ["git", "show", f"HEAD:{name}"], cwd=run_rust.ROOT |
| 65 | ) |
| 66 | self.assertEqual((run_rust.ROOT / name).read_bytes(), expected) |
| 67 | |
| 68 | def test_cross_build_never_executes_tests(self): |
| 69 | args = run_rust.command(Path("tests/example/Cargo.toml"), "cross-build") |
| 70 | self.assertEqual(args[2], "build") |
| 71 | self.assertIn(run_rust.WINDOWS_TARGET, args) |
| 72 | self.assertNotIn("--test-threads=1", args) |
| 73 | |
| 74 | def test_preflight_failure_is_reported_without_hiding_other_phase(self): |
| 75 | with ( |
| 76 | patch.object(run_rust, "prepare_cross_build", side_effect=ValueError("missing linker")), |
| 77 | patch.object(run_rust, "run", return_value=[{"manifest": "sample", "status": "passed"}]), |
| 78 | ): |
| 79 | results = run_rust.execute_phases(Path("/repo"), ["build", "cross-build"]) |
| 80 | self.assertEqual([row["status"] for row in results], ["passed", "failed"]) |
| 81 | self.assertEqual(results[1]["reason"], "missing linker") |
| 82 | |
| 83 | def test_self_check_failure_replaces_previous_success_report(self): |
| 84 | for phase, platform in (("local", "linux"), ("test", "win32")): |
| 85 | with self.subTest(phase=phase), tempfile.TemporaryDirectory( |
| 86 | dir=run_rust.ROOT / "tests/ci" |
| 87 | ) as directory: |
| 88 | root = Path(directory) |
| 89 | (root / "tests/ci").mkdir(parents=True) |
| 90 | report_path = root / f"tests/ci/{phase}-results.json" |
| 91 | with ( |
| 92 | patch.object(run_rust, "ROOT", root), |
| 93 | patch.object(run_rust.sys, "argv", ["run_rust.py", phase]), |
| 94 | patch.object(run_rust.sys, "platform", platform), |
| 95 | patch.dict(run_rust.os.environ, {"GITHUB_STEP_SUMMARY": ""}), |
| 96 | patch.object(run_rust.subprocess, "run", side_effect=[ |
| 97 | subprocess.CompletedProcess([], 0), |
| 98 | subprocess.CompletedProcess([], 7), |
| 99 | ]), |
| 100 | patch.object(run_rust.subprocess, "check_output", side_effect=[ |
| 101 | "successful-head\n", "failed-head\n", |
| 102 | ]), |
| 103 | patch.object(run_rust, "execute_phases", return_value=[ |
| 104 | {"phase": "test", "manifest": "sample", "status": "passed"}, |
| 105 | ]) as execute, |
| 106 | redirect_stdout(StringIO()), |
| 107 | ): |
| 108 | self.assertEqual(run_rust.main(), 0) |
| 109 | successful = json.loads(report_path.read_text(encoding="utf-8")) |
| 110 | self.assertEqual(successful["results"][0]["status"], "passed") |
| 111 | self.assertEqual(run_rust.main(), 7) |
| 112 | failed = json.loads(report_path.read_text(encoding="utf-8")) |
| 113 | self.assertEqual(failed["head"], "failed-head") |
| 114 | self.assertEqual(failed["results"], [{ |
| 115 | "phase": "self-check", |
| 116 | "manifest": "tests/ci", |
| 117 | "status": "failed", |
| 118 | "exit_code": 7, |
| 119 | }]) |
| 120 | self.assertFalse(failed["windows_runtime_tested"]) |
| 121 | execute.assert_called_once() |
| 122 | |
| 123 | def test_cargo_contract(self): |
| 124 | args = run_rust.command(Path("tests/example/Cargo.toml"), "test") |
| 125 | self.assertIn("--locked", args) |
| 126 | self.assertIn("--all-features", args) |
| 127 | self.assertEqual(args[-2:], ["--", "--test-threads=1"]) |
| 128 | |
| 129 | |
| 130 | if __name__ == "__main__": |
| 131 | unittest.main() |