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

Ensure we return error if argument gives are not present #3467

Merged
merged 1 commit into from
May 20, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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 @@ -59,7 +59,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: 798
PYTEST_REQPASS: 799
steps:
- name: Activate WSL1
if: "contains(matrix.shell, 'wsl')"
Expand Down
21 changes: 13 additions & 8 deletions src/ansiblelint/_internal/load-failure.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@

"Linter failed to process a file, possible invalid file. Possible reasons:

* contains unsupported encoding (only UTF-8 is supported)
* not an Ansible file
* it contains some unsupported custom YAML objects (`!!` prefix)
* it was not able to decrypt an inline `!vault` block.

This violation **is not** skippable, so it cannot be added to the `warn_list`
or the `skip_list`. If a vault decryption issue cannot be avoided, the
offending file can be added to `exclude_paths` configuration.
- contains unsupported encoding (only UTF-8 is supported)
- not an Ansible file
- it contains some unsupported custom YAML objects (`!!` prefix)
- it was not able to decrypt an inline `!vault` block.

This violation **is not** skippable, so it cannot be added to the `warn_list` or
the `skip_list`. If a vault decryption issue cannot be avoided, the offending
file can be added to `exclude_paths` configuration.

Possible errors codes:

- `load-failure[not-found]` - Indicates that one argument file or folder was not
found on disk.
3 changes: 3 additions & 0 deletions src/ansiblelint/_internal/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ class LoadingFailureRule(BaseRule):
version_added = "v4.3.0"
_help = LOAD_FAILURE_MD
_order = 0
_ids = {
"load-failure[not-found]": "File not found",
}


class WarningRule(BaseRule):
Expand Down
4 changes: 3 additions & 1 deletion src/ansiblelint/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,9 @@ def __init__( # noqa: C901
if is_relative:
name = name.relative_to(name.cwd())
name = normpath_path(name)
self.path = name
# we need to be sure that we expanduser() because otherwise a simple
# test like .path.exists() will return unexpected results.
self.path = name.expanduser()
# Filename is effective file on disk, for stdin is a namedtempfile
self.name = self.filename = str(name)

Expand Down
10 changes: 10 additions & 0 deletions src/ansiblelint/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,16 @@ def _run(self) -> list[MatchError]: # noqa: C901
),
)
lintable.stop_processing = True
# identify missing files/folders
if not lintable.path.exists():
matches.append(
MatchError(
lintable=lintable,
message="File or found not found.",
ssbarnea marked this conversation as resolved.
Show resolved Hide resolved
rule=LoadingFailureRule(),
tag="load-failure[not-found]",
),
)

# -- phase 1 : syntax check in parallel --
def worker(lintable: Lintable) -> list[MatchError]:
Expand Down
1 change: 1 addition & 0 deletions src/ansiblelint/schemas/ansible-lint-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@
"latest",
"literal-compare",
"load-failure",
"load-failure[not-found]",
"loop-var-prefix",
"loop-var-prefix[missing]",
"loop-var-prefix[wrong]",
Expand Down
1 change: 1 addition & 0 deletions test/schemas/negative_test/.ansible-lint.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"latest",
"literal-compare",
"load-failure",
"load-failure[not-found]",
"loop-var-prefix",
"loop-var-prefix[missing]",
"loop-var-prefix[wrong]",
Expand Down
17 changes: 17 additions & 0 deletions test/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,20 @@ def test_files_not_scanned_twice(default_rules_collection: RulesCollection) -> N
# this second run should return 0 because the included filed was already
# processed and added to checked_files, which acts like a bypass list.
assert len(run2) == 0


def test_runner_not_found(default_rules_collection: RulesCollection) -> None:
"""Ensure that lintables aren't double-checked."""
checked_files: set[Lintable] = set()

filename = Path("this/folder/does/not/exist").resolve()
runner = Runner(
filename,
rules=default_rules_collection,
verbosity=0,
checked_files=checked_files,
)
result = runner.run()
assert len(runner.checked_files) == 1
assert len(result) == 1
assert result[0].tag == "load-failure[not-found]"