| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- import importlib.util
- import tempfile
- import unittest
- from pathlib import Path
- from unittest.mock import patch
- import yaml
- spec = importlib.util.spec_from_file_location('bridge_installer', Path(__file__).parents[3] / 'scripts/install-hermes-bridge.py')
- installer = importlib.util.module_from_spec(spec)
- spec.loader.exec_module(installer)
- class InstallerTests(unittest.TestCase):
- def setUp(self):
- self.temp = tempfile.TemporaryDirectory()
- self.root = Path(self.temp.name)
- self.source = self.root / 'source'; self.source.mkdir()
- (self.source / 'plugin.yaml').write_text('name: yaoyao-bot-bridge\nversion: 1.2.0\n')
- self.home = self.root / 'hermes'; self.home.mkdir()
- self.config = self.home / 'config.yaml'
- self.config.write_text('model:\n default: keep-model\nplugins:\n enabled: [existing]\n')
- self.target = self.home / 'plugins' / 'yaoyao-bot-bridge'
- self.target.mkdir(parents=True)
- (self.target / 'old').write_text('previous-plugin')
- def tearDown(self):
- self.temp.cleanup()
- def test_upgrade_backs_up_plugin_and_preserves_unrelated_config(self):
- original = self.config.read_bytes()
- result = installer.install(self.home, 'default', self.source)
- self.assertEqual((Path(result['backup']) / 'config.yaml').read_bytes(), original)
- self.assertEqual((Path(result['backup']) / 'yaoyao-bot-bridge' / 'old').read_text(), 'previous-plugin')
- config = yaml.safe_load(self.config.read_text())
- self.assertEqual(config['model']['default'], 'keep-model')
- self.assertEqual(config['plugins']['enabled'], ['existing', 'yaoyao-bot-bridge'])
- def test_preserves_an_explicitly_disabled_plugin(self):
- self.config.write_text('plugins:\n disabled: [yaoyao-bot-bridge]\n')
- result = installer.install(self.home, 'default', self.source)
- self.assertTrue(result['disabled'])
- self.assertNotIn('yaoyao-bot-bridge', yaml.safe_load(self.config.read_text())['plugins'].get('enabled', []))
- def test_failed_copy_or_config_write_preserves_original_installation(self):
- for function in ['shutil.copytree', 'yaml.safe_dump']:
- with self.subTest(function=function):
- original = self.config.read_bytes()
- with patch.object(installer.shutil if function.startswith('shutil') else installer.yaml,
- function.split('.')[1], side_effect=RuntimeError('fixture-failure')):
- with self.assertRaises(RuntimeError): installer.install(self.home, 'default', self.source)
- self.assertEqual(self.config.read_bytes(), original)
- self.assertEqual((self.target / 'old').read_text(), 'previous-plugin')
- if __name__ == '__main__':
- unittest.main()
|