def test_update_yaml_file_not_found_error(self): try: update_yaml_file("/some-unknown-dir/some-random-unknown-file", "a.b", "foo") self.fail() except FileNotFoundError: pass
def __update_yaml_file(git_repo: GitRepo, file_path: str, key: str, value: Any) -> bool: full_file_path = git_repo.get_full_file_path(file_path) try: return update_yaml_file(full_file_path, key, value) except (FileNotFoundError, IsADirectoryError) as ex: raise GitOpsException(f"No such file: {file_path}") from ex except YAMLException as ex: raise GitOpsException(f"Error loading file: {file_path}") from ex except KeyError as ex: raise GitOpsException( f"Key '{key}' not found in file: {file_path}") from ex
def __update_values(self, git_repo: GitRepo) -> Dict[str, Any]: args = self.__args single_commit = args.single_commit or args.commit_message full_file_path = git_repo.get_full_file_path(args.file) updated_values = {} for key, value in args.values.items(): try: updated_value = update_yaml_file(full_file_path, key, value) except (FileNotFoundError, IsADirectoryError) as ex: raise GitOpsException(f"No such file: {args.file}") from ex except YAMLException as ex: raise GitOpsException( f"Error loading file: {args.file}") from ex except KeyError as ex: raise GitOpsException( f"Key '{key}' not found in file: {args.file}") from ex if not updated_value: logging.info("Yaml property %s already up-to-date", key) continue logging.info("Updated yaml property %s to %s", key, value) updated_values[key] = value if not single_commit: self.__commit(git_repo, f"changed '{key}' to '{value}' in {args.file}") if single_commit and updated_values: if args.commit_message: message = args.commit_message elif len(updated_values) == 1: key, value = list(updated_values.items())[0] message = f"changed '{key}' to '{value}' in {args.file}" else: updates_count = len(updated_values) message = f"updated {updates_count} value{'s' if updates_count > 1 else ''} in {args.file}" message += f"\n\n{yaml_dump(updated_values)}" self.__commit(git_repo, message) return updated_values
def test_update_yaml_file_is_a_directory_error(self): try: update_yaml_file("/tmp", "a.b", "foo") self.fail() except IsADirectoryError: pass
def test_update_yaml_file(self): test_file = self._create_file( """\ a: # comment 1 # comment 2 b: d: 1 # comment 3 c: 2 # comment 4 e: "expect quotes are preserved" e: - f: 3 # comment 5 g: 4 # comment 6 - [hello, world] # comment 7 - foo: # comment 8 bar # comment 9""" ) self.assertTrue(update_yaml_file(test_file, "a.b.c", "2")) self.assertFalse(update_yaml_file(test_file, "a.b.c", "2")) # already updated self.assertTrue(update_yaml_file(test_file, "a.e.[0].g", 42)) self.assertFalse(update_yaml_file(test_file, "a.e.[0].g", 42)) # already updated self.assertTrue(update_yaml_file(test_file, "a.e.[1].[1]", "tester")) self.assertFalse(update_yaml_file(test_file, "a.e.[1].[1]", "tester")) # already updated self.assertTrue(update_yaml_file(test_file, "a.e.[2]", "replaced object")) self.assertFalse(update_yaml_file(test_file, "a.e.[2]", "replaced object")) # already updated expected = """\ a: # comment 1 # comment 2 b: d: 1 # comment 3 c: '2' # comment 4 e: "expect quotes are preserved" e: - f: 3 # comment 5 g: 42 # comment 6 - [hello, tester] # comment 7 - replaced object """ actual = self._read_file(test_file) self.assertEqual(expected, actual) with pytest.raises(KeyError) as ex: update_yaml_file(test_file, "x.y", "foo") self.assertEqual("\"Key 'x' not found in YAML!\"", str(ex.value)) with pytest.raises(KeyError) as ex: update_yaml_file(test_file, "[42].y", "foo") self.assertEqual("\"Key '[42]' not found in YAML!\"", str(ex.value)) with pytest.raises(KeyError) as ex: update_yaml_file(test_file, "a.x", "foo") self.assertEqual("\"Key 'a.x' not found in YAML!\"", str(ex.value)) with pytest.raises(KeyError) as ex: update_yaml_file(test_file, "a.[42]", "foo") self.assertEqual("\"Key 'a.[42]' not found in YAML!\"", str(ex.value)) with pytest.raises(KeyError) as ex: update_yaml_file(test_file, "a.e.[3]", "foo") self.assertEqual("\"Key 'a.e.[3]' not found in YAML!\"", str(ex.value)) with pytest.raises(KeyError) as ex: update_yaml_file(test_file, "a.e.[2].[2]", "foo") self.assertEqual("\"Key 'a.e.[2].[2]' not found in YAML!\"", str(ex.value)) with pytest.raises(KeyError) as ex: update_yaml_file(test_file, "", "foo") self.assertEqual("'Empty key!'", str(ex.value)) actual = self._read_file(test_file) self.assertEqual(expected, actual)