def test_save_config_bad_path_exists(self): cm = ConfigMixin() with temporary_path() as tmp: config_folder = tmp.joinpath("config") config_folder.mkdir(parents=True) cm.config_path = config_folder with self.assertRaises(FileExistsError) as err: cm.save_config() self.assertIn("is an existing directory name", str(err.exception))
def test_load_config_saved(self): m = mock.Mock() loader = m.a() cm = ConfigMixin() cm.add_config_item(key="test-int", saver=lambda: 123, loader=loader, default=456) with temporary_path() as tmp: cm.config_path = tmp.joinpath("config") cm.save_config() cm.load_config() self.assertTrue(loader.called) self.assertTrue(loader.called_with('123'))
def test_load_config_default_no_file(self): m = mock.Mock() loader = m.a() cm = ConfigMixin() cm.add_config_item(key="test-int", saver=lambda: 123, loader=loader, default=456) with temporary_path() as tmp: cm.config_path = tmp.joinpath("config") cm.load_config() self.assertTrue(loader.called) # not yet saved, should be called with default value self.assertTrue(loader.called_with('456'))
def test_run_subprocess(self): test_directory_name = "test-dir" if os.name == "posix": mkdir_cmd = ("mkdir", test_directory_name) elif os.name == "nt": mkdir_cmd = ("cmd", "/c", "mkdir", test_directory_name) else: raise NotImplementedError iface = InterfacePlugin() with temporary_path() as tmp: result = iface.run_subprocess(mkdir_cmd, cwd=tmp, shell=False) self.assertEqual(0, result) self.assertTrue(tmp.joinpath(test_directory_name).is_dir())
def test_load_config_errors(self): def loader_with_errors(value: int): # noqa raise RuntimeError cm = ConfigMixin() cm.add_config_item(key="test-int", saver=lambda: 123, loader=loader_with_errors, default=456) with temporary_path() as tmp: cm.config_path = tmp.joinpath("config") cm.save_config() try: cm.load_config() except RuntimeError: self.fail("No errors should be raised.")
def test_save_config(self): cm = ConfigMixin() cm.add_config_item(key="test-bool", saver=lambda: True, loader=lambda x: x, default=True) cm.add_config_item(key="test-int", saver=lambda: 123, loader=lambda x: x, default=456) with temporary_path() as tmp: cm.config_path = tmp.joinpath("config") cm.save_config() with cm.config_path.open("r") as fp: saved_data = json.load(fp) self.assertIn("test-bool", saved_data.keys()) self.assertIn("test-int", saved_data.keys()) self.assertTrue(saved_data["test-bool"]) self.assertEqual(123, saved_data["test-int"])
def test_file_exists(self): v = ValidateFileExists(param=None) # noqa with temporary_path() as tmp: file1 = tmp.joinpath("file1") with self.assertRaises(ParameterValidationError): v.validate(str(file1)) file1.touch() last_path = os.getcwd() os.chdir(tmp) try: value = v.validate("file1") except ParameterValidationError: self.fail() else: self.assertEqual(str(file1), value) finally: os.chdir(last_path)
def test_folder_exists(self): v = ValidateFolderExists(param=None) # noqa with temporary_path() as tmp: folder = tmp.joinpath("folder") with self.assertRaises(ParameterValidationError): v.validate(str(folder)) folder.mkdir() try: v.validate(str(folder)) except ParameterValidationError: self.fail() last_path = os.getcwd() os.chdir(tmp) try: value = v.validate("folder") except ParameterValidationError: self.fail() else: self.assertEqual(str(folder), value) finally: os.chdir(last_path)
def test_data(self): with temporary_path() as tmp: class TestPlugin(BasePlugin): def __init__(self): super().__init__() self.config_path = tmp.joinpath("config.cfg") self.data = {} def init_config(self): self.add_config_item(key="test", saver=lambda: self.data, loader=lambda x: self.data.update(x), default={}) tp = TestPlugin() self.assertDictEqual({}, tp.data) tp.data['test'] = 'value' tp.save_config() tp.data.clear() tp.load_config() self.assertDictEqual({'test': 'value'}, tp.data)