team-ljyzipelo

공개
226줄10 KB
1from contextlib import redirect_stdout
2from io import StringIO
3import json
4from pathlib import Path
5import tempfile
6import subprocess
7import unittest
8from unittest.mock import patch
9
10import run_rust
11
12
13class 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, "prepare_tools"),
94 patch.object(run_rust, "prepare_local_tools"),
95 patch.object(run_rust.sys, "argv", ["run_rust.py", phase]),
96 patch.object(run_rust.sys, "platform", platform),
97 patch.dict(run_rust.os.environ, {"GITHUB_STEP_SUMMARY": ""}),
98 patch.object(run_rust.subprocess, "run", side_effect=[
99 subprocess.CompletedProcess([], 0),
100 subprocess.CompletedProcess([], 7),
101 ]),
102 patch.object(run_rust.subprocess, "check_output", side_effect=[
103 "successful-head\n", "failed-head\n",
104 ]),
105 patch.object(run_rust, "execute_phases", return_value=[
106 {"phase": "test", "manifest": "sample", "status": "passed"},
107 ]) as execute,
108 redirect_stdout(StringIO()),
109 ):
110 self.assertEqual(run_rust.main(), 0)
111 successful = json.loads(report_path.read_text(encoding="utf-8"))
112 self.assertEqual(successful["results"][0]["status"], "passed")
113 self.assertEqual(run_rust.main(), 7)
114 failed = json.loads(report_path.read_text(encoding="utf-8"))
115 self.assertEqual(failed["head"], "failed-head")
116 self.assertEqual(failed["results"], [{
117 "phase": "self-check",
118 "manifest": "tests/ci",
119 "status": "failed",
120 "exit_code": 7,
121 }])
122 self.assertFalse(failed["windows_runtime_tested"])
123 execute.assert_called_once()
124
125 def test_linked_worktree_finds_setup_tools_without_path_export(self):
126 with tempfile.TemporaryDirectory(dir=run_rust.ROOT / "tests/ci") as directory:
127 base = Path(directory)
128 root = base / "linked"
129 tools = base / "tests/ci/tools"
130 binaries = tools / "mingw/usr/bin"
131 binaries.mkdir(parents=True)
132 for name in ("gcc", "g++"):
133 executable = binaries / f"x86_64-w64-mingw32-{name}-posix"
134 executable.write_text("#!/bin/sh\nexit 0\n")
135 executable.chmod(0o700)
136 rustup = tools / "rustup"
137 (rustup / "toolchains/1.99.1-x86_64-unknown-linux-gnu").mkdir(parents=True)
138 with (
139 patch.dict(run_rust.os.environ, {"PATH": ""}, clear=True),
140 patch.object(run_rust.subprocess, "check_output", return_value=str(base / ".git")),
141 ):
142 run_rust.prepare_tools(root)
143 run_rust.prepare_cross_build()
144 self.assertEqual(run_rust.os.environ["RUSTUP_HOME"], str(rustup))
145 self.assertEqual(
146 run_rust.os.environ["CARGO_TARGET_X86_64_PC_WINDOWS_GNU_LINKER"],
147 str(binaries / "x86_64-w64-mingw32-gcc-posix"),
148 )
149
150 def test_local_bootstraps_missing_cross_tools_and_reloads_paths(self):
151 root = Path("/repo")
152 with (
153 patch.object(run_rust, "prepare_cross_build", side_effect=[ValueError("missing"), None]) as detect,
154 patch.object(run_rust.subprocess, "run") as setup,
155 patch.object(run_rust, "prepare_tools") as reload,
156 ):
157 run_rust.prepare_local_tools(root)
158 setup.assert_called_once_with(
159 ["bash", "/repo/tests/ci/setup_windows.sh"], cwd=root, check=True,
160 )
161 reload.assert_called_once_with(root)
162 self.assertEqual(detect.call_count, 2)
163
164 def test_existing_cross_tools_do_not_trigger_downloads(self):
165 with (
166 patch.object(run_rust, "prepare_cross_build"),
167 patch.object(run_rust.subprocess, "run") as setup,
168 ):
169 run_rust.prepare_local_tools(Path("/repo"))
170 setup.assert_not_called()
171
172 def test_explicit_tools_directory_does_not_fall_back_to_other_worktree(self):
173 with (
174 patch.dict(run_rust.os.environ, {"PATH": "", "ZIPELO_CI_TOOLS": "/missing"}, clear=True),
175 patch.object(run_rust.subprocess, "check_output") as git,
176 ):
177 run_rust.prepare_tools(Path("/repo"))
178 git.assert_not_called()
179 with self.assertRaisesRegex(ValueError, "setup_windows.sh"):
180 run_rust.prepare_cross_build()
181
182 def test_filesystem_skip_is_limited_to_publication_test_crates(self):
183 with tempfile.TemporaryDirectory(dir=run_rust.ROOT / "tests/ci") as directory:
184 root = Path(directory)
185 for name in run_rust.FILE_FIXTURES:
186 (root / "tests" / name).mkdir(parents=True)
187 paths = [
188 root / "tests/rust-file-publication/Cargo.toml",
189 root / "tests/rust-gzip-file-job/Cargo.toml",
190 root / "tests/rust-file-snapshot/Cargo.toml",
191 root / "experiments/rust-file-publication/Cargo.toml",
192 ]
193 with (
194 patch.object(run_rust.sys, "platform", "linux"),
195 patch.object(run_rust, "manifests", return_value=paths),
196 patch.object(run_rust.subprocess, "check_output", return_value="1021994\n"),
197 patch.object(run_rust.subprocess, "run", return_value=subprocess.CompletedProcess([], 0)) as cargo,
198 ):
199 results = run_rust.run(root, "test")
200 self.assertEqual([row["status"] for row in results], [
201 "not-applicable", "not-applicable", "passed", "passed",
202 ])
203 self.assertIn("0x1021994", results[0]["reason"])
204 self.assertEqual(cargo.call_count, 2)
205 cargo.reset_mock()
206 results = run_rust.run(root, "cross-build")
207 self.assertTrue(all(row["status"] == "passed" for row in results))
208 self.assertEqual(cargo.call_count, 4)
209
210 def test_unknown_filesystem_probe_error_is_failure(self):
211 with (
212 patch.object(run_rust, "prepare_fixtures", side_effect=subprocess.CalledProcessError(1, "stat")),
213 patch.object(run_rust.sys, "platform", "linux"),
214 ):
215 results = run_rust.execute_phases(run_rust.ROOT, ["test"])
216 self.assertEqual(results[0]["status"], "failed")
217
218 def test_cargo_contract(self):
219 args = run_rust.command(Path("tests/example/Cargo.toml"), "test")
220 self.assertIn("--locked", args)
221 self.assertIn("--all-features", args)
222 self.assertEqual(args[-2:], ["--", "--test-threads=1"])
223
224
225if __name__ == "__main__":
226 unittest.main()