| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- import importlib.util
- import json
- from pathlib import Path
- import sqlite3
- import tempfile
- import unittest
- s=importlib.util.spec_from_file_location('install_voc',Path(__file__).with_name('install_voc.py'))
- m=importlib.util.module_from_spec(s);s.loader.exec_module(m)
- class InstallTests(unittest.TestCase):
- def setUp(self):
- self.temp=tempfile.TemporaryDirectory();self.root=Path(self.temp.name)
- self.package=self.root/'package';self.library=self.root/'installed/library';self.skill=self.root/'installed/skills/voc'
- (self.package/'library').mkdir(parents=True);(self.package/'skill').mkdir()
- 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()
- (self.package/'skill/SKILL.md').write_text('fixture',encoding='utf-8')
- files={p.relative_to(self.package).as_posix():m.digest(p) for p in self.package.rglob('*') if p.is_file()}
- (self.package/'INSTALL.json').write_text(json.dumps(dict(generation='test-generation',files=files)),encoding='utf-8')
- def tearDown(self):self.temp.cleanup()
- def test_plan_does_not_create_targets(self):
- r=m.install(self.package,self.library,self.skill)
- self.assertTrue(r['readOnly']);self.assertFalse(self.library.exists());self.assertFalse(self.skill.exists())
- def test_apply_readback_and_noop(self):
- r=m.install(self.package,self.library,self.skill,True)
- self.assertTrue(r['filesystemVerified']);self.assertTrue(r['applied'])
- r=m.install(self.package,self.library,self.skill,True)
- self.assertFalse(r['applied'])
- def test_conflicting_existing_files_preserved(self):
- self.skill.mkdir(parents=True);(self.skill/'other.txt').write_text('keep')
- with self.assertRaisesRegex(ValueError,'TARGET_EXISTS_DIFFERENT_CONTENT'):m.install(self.package,self.library,self.skill,True)
- self.assertFalse(self.library.exists());self.assertEqual((self.skill/'other.txt').read_text(),'keep')
- def test_tampered_package_stops_before_install(self):
- (self.package/'skill/SKILL.md').write_text('tampered')
- with self.assertRaisesRegex(ValueError,'PACKAGE_HASH_MISMATCH'):m.install(self.package,self.library,self.skill,True)
- self.assertFalse(self.library.exists())
- def test_unexpected_extra_files_and_nested_target_rejected(self):
- (self.package/'skill/secret.txt').write_text('fixture-not-a-secret')
- with self.assertRaisesRegex(ValueError,'UNEXPECTED_PACKAGE_FILES'):m.install(self.package,self.library,self.skill)
- with self.assertRaisesRegex(ValueError,'NESTED_TARGETS'):m.install(self.package,self.library,self.library/'skill')
- if __name__=='__main__':unittest.main()
|