Skip to content

Commit

Permalink
Avoid erroring with empty vars files (#2310)
Browse files Browse the repository at this point in the history
- Empty YAML files should not longer produce errors
- We now made the broad rule exception catching to warning instead of
  debug, so we make it less likely to miss these, especially as they
  are almost always sing of a corner-case that we did not cover.
  • Loading branch information
ssbarnea committed Aug 20, 2022
1 parent 1cefe54 commit 7ad2aaf
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ jobs:
WSLENV: FORCE_COLOR:PYTEST_REQPASS:TOXENV:TOX_PARALLEL_NO_SPINNER
# Number of expected test passes, safety measure for accidental skip of
# tests. Update value if you add/remove tests.
PYTEST_REQPASS: 661
PYTEST_REQPASS: 660

steps:
- name: Activate WSL1
Expand Down
2 changes: 1 addition & 1 deletion src/ansiblelint/_internal/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def getmatches(self, file: Lintable) -> List[MatchError]:
try:
matches.extend(method(file))
except Exception as exc: # pylint: disable=broad-except
_logger.debug(
_logger.warning(
"Ignored exception from %s.%s: %s",
self.__class__.__name__,
method,
Expand Down
4 changes: 3 additions & 1 deletion src/ansiblelint/rules/var_naming.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ def matchplay(self, file: Lintable, data: odict[str, Any]) -> List[MatchError]:
results: List[MatchError] = []
raw_results: List[MatchError] = []

if not data:
return results
# If the Play uses the 'vars' section to set variables
our_vars = data.get("vars", {})
for key in our_vars.keys():
Expand Down Expand Up @@ -161,7 +163,7 @@ def matchyaml(self, file: Lintable) -> List[MatchError]:
raw_results: List[MatchError] = []
meta_data: Dict[AnsibleUnicode, Any] = {}

if str(file.kind) == "vars":
if str(file.kind) == "vars" and file.data:
meta_data = parse_yaml_from_file(str(file.path))
for key in meta_data.keys():
if self.is_invalid_variable_name(key):
Expand Down
4 changes: 4 additions & 0 deletions src/ansiblelint/yaml_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ def nested_items_path(
:returns: each iteration yields the key (of the parent dict) or the index (lists)
"""
# As typing and mypy cannot effectively ensure we are called only with
# valid data, we better ignore NoneType
if data_collection is None:
return
yield from _nested_items_path(data_collection=data_collection, parent_path=[])


Expand Down
2 changes: 1 addition & 1 deletion test/test_yaml_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ def test_nested_items_path() -> None:
"string",
42,
1.234,
None,
("tuple",),
{"set"},
# NoneType is no longer include, as we assume we have to ignore it
),
)
def test_nested_items_path_raises_typeerror(invalid_data_input: Any) -> None:
Expand Down

0 comments on commit 7ad2aaf

Please sign in to comment.