import os
import tempfile
import unittest
from unittest.mock import patch
from olc.stub.config_stub_manager import JsonFileConfigStub, OlcConfigPluginManager, Stub
class TestConfigStubManager(unittest.TestCase):
def setUp(self):
self.temp_dir = tempfile.mkdtemp()
self.json_file = os.path.join(self.temp_dir, "olc.json")
with open(self.json_file, "w", encoding="utf-8") as f:
f.write('{"rules": ["rule1", "rule2"]}\n')
OlcConfigPluginManager._instance = None
def tearDown(self):
for root, dirs, files in os.walk(self.temp_dir, topdown=False):
for file in files:
os.remove(os.path.join(root, file))
for dir in dirs:
os.rmdir(os.path.join(root, dir))
os.rmdir(self.temp_dir)
@patch('olc.utils.path_utils.PathUtils.get_config_path')
def test_json_file_config_stub_get_olc_config_rules_success(self, mock_get_config_path):
mock_get_config_path.return_value = self.temp_dir + "/"
stub = JsonFileConfigStub()
result = stub.get_olc_config_rules()
self.assertIsNotNone(result)
self.assertIn('"rules": ["rule1", "rule2"]', result)
@patch('olc.utils.path_utils.PathUtils.get_config_path')
def test_json_file_config_stub_get_olc_config_rules_file_not_found(self, mock_get_config_path):
non_existent_dir = os.path.join(self.temp_dir, "non_existent")
mock_get_config_path.return_value = non_existent_dir + "/"
stub = JsonFileConfigStub()
result = stub.get_olc_config_rules()
self.assertIsNone(result)
@patch('olc.utils.path_utils.PathUtils.get_config_path')
def test_json_file_config_stub_get_olc_config_rules_not_json_file(self, mock_get_config_path):
mock_get_config_path.return_value = self.temp_dir + "/"
os.remove(self.json_file)
non_json_file = os.path.join(self.temp_dir, "olc.txt")
with open(non_json_file, "w", encoding="utf-8") as f:
f.write("not a json file")
stub = JsonFileConfigStub()
result = stub.get_olc_config_rules()
self.assertIsNone(result)
os.remove(non_json_file)
def test_olc_config_plugin_manager_register_stub(self):
manager = OlcConfigPluginManager.get_instance()
class TestStub(Stub):
def close_stub(self):
pass
def get_stub_name(self):
return "test"
def get_olc_config_rules(self):
return None
manager.register_stub("test", TestStub)
self.assertIn("test", manager.get_stub_list())
self.assertEqual(manager.get_stub_plugin("test"), TestStub)
def test_olc_config_plugin_manager_get_stub_plugin_not_found(self):
manager = OlcConfigPluginManager.get_instance()
result = manager.get_stub_plugin("non_existent")
self.assertIsNone(result)
def test_olc_config_plugin_manager_register_stub_empty_name(self):
manager = OlcConfigPluginManager.get_instance()
class TestStub(Stub):
def close_stub(self):
pass
def get_stub_name(self):
return "test"
def get_olc_config_rules(self):
return None
manager.register_stub("", TestStub)
self.assertNotIn("", manager.get_stub_list())
def test_olc_config_plugin_manager_register_stub_none_stub(self):
manager = OlcConfigPluginManager.get_instance()
manager.register_stub("test", None)
self.assertNotIn("test", manager.get_stub_list())
if __name__ == "__main__":
unittest.main()