Skip to content

Commit

Permalink
Linting fixes for command_instead_of* rules (ruff:D102) (#4252)
Browse files Browse the repository at this point in the history
  • Loading branch information
alisonlhart authored Jul 10, 2024
1 parent 22041b4 commit 95382d3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/ansiblelint/rules/command_instead_of_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ def matchtask(
task: Task,
file: Lintable | None = None,
) -> bool | str:
"""Check if a command is used instead of an appropriate module.
:param task: Task to check for shell usage
:param file: File to lint
:returns: False if command module isn't used, or a string showing the command used
"""
if task["action"]["__ansible_module__"] not in self._commands:
return False

Expand Down Expand Up @@ -139,7 +145,12 @@ def test_command_instead_of_module(
file: str,
expected: int,
) -> None:
"""Validate that rule works as intended."""
"""Validate that rule works as intended.
:param default_rules_collection: Default rules for testing
:param file: Test file to check for violations
:expected: Expected number of errors
"""
results = Runner(file, rules=default_rules_collection).run()

for result in results:
Expand Down
19 changes: 18 additions & 1 deletion src/ansiblelint/rules/command_instead_of_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ def matchtask(
task: Task,
file: Lintable | None = None,
) -> bool | str:
"""Check if a shell module is used instead of an appropriate command.
:param task: Task to check for shell usage
:param file: File to lint
:returns: False if shell module isn't used, or string output of where it is used
"""
# Use unjinja so that we don't match on jinja filters
# rather than pipes
if task["action"]["__ansible_module__"] in ["shell", "ansible.builtin.shell"]:
Expand All @@ -72,6 +78,12 @@ def transform(
lintable: Lintable,
data: CommentedMap | CommentedSeq | str,
) -> None:
"""Transform the data.
:param match: The match to transform.
:param lintable: The file to transform.
:param data: The data to transform.
"""
if match.tag == "command-instead-of-shell":
target_task = self.seek(match.yaml_path, data)
for _ in range(len(target_task)):
Expand Down Expand Up @@ -108,7 +120,12 @@ def test_rule_command_instead_of_shell(
file: str,
expected: int,
) -> None:
"""Validate that rule works as intended."""
"""Validate that rule works as intended.
:param default_rules_collection: Default rules for testing
:param file: Test file to check for violations
:expected: Expected number of errors
"""
results = Runner(file, rules=default_rules_collection).run()
for result in results:
assert result.rule.id == UseCommandInsteadOfShellRule.id, result
Expand Down

0 comments on commit 95382d3

Please sign in to comment.