Skip to content
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

Fix playbook/rulebook detection #3005

Merged
merged 1 commit into from
Feb 10, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ jobs:
WSLENV: FORCE_COLOR:PYTEST_REQPASS:TOXENV:GITHUB_STEP_SUMMARY
# Number of expected test passes, safety measure for accidental skip of
# tests. Update value if you add/remove tests.
PYTEST_REQPASS: 781
PYTEST_REQPASS: 782

steps:
- name: Activate WSL1
Expand Down
7 changes: 6 additions & 1 deletion src/ansiblelint/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def __init__(
self.abspath = self.path.expanduser().absolute()

if self.kind == "yaml":
self._guess_kind()
self.data # pylint: disable=pointless-statement

def _guess_kind(self) -> None:
if self.kind == "yaml":
Expand Down Expand Up @@ -371,6 +371,11 @@ def data(self) -> Any:
)

self._data = parse_yaml_linenumbers(self)
# now that _data is not empty, we can try guessing if playbook or rulebook
# it has to be done before append_skipped_rules() call as it's relying
# on self.kind.
if self.kind == "yaml":
self._guess_kind()
# Lazy import to avoid delays and cyclic-imports
if "append_skipped_rules" not in globals():
# pylint: disable=import-outside-toplevel
Expand Down
8 changes: 4 additions & 4 deletions src/ansiblelint/testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ def run(self, filename: str) -> list[MatchError]:
"""Lints received filename."""
return self._call_runner(filename)

def run_playbook(self, playbook_text: str) -> list[MatchError]:
def run_playbook(
self, playbook_text: str, prefix: str = "playbook"
) -> list[MatchError]:
"""Lints received text as a playbook."""
with tempfile.NamedTemporaryFile(
mode="w", suffix=".yml", prefix="playbook"
) as fh:
with tempfile.NamedTemporaryFile(mode="w", suffix=".yml", prefix=prefix) as fh:
fh.write(playbook_text)
fh.flush()
results = self._call_runner(fh.name)
Expand Down
7 changes: 7 additions & 0 deletions test/test_skiputils.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ def test_playbook_noqa(default_text_runner: RunFromText) -> None:
assert len(results) == 1


def test_playbook_noqa2(default_text_runner: RunFromText) -> None:
"""Check that noqa is properly taken into account on vars and tasks."""
results = default_text_runner.run_playbook(PLAYBOOK_WITH_NOQA, "test")
# Should raise error at "SOME_VAR".
assert len(results) == 1


@pytest.mark.parametrize(
("lintable", "yaml", "expected_form"),
(
Expand Down