test_install_voc.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import importlib.util
  2. import json
  3. from pathlib import Path
  4. import sqlite3
  5. import tempfile
  6. import unittest
  7. s=importlib.util.spec_from_file_location('install_voc',Path(__file__).with_name('install_voc.py'))
  8. m=importlib.util.module_from_spec(s);s.loader.exec_module(m)
  9. class InstallTests(unittest.TestCase):
  10. def setUp(self):
  11. self.temp=tempfile.TemporaryDirectory();self.root=Path(self.temp.name)
  12. self.package=self.root/'package';self.library=self.root/'installed/library';self.skill=self.root/'installed/skills/voc'
  13. (self.package/'library').mkdir(parents=True);(self.package/'skill').mkdir()
  14. c=sqlite3.connect(self.package/'library/library.sqlite');c.execute('create table meta(key text primary key,value text)');c.execute("insert into meta values('generation','test-generation')");c.commit();c.close()
  15. (self.package/'skill/SKILL.md').write_text('fixture',encoding='utf-8')
  16. files={p.relative_to(self.package).as_posix():m.digest(p) for p in self.package.rglob('*') if p.is_file()}
  17. (self.package/'INSTALL.json').write_text(json.dumps(dict(generation='test-generation',files=files)),encoding='utf-8')
  18. def tearDown(self):self.temp.cleanup()
  19. def test_plan_does_not_create_targets(self):
  20. r=m.install(self.package,self.library,self.skill)
  21. self.assertTrue(r['readOnly']);self.assertFalse(self.library.exists());self.assertFalse(self.skill.exists())
  22. def test_apply_readback_and_noop(self):
  23. r=m.install(self.package,self.library,self.skill,True)
  24. self.assertTrue(r['filesystemVerified']);self.assertTrue(r['applied'])
  25. r=m.install(self.package,self.library,self.skill,True)
  26. self.assertFalse(r['applied'])
  27. def test_conflicting_existing_files_preserved(self):
  28. self.skill.mkdir(parents=True);(self.skill/'other.txt').write_text('keep')
  29. with self.assertRaisesRegex(ValueError,'TARGET_EXISTS_DIFFERENT_CONTENT'):m.install(self.package,self.library,self.skill,True)
  30. self.assertFalse(self.library.exists());self.assertEqual((self.skill/'other.txt').read_text(),'keep')
  31. def test_tampered_package_stops_before_install(self):
  32. (self.package/'skill/SKILL.md').write_text('tampered')
  33. with self.assertRaisesRegex(ValueError,'PACKAGE_HASH_MISMATCH'):m.install(self.package,self.library,self.skill,True)
  34. self.assertFalse(self.library.exists())
  35. def test_unexpected_extra_files_and_nested_target_rejected(self):
  36. (self.package/'skill/secret.txt').write_text('fixture-not-a-secret')
  37. with self.assertRaisesRegex(ValueError,'UNEXPECTED_PACKAGE_FILES'):m.install(self.package,self.library,self.skill)
  38. with self.assertRaisesRegex(ValueError,'NESTED_TARGETS'):m.install(self.package,self.library,self.library/'skill')
  39. if __name__=='__main__':unittest.main()