From 7b6b1232df5788dbf5e318c5468e88f3ce42c2de Mon Sep 17 00:00:00 2001 From: Graham Noel Date: Tue, 3 Jun 2025 12:54:55 +0100 Subject: [PATCH 1/9] utils.py: update file not found exception message Previous message implies the file is found but the contents are not valid. Especially when referring to a YAML file. This adds a clearer message and specifies where the code has looked for the file. --- infrahub_sdk/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/infrahub_sdk/utils.py b/infrahub_sdk/utils.py index a5d6f28d..0994e4f7 100644 --- a/infrahub_sdk/utils.py +++ b/infrahub_sdk/utils.py @@ -353,7 +353,7 @@ def write_to_file(path: Path, value: Any) -> bool: def read_file(file_name: Path) -> str: if not file_name.is_file(): - raise FileNotValidError(name=str(file_name), message=f"{file_name} is not a valid file") + raise FileNotValidError(name=str(file_name), message=f"{file_name}: not found at {Path.cwd()}") try: with Path.open(file_name, encoding="utf-8") as fobj: return fobj.read() From ef8ca7133cb4b6c166a1fc05751e5f4fcd3046d0 Mon Sep 17 00:00:00 2001 From: Graham Noel Date: Wed, 4 Jun 2025 11:08:10 +0100 Subject: [PATCH 2/9] utils.py: read_file - change argument name Clearer argument name and use of path object --- infrahub_sdk/utils.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/infrahub_sdk/utils.py b/infrahub_sdk/utils.py index 0994e4f7..d1d11606 100644 --- a/infrahub_sdk/utils.py +++ b/infrahub_sdk/utils.py @@ -351,14 +351,14 @@ def write_to_file(path: Path, value: Any) -> bool: return written is not None -def read_file(file_name: Path) -> str: - if not file_name.is_file(): - raise FileNotValidError(name=str(file_name), message=f"{file_name}: not found at {Path.cwd()}") +def read_file(file_path: Path) -> str: + if not file_path.is_file(): + raise FileNotValidError(name=str(file_path.name), message=f"{file_path.name}: not found at {file_path.cwd()}") try: - with Path.open(file_name, encoding="utf-8") as fobj: + with Path.open(file_path, encoding="utf-8") as fobj: return fobj.read() except UnicodeDecodeError as exc: - raise FileNotValidError(name=str(file_name), message=f"Unable to read {file_name} with utf-8 encoding") from exc + raise FileNotValidError(name=str(file_path.name), message=f"Unable to read {file_path.name} with utf-8 encoding") from exc def get_user_permissions(data: list[dict]) -> dict: From f9d4551244ef288f329847a5540442050b3f23c9 Mon Sep 17 00:00:00 2001 From: Graham Noel Date: Wed, 4 Jun 2025 11:10:10 +0100 Subject: [PATCH 3/9] repository.py: param signature change Downstream method changed signature: file_name --> file_path --- infrahub_sdk/ctl/repository.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/infrahub_sdk/ctl/repository.py b/infrahub_sdk/ctl/repository.py index 1992a537..98e394bf 100644 --- a/infrahub_sdk/ctl/repository.py +++ b/infrahub_sdk/ctl/repository.py @@ -45,7 +45,7 @@ def get_repository_config(repo_config_file: Path) -> InfrahubRepositoryConfig: def load_repository_config_file(repo_config_file: Path) -> dict: - yaml_data = read_file(file_name=repo_config_file) + yaml_data = read_file(file_path=repo_config_file) try: data = yaml.safe_load(yaml_data) From 1e755697df60156ef661b1dc19acfda93d44cd5a Mon Sep 17 00:00:00 2001 From: Graham Noel Date: Wed, 4 Jun 2025 11:14:47 +0100 Subject: [PATCH 4/9] Update test_yaml.py Fix test_missing_file after error message change --- tests/unit/sdk/test_yaml.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/unit/sdk/test_yaml.py b/tests/unit/sdk/test_yaml.py index f596a088..cbf1a637 100644 --- a/tests/unit/sdk/test_yaml.py +++ b/tests/unit/sdk/test_yaml.py @@ -6,11 +6,13 @@ def test_read_missing_file() -> None: - file = here / "test_data/i_do_not_exist.yml" - yaml_file = YamlFile(location=file) + file_name = "i_do_not_exist.yml" + dir = here / "test_data" + full_path = dir / filename + yaml_file = YamlFile(location=full_path) yaml_file.load_content() assert not yaml_file.valid - assert yaml_file.error_message == f"{file} is not a valid file" + assert yaml_file.error_message == f"{file_name}: not found at {dir}" def test_read_incorrect_encoding() -> None: From 2fe34b40423b2b1f31a4e504c08fd44bda3ae119 Mon Sep 17 00:00:00 2001 From: Graham Noel Date: Wed, 4 Jun 2025 11:16:40 +0100 Subject: [PATCH 5/9] Update test_yaml.py Use correct variable name --- tests/unit/sdk/test_yaml.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/sdk/test_yaml.py b/tests/unit/sdk/test_yaml.py index cbf1a637..3a73391d 100644 --- a/tests/unit/sdk/test_yaml.py +++ b/tests/unit/sdk/test_yaml.py @@ -8,7 +8,7 @@ def test_read_missing_file() -> None: file_name = "i_do_not_exist.yml" dir = here / "test_data" - full_path = dir / filename + full_path = dir / file_name yaml_file = YamlFile(location=full_path) yaml_file.load_content() assert not yaml_file.valid From 2b21e42a5770940d9f463fd1246840c0d33963ac Mon Sep 17 00:00:00 2001 From: Graham Noel Date: Wed, 4 Jun 2025 11:19:49 +0100 Subject: [PATCH 6/9] ruff formatting --- infrahub_sdk/utils.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/infrahub_sdk/utils.py b/infrahub_sdk/utils.py index d1d11606..08b8fbeb 100644 --- a/infrahub_sdk/utils.py +++ b/infrahub_sdk/utils.py @@ -358,7 +358,9 @@ def read_file(file_path: Path) -> str: with Path.open(file_path, encoding="utf-8") as fobj: return fobj.read() except UnicodeDecodeError as exc: - raise FileNotValidError(name=str(file_path.name), message=f"Unable to read {file_path.name} with utf-8 encoding") from exc + raise FileNotValidError( + name=str(file_path.name), message=f"Unable to read {file_path.name} with utf-8 encoding" + ) from exc def get_user_permissions(data: list[dict]) -> dict: From 8c77828feb46023917cf76f88d769284fbf5eebc Mon Sep 17 00:00:00 2001 From: Graham Noel Date: Wed, 4 Jun 2025 11:35:57 +0100 Subject: [PATCH 7/9] utils.py: read_file - use parent instead of cwd --- infrahub_sdk/utils.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/infrahub_sdk/utils.py b/infrahub_sdk/utils.py index 08b8fbeb..e0fc4737 100644 --- a/infrahub_sdk/utils.py +++ b/infrahub_sdk/utils.py @@ -353,7 +353,9 @@ def write_to_file(path: Path, value: Any) -> bool: def read_file(file_path: Path) -> str: if not file_path.is_file(): - raise FileNotValidError(name=str(file_path.name), message=f"{file_path.name}: not found at {file_path.cwd()}") + raise FileNotValidError( + name=str(file_path.name), message=f"{file_path.name}: not found at {file_path.parent}" + ) try: with Path.open(file_path, encoding="utf-8") as fobj: return fobj.read() From 19f4deb08eb78a534e6550b857ce38ae2a174f83 Mon Sep 17 00:00:00 2001 From: Graham Noel Date: Wed, 4 Jun 2025 11:38:39 +0100 Subject: [PATCH 8/9] Update test_yaml.py Update incorrect encoding test after read_file method changes. --- tests/unit/sdk/test_yaml.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/unit/sdk/test_yaml.py b/tests/unit/sdk/test_yaml.py index 3a73391d..3532265c 100644 --- a/tests/unit/sdk/test_yaml.py +++ b/tests/unit/sdk/test_yaml.py @@ -16,11 +16,12 @@ def test_read_missing_file() -> None: def test_read_incorrect_encoding() -> None: - file = here / "test_data/schema_encoding_error.yml" - yaml_file = YamlFile(location=file) + file_name = "schema_encoding_error.yml" + full_path = here / "test_data" / file_name + yaml_file = YamlFile(location=full_path) yaml_file.load_content() assert not yaml_file.valid - assert yaml_file.error_message == f"Unable to read {file} with utf-8 encoding" + assert yaml_file.error_message == f"Unable to read {file_name} with utf-8 encoding" def test_read_multiple_files() -> None: From 67cb607b0972d4f983bd5b2df21a79747873843b Mon Sep 17 00:00:00 2001 From: Graham Noel Date: Wed, 4 Jun 2025 11:41:26 +0100 Subject: [PATCH 9/9] ruff formatting --- infrahub_sdk/utils.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/infrahub_sdk/utils.py b/infrahub_sdk/utils.py index e0fc4737..b4ce22a4 100644 --- a/infrahub_sdk/utils.py +++ b/infrahub_sdk/utils.py @@ -353,9 +353,7 @@ def write_to_file(path: Path, value: Any) -> bool: def read_file(file_path: Path) -> str: if not file_path.is_file(): - raise FileNotValidError( - name=str(file_path.name), message=f"{file_path.name}: not found at {file_path.parent}" - ) + raise FileNotValidError(name=str(file_path.name), message=f"{file_path.name}: not found at {file_path.parent}") try: with Path.open(file_path, encoding="utf-8") as fobj: return fobj.read()