composer/tests/materialize.py

69 lines
3 KiB
Python

"""Filesystem contract fixtures; executed only by the normal release pipeline."""
import importlib.util
import os
from pathlib import Path
import tempfile
import unittest
spec = importlib.util.spec_from_file_location('materialize', Path(__file__).parents[1] / 'materialize.py')
engine = importlib.util.module_from_spec(spec)
spec.loader.exec_module(engine)
class Materialization(unittest.TestCase):
def setUp(self):
self.temporary = tempfile.TemporaryDirectory(prefix='archipelago-compose-')
self.root = Path(self.temporary.name)
self.base = self.root / 'base'
self.output = self.root / 'output'
(self.base / 'usr/share').mkdir(parents=True)
self.sources = self.root / 'sources'
self.sources.mkdir()
(self.sources / 'file').write_bytes(b'new payload\n')
def tearDown(self):
self.temporary.cleanup()
def file(self, path, before=None):
return dict(path=path, before=before, after=engine.identity(self.sources / 'file'),
source_root=str(self.sources), source_path='/file')
def run_plan(self, effects):
engine.materialize(self.base, self.output, dict(version=0, effects=effects))
def test_new_directory_and_file_preserve_exact_metadata(self):
os.chmod(self.sources / 'file', 0o4750)
os.setxattr(self.sources / 'file', 'user.archipelago', b'fixture')
directory = engine.identity(self.base / 'usr/share')
self.run_plan([self.file('/usr/share/new/file'),
dict(path='/usr/share/new', before=None, after=directory)])
self.assertEqual(engine.identity(self.output / 'usr/share/new/file'), engine.identity(self.sources / 'file'))
self.assertFalse((self.base / 'usr/share/new').exists())
def test_replacement_does_not_follow_old_symlink(self):
victim = self.root / 'victim'
victim.write_bytes(b'keep this\n')
link = self.base / 'usr/share/link'
link.symlink_to(victim)
self.run_plan([self.file('/usr/share/link', engine.identity(link))])
self.assertEqual(victim.read_bytes(), b'keep this\n')
self.assertTrue(link.is_symlink())
self.assertFalse((self.output / 'usr/share/link').is_symlink())
def test_changed_source_rejected_before_output_creation(self):
effect = self.file('/usr/share/file')
(self.sources / 'file').write_bytes(b'changed after planning\n')
with self.assertRaises(ValueError):
self.run_plan([effect])
self.assertFalse(self.output.exists())
def test_no_implicit_recursive_delete(self):
(self.base / 'usr/share/retained').write_text('retain')
with self.assertRaises(OSError):
self.run_plan([dict(path='/usr/share', before=engine.identity(self.base / 'usr/share'), after=None)])
self.assertEqual((self.base / 'usr/share/retained').read_text(), 'retain')
self.assertEqual((self.output / 'usr/share/retained').read_text(), 'retain')
if __name__ == '__main__':
unittest.main()