'''Проверки регистрации сервиса без удаления других сервисов терминала.''' import tempfile import unittest from pathlib import Path from deploy_service import services_ini_update, write_services_ini class ServicesIniTests(unittest.TestCase): def setUp(self): self.temp = tempfile.TemporaryDirectory() self.root = Path(self.temp.name) self.template = Path(__file__).resolve().parent.parent / 'static/services.ini' def tearDown(self): self.temp.cleanup() def test_missing_configuration_is_created_as_utf16_and_is_idempotent(self): target, text, changed = services_ini_update(self.root, self.template, 'custom.sqlite') self.assertTrue(changed) write_services_ini(target, text) self.assertTrue(target.read_bytes().startswith(b'\xff\xfe')) self.assertIn('enabled=1', text) self.assertIn('path=Services\\Shared Projects\\mt5-manager\\ServiceStateWriter.ex5', text) self.assertIn('InpDatabase=custom.sqlite', text) self.assertNotIn('\r\r\n', target.read_bytes().decode('utf-16')) self.assertFalse(services_ini_update(self.root, self.template, 'custom.sqlite')[2]) def test_repair_preserves_other_services_and_custom_parameters(self): other = '\nname=OtherService\npath=Services\\Other.ex5\nenabled=1\n' ours = self.template.read_text(encoding='utf-16').strip() ours = ours.replace('enabled=1', 'enabled=0').replace('InpIntervalSec=2', 'InpIntervalSec=30') ours = ours.replace('path=Services\\Shared Projects\\mt5-manager\\ServiceStateWriter.ex5', 'path=Services\\old-location.ex5') target = self.root / 'config/services.ini' target.parent.mkdir() target.write_text(other + '\n' + ours + '\n', encoding='utf-16') _, text, changed = services_ini_update(self.root, self.template, 'updated.sqlite') self.assertTrue(changed) self.assertIn(other, text) self.assertIn('InpIntervalSec=30', text) self.assertIn('InpDatabase=updated.sqlite', text) self.assertIn('path=Services\\Shared Projects\\mt5-manager\\ServiceStateWriter.ex5', text) self.assertNotIn('enabled=0', text) def test_existing_unrelated_service_is_kept_when_adding_ours(self): target = self.root / 'config/services.ini' target.parent.mkdir() other = '\nname=Other\npath=Services\\Other.ex5\nenabled=1\n\n' target.write_text(other, encoding='utf-16') _, text, changed = services_ini_update(self.root, self.template, 'custom.sqlite') self.assertTrue(changed) self.assertIn(other, text) self.assertEqual(text.count(''), 2) def test_malformed_configuration_is_not_overwritten(self): target = self.root / 'config/services.ini' target.parent.mkdir() target.write_text('\nname=Other\n', encoding='utf-16') original = target.read_bytes() with self.assertRaises(ValueError): services_ini_update(self.root, self.template, 'custom.sqlite') self.assertEqual(target.read_bytes(), original) def test_malformed_inputs_are_not_overwritten(self): target = self.root / 'config/services.ini' target.parent.mkdir() target.write_text('\nname=ServiceStateWriter\n\n', encoding='utf-16') original = target.read_bytes() with self.assertRaises(ValueError): services_ini_update(self.root, self.template, 'custom.sqlite') self.assertEqual(target.read_bytes(), original) if __name__ == '__main__': unittest.main()