From eba53ae6158e1ada2991223e14739220835a2d95 Mon Sep 17 00:00:00 2001 From: Graham Noel Date: Wed, 4 Jun 2025 23:40:14 +0100 Subject: [PATCH 1/2] utils.py: update file not found exception message (#425) * 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. * utils.py: read_file - change argument name Clearer argument name and use of path object * repository.py: param signature change Downstream method changed signature: file_name --> file_path * Update test_yaml.py Fix test_missing_file after error message change * Update test_yaml.py Use correct variable name * ruff formatting * utils.py: read_file - use parent instead of cwd * Update test_yaml.py Update incorrect encoding test after read_file method changes. * ruff formatting --- infrahub_sdk/ctl/repository.py | 2 +- infrahub_sdk/utils.py | 12 +++++++----- tests/unit/sdk/test_yaml.py | 15 +++++++++------ 3 files changed, 17 insertions(+), 12 deletions(-) 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) diff --git a/infrahub_sdk/utils.py b/infrahub_sdk/utils.py index a5d6f28d..b4ce22a4 100644 --- a/infrahub_sdk/utils.py +++ b/infrahub_sdk/utils.py @@ -351,14 +351,16 @@ 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} is not a valid file") +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}") 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: diff --git a/tests/unit/sdk/test_yaml.py b/tests/unit/sdk/test_yaml.py index f596a088..3532265c 100644 --- a/tests/unit/sdk/test_yaml.py +++ b/tests/unit/sdk/test_yaml.py @@ -6,19 +6,22 @@ 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 / file_name + 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: - 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 cd80325265362996c66ff405351bcb8575d7ccce Mon Sep 17 00:00:00 2001 From: Patrick Ogenstad Date: Thu, 5 Jun 2025 09:26:01 +0200 Subject: [PATCH 2/2] Allow whenever above 0.7.2 and below 0.8.0 --- changelog/+26b92d23.housekeeping.md | 1 + poetry.lock | 2 +- pyproject.toml | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) create mode 100644 changelog/+26b92d23.housekeeping.md diff --git a/changelog/+26b92d23.housekeeping.md b/changelog/+26b92d23.housekeeping.md new file mode 100644 index 00000000..6a49d751 --- /dev/null +++ b/changelog/+26b92d23.housekeeping.md @@ -0,0 +1 @@ +Loosen pinned requirement for `whenever` to allow versions from 0.7.2 up to but not including 0.8.0. diff --git a/poetry.lock b/poetry.lock index 9477579f..25585102 100644 --- a/poetry.lock +++ b/poetry.lock @@ -2394,4 +2394,4 @@ tests = ["Jinja2", "pytest", "pyyaml", "rich"] [metadata] lock-version = "2.1" python-versions = "^3.9, <3.14" -content-hash = "e5bec17b74d706a51f41c3a637a7c12079c3550866be6f81cca9314ec1eeb9e6" +content-hash = "8fb3db8c3b2045247ef681d483005c7f3d19579fb9f982d9595a3ec7b94f8b24" diff --git a/pyproject.toml b/pyproject.toml index 5b4219ad..73541708 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -42,7 +42,7 @@ pytest = { version = "*", optional = true } pyyaml = { version = "^6", optional = true } eval-type-backport = { version = "^0.2.2", python = "~3.9" } dulwich = "^0.21.4" -whenever = "0.7.2" +whenever = ">=0.7.2,<0.8.0" netutils = "^1.0.0" click = { version = "8.1.*", optional = true }