Skip to content

Commit 1b1b403

Browse files
committed
Fix tests for invalid files
1 parent 9a26a24 commit 1b1b403

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

infrahub_sdk/yaml.py

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,28 @@ def load_content(self) -> None:
6060
def validate_content(self) -> None:
6161
pass
6262

63+
@classmethod
64+
def init(cls, location: Path, multiple_documents: bool, content: dict | None) -> Self:
65+
if not content:
66+
return cls._file_is_empty(path=location, has_multiple_document=multiple_documents)
67+
68+
return cls(location=location, multiple_documents=multiple_documents, content=content)
69+
70+
@classmethod
71+
def _file_is_empty(cls, path: Path, has_multiple_document: bool) -> Self:
72+
return cls(
73+
location=path, multiple_documents=has_multiple_document, error_message="Invalid YAML/JSON file", valid=False
74+
)
75+
76+
@classmethod
77+
def _file_is_invalid(cls, path: Path, has_multiple_document: bool) -> Self:
78+
return cls(
79+
location=path,
80+
multiple_documents=has_multiple_document,
81+
error_message="Invalid YAML/JSON file",
82+
valid=False,
83+
)
84+
6385
@classmethod
6486
def load_file_from_disk(cls, path: Path) -> list[Self]:
6587
yaml_files: list[Self] = []
@@ -71,24 +93,23 @@ def load_file_from_disk(cls, path: Path) -> list[Self]:
7193

7294
if has_multiple_document:
7395
for content in yaml.safe_load_all(file_content):
74-
yaml_files.append(cls(location=path, multiple_documents=has_multiple_document, content=content))
96+
yaml_files.append(
97+
cls.init(location=path, multiple_documents=has_multiple_document, content=content)
98+
)
99+
75100
else:
76101
yaml_files.append(
77-
cls(location=path, multiple_documents=has_multiple_document, content=yaml.safe_load(file_content))
102+
cls.init(
103+
location=path, multiple_documents=has_multiple_document, content=yaml.safe_load(file_content)
104+
)
78105
)
106+
79107
except FileNotValidError as exc:
80108
yaml_files.append(
81109
cls(location=path, multiple_documents=has_multiple_document, error_message=exc.message, valid=False)
82110
)
83111
except (yaml.YAMLError, ParserError):
84-
yaml_files.append(
85-
cls(
86-
location=path,
87-
multiple_documents=has_multiple_document,
88-
error_message="Invalid YAML/JSON file",
89-
valid=False,
90-
)
91-
)
112+
yaml_files.append(cls._file_is_invalid(path, has_multiple_document))
92113

93114
if has_multiple_document:
94115
for idx, file in enumerate(yaml_files):

tests/unit/ctl/test_schema_app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def test_schema_load_empty(httpx_mock: HTTPXMock):
1414
result = runner.invoke(app=app, args=["load", str(fixture_file)])
1515

1616
assert result.exit_code == 1
17-
assert "Empty YAML/JSON file" in result.stdout
17+
assert "Invalid YAML/JSON file" in result.stdout
1818

1919

2020
def test_schema_load_one_valid(httpx_mock: HTTPXMock):

tests/unit/ctl/test_validate_app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def test_validate_schema_empty():
2121

2222
result = runner.invoke(app=app, args=["schema", str(fixture_file)])
2323
assert result.exit_code == 1
24-
assert "Empty YAML/JSON file" in remove_ansi_color(result.stdout)
24+
assert "Invalid YAML/JSON file" in remove_ansi_color(result.stdout)
2525

2626

2727
def test_validate_schema_non_valid():

0 commit comments

Comments
 (0)