Skip to content

utils.py: update file not found exception message #425

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion infrahub_sdk/ctl/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 7 additions & 5 deletions infrahub_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
15 changes: 9 additions & 6 deletions tests/unit/sdk/test_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down