Skip to content

Commit eba53ae

Browse files
authored
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
1 parent 80b50f4 commit eba53ae

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

infrahub_sdk/ctl/repository.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def get_repository_config(repo_config_file: Path) -> InfrahubRepositoryConfig:
4545

4646

4747
def load_repository_config_file(repo_config_file: Path) -> dict:
48-
yaml_data = read_file(file_name=repo_config_file)
48+
yaml_data = read_file(file_path=repo_config_file)
4949

5050
try:
5151
data = yaml.safe_load(yaml_data)

infrahub_sdk/utils.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -351,14 +351,16 @@ def write_to_file(path: Path, value: Any) -> bool:
351351
return written is not None
352352

353353

354-
def read_file(file_name: Path) -> str:
355-
if not file_name.is_file():
356-
raise FileNotValidError(name=str(file_name), message=f"{file_name} is not a valid file")
354+
def read_file(file_path: Path) -> str:
355+
if not file_path.is_file():
356+
raise FileNotValidError(name=str(file_path.name), message=f"{file_path.name}: not found at {file_path.parent}")
357357
try:
358-
with Path.open(file_name, encoding="utf-8") as fobj:
358+
with Path.open(file_path, encoding="utf-8") as fobj:
359359
return fobj.read()
360360
except UnicodeDecodeError as exc:
361-
raise FileNotValidError(name=str(file_name), message=f"Unable to read {file_name} with utf-8 encoding") from exc
361+
raise FileNotValidError(
362+
name=str(file_path.name), message=f"Unable to read {file_path.name} with utf-8 encoding"
363+
) from exc
362364

363365

364366
def get_user_permissions(data: list[dict]) -> dict:

tests/unit/sdk/test_yaml.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,22 @@
66

77

88
def test_read_missing_file() -> None:
9-
file = here / "test_data/i_do_not_exist.yml"
10-
yaml_file = YamlFile(location=file)
9+
file_name = "i_do_not_exist.yml"
10+
dir = here / "test_data"
11+
full_path = dir / file_name
12+
yaml_file = YamlFile(location=full_path)
1113
yaml_file.load_content()
1214
assert not yaml_file.valid
13-
assert yaml_file.error_message == f"{file} is not a valid file"
15+
assert yaml_file.error_message == f"{file_name}: not found at {dir}"
1416

1517

1618
def test_read_incorrect_encoding() -> None:
17-
file = here / "test_data/schema_encoding_error.yml"
18-
yaml_file = YamlFile(location=file)
19+
file_name = "schema_encoding_error.yml"
20+
full_path = here / "test_data" / file_name
21+
yaml_file = YamlFile(location=full_path)
1922
yaml_file.load_content()
2023
assert not yaml_file.valid
21-
assert yaml_file.error_message == f"Unable to read {file} with utf-8 encoding"
24+
assert yaml_file.error_message == f"Unable to read {file_name} with utf-8 encoding"
2225

2326

2427
def test_read_multiple_files() -> None:

0 commit comments

Comments
 (0)