Skip to content

Commit

Permalink
Allow igores from test/sanity/ignores.txt
Browse files Browse the repository at this point in the history
Fixes: #1584
  • Loading branch information
ssbarnea committed Feb 9, 2023
1 parent 26a12aa commit 16c0936
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 4 deletions.
5 changes: 4 additions & 1 deletion .ansible-lint
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ use_default_rules: true
# rulesdir:
# - ./rule/directory/

# Ansible-lint completely ignores rules or tags listed below
# Ansible-lint is able to recognize and load skip rules stored inside
# `tests/sanity/ignore.txt` files (same as ansible-test). To skip
# a rule just enter filename and tag, like "playbook.yml package-latest" on a
# new line. Optionally you can add comments after the tag, prefixed by "#".
skip_list:
- skip_this_tag

Expand Down
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
12 changes: 12 additions & 0 deletions docs/configuring.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ counterparts:
--8<-- ".ansible-lint"
```

## Ignoring rules for entire files

Ansible-lint will load skip rules from `tests/sanity/ignore.txt` file, if one is
detected, the same as
[ansible-test](https://docs.ansible.com/ansible/latest/dev_guide/testing/sanity/ignores.html).

```yaml title="tests/sanity/ignore.txt"
# this is just a comment
playbook.yml package-latest # disable package-latest rule for playbook.yml
playbook.yml deprecated-module
```

## Pre-commit setup

To use Ansible-lint with [pre-commit], add the following to the
Expand Down
3 changes: 3 additions & 0 deletions examples/tests/sanity/ignore.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
playbook2.yml package-latest # comment
playbook2.yml foo-bar
# commented line
10 changes: 9 additions & 1 deletion src/ansiblelint/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
from ansiblelint.config import get_version_warning, options
from ansiblelint.constants import EXIT_CONTROL_C_RC, GIT_CMD, LOCK_TIMEOUT_RC
from ansiblelint.file_utils import abspath, cwd, normpath
from ansiblelint.loaders import load_ignore_txt
from ansiblelint.skip_utils import normalize_tag
from ansiblelint.version import __version__

Expand Down Expand Up @@ -283,7 +284,14 @@ def main(argv: list[str] | None = None) -> int: # noqa: C901
mark_as_success = False

# Remove skipped list items from the result
result.matches = [m for m in result.matches if m.tag not in app.options.skip_list]
ignore_map = load_ignore_txt()
result.matches = [
m
for m in result.matches
if m.tag not in app.options.skip_list and m.tag not in ignore_map[m.filename]
]

# Remove the skips from ignore.txt file

app.render_matches(result.matches)

Expand Down
32 changes: 31 additions & 1 deletion src/ansiblelint/loaders.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
"""Utilities for loading various files."""
from __future__ import annotations

import logging
import os
from collections import defaultdict
from functools import partial
from pathlib import Path
from typing import Any
Expand All @@ -14,8 +17,10 @@
except (ImportError, AttributeError):
from yaml import FullLoader, SafeLoader # type: ignore

IGNORE_TXT = "tests/sanity/ignore.txt"
yaml_load = partial(yaml.load, Loader=FullLoader)
yaml_load_safe = partial(yaml.load, Loader=SafeLoader)
_logger = logging.getLogger(__name__)


def yaml_from_file(filepath: str | Path) -> Any:
Expand All @@ -24,4 +29,29 @@ def yaml_from_file(filepath: str | Path) -> Any:
return yaml_load(content)


__all__ = ["yaml_from_file", "yaml_load", "yaml_load_safe", "YAMLError"]
def load_ignore_txt(filepath: str | Path = IGNORE_TXT) -> dict[str, set[str]]:
"""Return a list of rules to ignore."""
result = defaultdict(set)
if os.path.isfile(filepath):
with open(str(filepath), encoding="utf-8") as content:
_logger.debug("Loading ignores from %s", filepath)
for line in content:
entry = line.split("#")[0].rstrip()
if entry:
try:
path, rule = entry.split()
except ValueError as exc:
raise RuntimeError(
f"Unable to parse line '{line}' from {filepath} file."
) from exc
result[path].add(rule)
return result


__all__ = [
"load_ignore_txt",
"yaml_from_file",
"yaml_load",
"yaml_load_safe",
"YAMLError",
]
8 changes: 8 additions & 0 deletions test/test_loaders.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"""Tests for loaders submodule."""
from ansiblelint.loaders import load_ignore_txt


def test_load_ignore_txt() -> None:
"""Test load_ignore_txt."""
result = load_ignore_txt("examples/tests/sanity/ignore.txt")
assert result == {"playbook2.yml": {"foo-bar", "package-latest"}}

0 comments on commit 16c0936

Please sign in to comment.