-
-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathtest_build_shaft_mcp_live_jar.py
More file actions
113 lines (99 loc) · 4.49 KB
/
Copy pathtest_build_shaft_mcp_live_jar.py
File metadata and controls
113 lines (99 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import os
import re
import subprocess
import tempfile
import unittest
from pathlib import Path
ACTION_PATH = Path(__file__).resolve().parents[2] / ".github" / "actions" / "build-shaft-mcp-live-jar" / "action.yml"
class BuildShaftMcpLiveJarTest(unittest.TestCase):
@staticmethod
def _build_script() -> str:
action = ACTION_PATH.read_text(encoding="utf-8")
return action.split(" run: |\n", 1)[1].split(
" - name: Write shaft-mcp launcher argfile", 1
)[0]
def test_launcher_build_retries_the_complete_maven_sequence(self):
build_script = self._build_script()
self.assertIn("for attempt in 1 2 3; do", build_script)
self.assertIn('sleep "$((attempt * 10))"', build_script)
self.assertIn('exit "$status"', build_script)
self.assertRegex(
build_script,
re.compile(
r"mvn --batch-mode -pl shaft-mcp -am install .*?\n"
r"\s*&& mvn --batch-mode -pl shaft-mcp dependency:copy-dependencies .*?\n"
r"\s*status=\$\?",
re.DOTALL,
),
)
def test_launcher_build_retries_after_copy_dependencies_fails_under_errexit(self):
with tempfile.TemporaryDirectory() as temporary_directory:
temporary_path = Path(temporary_directory)
maven_log = temporary_path / "maven.log"
fake_maven = temporary_path / "mvn"
fake_maven.write_bytes(
b"#!/usr/bin/env bash\n"
b"log=\"$(dirname \"$0\")/maven.log\"\n"
b"printf '%s\\n' \"$*\" >> \"$log\"\n"
b"if [ \"$(wc -l < \"$log\")\" -eq 2 ]; then\n"
b" exit 17\n"
b"fi\n"
)
fake_sleep = temporary_path / "sleep"
fake_sleep.write_bytes(b"#!/usr/bin/env bash\n")
fake_maven.chmod(0o755)
fake_sleep.chmod(0o755)
environment = os.environ | {
"PATH": f"{temporary_path}{os.pathsep}{os.environ['PATH']}",
}
completed = subprocess.run(
["bash", "-e"],
input=(
b'PATH="$(dirname "$(command -v mvn)"):$PATH"\n'
+ self._build_script().encode()
),
capture_output=True,
env=environment,
)
self.assertEqual(completed.returncode, 0, completed.stderr.decode())
maven_commands = maven_log.read_text(encoding="utf-8").splitlines()
self.assertEqual(len(maven_commands), 4)
self.assertIn("install", maven_commands[0])
self.assertIn("dependency:copy-dependencies", maven_commands[1])
self.assertIn("install", maven_commands[2])
self.assertIn("dependency:copy-dependencies", maven_commands[3])
def test_launcher_build_propagates_the_final_copy_dependencies_failure(self):
with tempfile.TemporaryDirectory() as temporary_directory:
temporary_path = Path(temporary_directory)
maven_log = temporary_path / "maven.log"
fake_maven = temporary_path / "mvn"
fake_maven.write_bytes(
b"#!/usr/bin/env bash\n"
b"log=\"$(dirname \"$0\")/maven.log\"\n"
b"printf '%s\\n' \"$*\" >> \"$log\"\n"
b"case \"$*\" in *dependency:copy-dependencies*) exit 17;; esac\n"
)
fake_sleep = temporary_path / "sleep"
fake_sleep.write_bytes(b"#!/usr/bin/env bash\n")
fake_maven.chmod(0o755)
fake_sleep.chmod(0o755)
environment = os.environ | {
"PATH": f"{temporary_path}{os.pathsep}{os.environ['PATH']}",
}
completed = subprocess.run(
["bash", "-e"],
input=(
b'PATH="$(dirname "$(command -v mvn)"):$PATH"\n'
+ self._build_script().encode()
),
capture_output=True,
env=environment,
)
self.assertEqual(completed.returncode, 17, completed.stderr.decode())
maven_commands = maven_log.read_text(encoding="utf-8").splitlines()
self.assertEqual(len(maven_commands), 6)
for install, copy_dependencies in zip(maven_commands[::2], maven_commands[1::2]):
self.assertIn("install", install)
self.assertIn("dependency:copy-dependencies", copy_dependencies)
if __name__ == "__main__":
unittest.main()