26 lines
1.8 KiB
Python
26 lines
1.8 KiB
Python
import importlib.util,pathlib,tempfile,unittest,os,time
|
|
spec=importlib.util.spec_from_file_location('disk',pathlib.Path(__file__).with_name('managed-disk.py'))
|
|
class Tests(unittest.TestCase):
|
|
def test_expiry_and_escape(self):
|
|
self.assertTrue(pathlib.Path(spec.origin).exists(),'managed retention implementation missing')
|
|
m=importlib.util.module_from_spec(spec);spec.loader.exec_module(m)
|
|
with tempfile.TemporaryDirectory() as d:
|
|
r=pathlib.Path(d);old=r/'old';old.mkdir();(old/'.disposable-test').write_text('canina-managed-test-v1\n');(old/'result').write_text('fixture');os.utime(old,(1,1))
|
|
unknown=r/'unknown';unknown.mkdir();os.utime(unknown,(1,1))
|
|
outside=r/'source';outside.mkdir();(outside/'important').write_text('preserve');os.symlink(outside,r/'escape')
|
|
self.assertEqual(m.retire(r,now=time.time()),['old']);self.assertEqual(m.retire(r,now=time.time()),[])
|
|
self.assertTrue((outside/'important').exists());self.assertTrue(unknown.exists());self.assertTrue((r/'escape').is_symlink())
|
|
def test_open_artifact_is_preserved(self):
|
|
m=importlib.util.module_from_spec(spec);spec.loader.exec_module(m)
|
|
with tempfile.TemporaryDirectory() as d:
|
|
r=pathlib.Path(d);p=r/'busy';p.mkdir();(p/'.disposable-test').write_text('canina-managed-test-v1\n');f=(p/'result').open('w');os.utime(p,(1,1))
|
|
try:self.assertEqual(m.retire(r),[])
|
|
finally:f.close()
|
|
self.assertEqual(m.retire(r),['busy'])
|
|
def test_mounted_artifact_is_preserved(self):
|
|
m=importlib.util.module_from_spec(spec);spec.loader.exec_module(m)
|
|
with tempfile.TemporaryDirectory() as d:
|
|
r=pathlib.Path(d);p=r/'mounted';p.mkdir();(p/'.disposable-test').write_text('canina-managed-test-v1\n');os.utime(p,(1,1))
|
|
self.assertEqual(m.retire(r,protected=[str(p/'result')]),[])
|
|
if __name__=='__main__':unittest.main()
|