21 lines
1.3 KiB
Python
21 lines
1.3 KiB
Python
import importlib.util, pathlib, tempfile, unittest, pwd
|
|
spec = importlib.util.spec_from_file_location('identity', pathlib.Path(__file__).resolve().parents[1] / 'packaging/ensure-default-identity.py')
|
|
m = importlib.util.module_from_spec(spec); spec.loader.exec_module(m)
|
|
class IdentityTest(unittest.TestCase):
|
|
def test_missing_and_idempotent(self):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
p=pathlib.Path(d)/'nwserv.conf'; p.write_text('2 MARS\n')
|
|
self.assertTrue(m.repair(p)); content=p.read_text()
|
|
self.assertIn(f'11 {pwd.getpwnam("nobody").pw_uid}\n', content)
|
|
self.assertFalse(m.repair(p)); self.assertEqual(p.read_text(), content)
|
|
self.assertEqual(len(list(pathlib.Path(d).glob('*.pre-*'))), 1)
|
|
def test_preserve_existing(self):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
p=pathlib.Path(d)/'nwserv.conf'; p.write_text('10 123\n11 456\n')
|
|
self.assertFalse(m.repair(p)); self.assertEqual(p.read_text(),'10 123\n11 456\n')
|
|
def test_partial(self):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
p=pathlib.Path(d)/'nwserv.conf'; p.write_text('# 11 0\n10 123')
|
|
m.repair(p); self.assertIn('10 123\n',p.read_text()); self.assertIn('\n11 ',p.read_text())
|
|
if __name__ == '__main__': unittest.main()
|