team-ljyzipelo

공개
229줄11 KB
1from contextlib import redirect_stdout
2from io import StringIO
3import json
4from pathlib import Path, PurePosixPath, PureWindowsPath
5import tempfile
6import subprocess
7import sys
8import unittest
9from unittest.mock import patch
10
11import run_rust
12
13
14class RunnerTests(unittest.TestCase):
15 def test_all_manifests_have_locks(self):
16 paths = run_rust.manifests(run_rust.ROOT)
17 expected = set(run_rust.ROOT.glob("experiments/*/Cargo.toml"))
18 expected |= set(run_rust.ROOT.glob("tests/*/Cargo.toml"))
19 self.assertEqual(set(paths), expected)
20
21 def test_failure_does_not_hide_later_crates(self):
22 paths = [
23 Path("/repo/experiments/a/Cargo.toml"),
24 Path("/repo/experiments/b/Cargo.toml"),
25 ]
26 with (
27 patch.object(run_rust, "manifests", return_value=paths),
28 patch.object(run_rust.subprocess, "run", side_effect=[
29 subprocess.CompletedProcess([], 7),
30 subprocess.CompletedProcess([], 0),
31 ]),
32 ):
33 results = run_rust.run(Path("/repo"), "build")
34 self.assertEqual([row["status"] for row in results], ["failed", "passed"])
35 self.assertEqual(results[0]["exit_code"], 7)
36
37 def test_windows_exclusion_is_explicit_and_only_for_linux_tests(self):
38 paths = [
39 Path("/repo/tests/rust-file-publication/Cargo.toml"),
40 Path("/repo/experiments/rust-file-publication/Cargo.toml"),
41 ]
42 with (
43 patch.object(run_rust, "manifests", return_value=paths),
44 patch.object(run_rust.sys, "platform", "win32"),
45 patch.object(
46 run_rust.subprocess, "run",
47 return_value=subprocess.CompletedProcess([], 0),
48 ) as execute,
49 ):
50 results = run_rust.run(Path("/repo"), "test")
51 self.assertEqual(
52 [row["status"] for row in results], ["not-applicable", "passed"]
53 )
54 self.assertEqual(execute.call_count, 1)
55
56 def test_fixture_bytes_match_git_blobs(self):
57 names = subprocess.check_output(
58 ["git", "ls-files", "-z", "tests/**/fixtures/*"], cwd=run_rust.ROOT
59 ).decode("utf-8").split("\0")
60 names = [name for name in names if name]
61 self.assertTrue(names, "검증할 fixture가 없음")
62 for name in names:
63 with self.subTest(path=name):
64 expected = subprocess.check_output(
65 ["git", "show", f"HEAD:{name}"], cwd=run_rust.ROOT
66 )
67 self.assertEqual((run_rust.ROOT / name).read_bytes(), expected)
68
69 def test_cross_build_never_executes_tests(self):
70 args = run_rust.command(Path("tests/example/Cargo.toml"), "cross-build")
71 self.assertEqual(args[2], "build")
72 self.assertIn(run_rust.WINDOWS_TARGET, args)
73 self.assertNotIn("--test-threads=1", args)
74
75 def test_preflight_failure_is_reported_without_hiding_other_phase(self):
76 with (
77 patch.object(run_rust, "prepare_cross_build", side_effect=ValueError("missing linker")),
78 patch.object(run_rust, "run", return_value=[{"manifest": "sample", "status": "passed"}]),
79 ):
80 results = run_rust.execute_phases(Path("/repo"), ["build", "cross-build"])
81 self.assertEqual([row["status"] for row in results], ["passed", "failed"])
82 self.assertEqual(results[1]["reason"], "missing linker")
83
84 def test_self_check_failure_replaces_previous_success_report(self):
85 for phase, platform in (("local", "linux"), ("test", "win32")):
86 with self.subTest(phase=phase), tempfile.TemporaryDirectory(
87 dir=run_rust.ROOT / "tests/ci"
88 ) as directory:
89 root = Path(directory)
90 (root / "tests/ci").mkdir(parents=True)
91 report_path = root / f"tests/ci/{phase}-results.json"
92 with (
93 patch.object(run_rust, "ROOT", root),
94 patch.object(run_rust, "prepare_tools"),
95 patch.object(run_rust, "prepare_local_tools"),
96 patch.object(run_rust.sys, "argv", ["run_rust.py", phase]),
97 patch.object(run_rust.sys, "platform", platform),
98 patch.dict(run_rust.os.environ, {"GITHUB_STEP_SUMMARY": ""}),
99 patch.object(run_rust.subprocess, "run", side_effect=[
100 subprocess.CompletedProcess([], 0),
101 subprocess.CompletedProcess([], 7),
102 ]),
103 patch.object(run_rust.subprocess, "check_output", side_effect=[
104 "successful-head\n", "failed-head\n",
105 ]),
106 patch.object(run_rust, "execute_phases", return_value=[
107 {"phase": "test", "manifest": "sample", "status": "passed"},
108 ]) as execute,
109 redirect_stdout(StringIO()),
110 ):
111 self.assertEqual(run_rust.main(), 0)
112 successful = json.loads(report_path.read_text(encoding="utf-8"))
113 self.assertEqual(successful["results"][0]["status"], "passed")
114 self.assertEqual(run_rust.main(), 7)
115 failed = json.loads(report_path.read_text(encoding="utf-8"))
116 self.assertEqual(failed["head"], "failed-head")
117 self.assertEqual(failed["results"], [{
118 "phase": "self-check",
119 "manifest": "tests/ci",
120 "status": "failed",
121 "exit_code": 7,
122 }])
123 self.assertFalse(failed["windows_runtime_tested"])
124 execute.assert_called_once()
125
126 @unittest.skipUnless(sys.platform == "linux", "Linux MinGW 실행 파일 탐색 시험임")
127 def test_linked_worktree_finds_setup_tools_without_path_export(self):
128 with tempfile.TemporaryDirectory(dir=run_rust.ROOT / "tests/ci") as directory:
129 base = Path(directory)
130 root = base / "linked"
131 tools = base / "tests/ci/tools"
132 binaries = tools / "mingw/usr/bin"
133 binaries.mkdir(parents=True)
134 for name in ("gcc", "g++"):
135 executable = binaries / f"x86_64-w64-mingw32-{name}-posix"
136 executable.write_text("#!/bin/sh\nexit 0\n")
137 executable.chmod(0o700)
138 rustup = tools / "rustup"
139 (rustup / "toolchains/1.99.1-x86_64-unknown-linux-gnu").mkdir(parents=True)
140 with (
141 patch.dict(run_rust.os.environ, {"PATH": ""}, clear=True),
142 patch.object(run_rust.subprocess, "check_output", return_value=str(base / ".git")),
143 ):
144 run_rust.prepare_tools(root)
145 run_rust.prepare_cross_build()
146 self.assertEqual(run_rust.os.environ["RUSTUP_HOME"], str(rustup))
147 self.assertEqual(
148 run_rust.os.environ["CARGO_TARGET_X86_64_PC_WINDOWS_GNU_LINKER"],
149 str(binaries / "x86_64-w64-mingw32-gcc-posix"),
150 )
151
152 def test_local_bootstraps_missing_cross_tools_and_reloads_paths(self):
153 for root in (Path("/repo"), PurePosixPath("/repo"), PureWindowsPath("C:/repo")):
154 with self.subTest(root=root):
155 with (
156 patch.object(run_rust, "prepare_cross_build", side_effect=[ValueError("missing"), None]) as detect,
157 patch.object(run_rust.subprocess, "run") as setup,
158 patch.object(run_rust, "prepare_tools") as reload,
159 ):
160 run_rust.prepare_local_tools(root)
161 setup.assert_called_once_with(
162 ["bash", str(root / "tests/ci/setup_windows.sh")], cwd=root, check=True,
163 )
164 reload.assert_called_once_with(root)
165 self.assertEqual(detect.call_count, 2)
166
167 def test_existing_cross_tools_do_not_trigger_downloads(self):
168 with (
169 patch.object(run_rust, "prepare_cross_build"),
170 patch.object(run_rust.subprocess, "run") as setup,
171 ):
172 run_rust.prepare_local_tools(Path("/repo"))
173 setup.assert_not_called()
174
175 def test_explicit_tools_directory_does_not_fall_back_to_other_worktree(self):
176 with (
177 patch.dict(run_rust.os.environ, {"PATH": "", "ZIPELO_CI_TOOLS": "/missing"}, clear=True),
178 patch.object(run_rust.subprocess, "check_output") as git,
179 ):
180 run_rust.prepare_tools(Path("/repo"))
181 git.assert_not_called()
182 with self.assertRaisesRegex(ValueError, "setup_windows.sh"):
183 run_rust.prepare_cross_build()
184
185 @unittest.skipUnless(sys.platform == "linux", "Linux 파일시스템과 소유권 검사 시험임")
186 def test_filesystem_skip_is_limited_to_publication_test_crates(self):
187 with tempfile.TemporaryDirectory(dir=run_rust.ROOT / "tests/ci") as directory:
188 root = Path(directory)
189 for name in run_rust.FILE_FIXTURES:
190 (root / "tests" / name).mkdir(parents=True)
191 paths = [
192 root / "tests/rust-file-publication/Cargo.toml",
193 root / "tests/rust-gzip-file-job/Cargo.toml",
194 root / "tests/rust-file-snapshot/Cargo.toml",
195 root / "experiments/rust-file-publication/Cargo.toml",
196 ]
197 with (
198 patch.object(run_rust, "manifests", return_value=paths),
199 patch.object(run_rust.subprocess, "check_output", return_value="1021994\n"),
200 patch.object(run_rust.subprocess, "run", return_value=subprocess.CompletedProcess([], 0)) as cargo,
201 ):
202 results = run_rust.run(root, "test")
203 self.assertEqual([row["status"] for row in results], [
204 "not-applicable", "not-applicable", "passed", "passed",
205 ])
206 self.assertIn("0x1021994", results[0]["reason"])
207 self.assertEqual(cargo.call_count, 2)
208 cargo.reset_mock()
209 results = run_rust.run(root, "cross-build")
210 self.assertTrue(all(row["status"] == "passed" for row in results))
211 self.assertEqual(cargo.call_count, 4)
212
213 def test_unknown_filesystem_probe_error_is_failure(self):
214 with (
215 patch.object(run_rust, "prepare_fixtures", side_effect=subprocess.CalledProcessError(1, "stat")),
216 patch.object(run_rust.sys, "platform", "linux"),
217 ):
218 results = run_rust.execute_phases(run_rust.ROOT, ["test"])
219 self.assertEqual(results[0]["status"], "failed")
220
221 def test_cargo_contract(self):
222 args = run_rust.command(Path("tests/example/Cargo.toml"), "test")
223 self.assertIn("--locked", args)
224 self.assertIn("--all-features", args)
225 self.assertEqual(args[-2:], ["--", "--test-threads=1"])
226
227
228if __name__ == "__main__":
229 unittest.main()