Setting the file. One moment. Test Persist Skill · Turnstile Spin · cloudflare/skills · Skills Docs17.11
Widget Create
Bundled file Validation
tests/test_persist_skill.py
Python·81 lines·3 KB
12
13
14class PersistSkillTests(unittest.TestCase):
15 def setUp(self):
16 self.temp = tempfile.TemporaryDirectory()
17 self.addCleanup(self.temp.cleanup)
18 self.root = Path(self.temp.name)
19 self.project = self.root / "project"
20 self.project.mkdir()
21 self.bin = self.root / "bin"
22 self.bin.mkdir()
23 # Supply a minimal cloned bundle without contacting GitHub.
24 git = self.bin / "git"
25 git.write_text('''#!/usr/bin/env bash
26set -eu
27if [[ "$*" == *sparse-checkout* ]]; then
28 exit 0
29fi
30for destination; do :; done
31mkdir -p "$destination/skills/turnstile-spin/scripts"
32printf 'fixture skill\\n' > "$destination/skills/turnstile-spin/SKILL.md"
33printf '#!/usr/bin/env bash\\n' > "$destination/skills/turnstile-spin/scripts/helper.sh"
34''')
35 git.chmod(0o755)
36 self.env = {**os.environ, "PATH": str(self.bin) + os.pathsep + os.environ["PATH"]}
37
38 def run_script(self, target):
39 result = subprocess.run(
40 ["bash", str(SCRIPT), "--path", str(target / "SKILL.md")],
41 cwd=self.project,
42 env=self.env,
43 capture_output=True,
44 text=True,
45 )
46 return result, json.loads(result.stdout)
47
48 def test_copy_failure_is_reported(self):
49 # A file where the parent directory should be prevents installation.
50 parent = self.project / ".claude"
51 parent.write_text("keep this file\n")
52 result, output = self.run_script(parent / "skills" / "turnstile-spin")
53 self.assertNotEqual(result.returncode, 0, result.stdout + result.stderr)
54 self.assertEqual(output, {"status": "error", "reason": "copy_failed"})
55 self.assertEqual(parent.read_text(), "keep this file\n")
56
57 def test_copy_to_new_or_empty_directory(self):
58 for existing in (False, True):
59 with self.subTest(existing=existing):
60 target = self.project / str(existing) / "turnstile-spin"
61 if existing:
62 target.mkdir(parents=True)
63 result, output = self.run_script(target)
64 self.assertEqual(result.returncode, 0, result.stderr)
65 self.assertEqual(output["status"], "ok")
66 self.assertEqual((target / "SKILL.md").read_text(), "fixture skill\n")
67 self.assertTrue(os.access(target / "scripts" / "helper.sh", os.X_OK))
68
69 def test_nonempty_destination_is_preserved(self):
70 target = self.project / "turnstile-spin"
71 target.mkdir()
72 existing = target / "SKILL.md"
73 existing.write_text("existing skill\n")
74 result, output = self.run_script(target)
75 self.assertNotEqual(result.returncode, 0)
76 self.assertEqual(output["reason"], "target_not_empty")
77 self.assertEqual(existing.read_text(), "existing skill\n")
78
79
80if __name__ == "__main__":
81 unittest.main()