Skip to content

Commit

Permalink
Merge branch 'main' of github.com:ansible/ansible-lint into fix/jinja…
Browse files Browse the repository at this point in the history
…-spacing
  • Loading branch information
audgirka committed Aug 31, 2023
2 parents 726b59c + f674472 commit 5c482d9
Show file tree
Hide file tree
Showing 33 changed files with 215 additions and 106 deletions.
6 changes: 3 additions & 3 deletions .config/requirements-lock.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
# pip-compile --no-annotate --output-file=.config/requirements-lock.txt --strip-extras --unsafe-package=resolvelib --unsafe-package=ruamel-yaml-clib pyproject.toml
#
ansible-compat==4.1.7
ansible-compat==4.1.8
ansible-core==2.15.3
attrs==23.1.0
black==23.7.0
Expand All @@ -14,7 +14,7 @@ cffi==1.15.1
charset-normalizer==3.2.0
click==8.1.7
cryptography==41.0.3
filelock==3.12.2
filelock==3.12.3
idna==3.4
importlib-resources==5.0.7
jinja2==3.1.2
Expand All @@ -33,7 +33,7 @@ pyyaml==6.0.1
referencing==0.30.2
requests==2.31.0
rich==13.5.2
rpds-py==0.9.2
rpds-py==0.10.0
ruamel-yaml==0.17.32
subprocess-tee==0.4.1
tomli==2.0.1
Expand Down
6 changes: 3 additions & 3 deletions .config/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
# pip-compile --extra=docs --extra=test --no-annotate --output-file=.config/requirements.txt --strip-extras --unsafe-package=resolvelib --unsafe-package=ruamel-yaml-clib pyproject.toml
#
ansible-compat==4.1.7
ansible-compat==4.1.8
ansible-core==2.15.3
astroid==2.15.6
attrs==23.1.0
Expand Down Expand Up @@ -70,7 +70,7 @@ pathspec==0.11.2
pillow==9.5.0
pipdeptree==2.7.1
platformdirs==3.10.0
pluggy==1.2.0
pluggy==1.3.0
ply==3.11
psutil==5.9.5
pycparser==2.21
Expand All @@ -91,7 +91,7 @@ referencing==0.30.2
regex==2023.5.5
requests==2.31.0
rich==13.5.2
rpds-py==0.9.2
rpds-py==0.10.0
ruamel-yaml==0.17.32
six==1.16.0
soupsieve==2.4.1
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,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: 814
PYTEST_REQPASS: 817
steps:
- name: Activate WSL1
if: "contains(matrix.shell, 'wsl')"
Expand Down
8 changes: 4 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ repos:
- id: check-useless-excludes
- repo: https://github.com/pre-commit/mirrors-prettier
# keep it before yamllint
rev: v3.0.2
rev: v3.0.3
hooks:
- id: prettier
# Temporary excludes so we can gradually normalize the formatting
Expand Down Expand Up @@ -63,14 +63,14 @@ repos:
- prettier-plugin-toml
- prettier-plugin-sort-json
- repo: https://github.com/streetsidesoftware/cspell-cli
rev: v7.0.0
rev: v7.2.0
hooks:
- id: cspell
# entry: codespell --relative
args: [--relative, --no-progress, --no-summary]
name: Spell check with cspell
- repo: https://github.com/python-jsonschema/check-jsonschema
rev: 0.24.1
rev: 0.26.3
hooks:
- id: check-github-workflows
- repo: https://github.com/pre-commit/pre-commit-hooks.git
Expand Down Expand Up @@ -130,7 +130,7 @@ repos:
types: [file, yaml]
entry: yamllint --strict
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: "v0.0.285"
rev: "v0.0.286"
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
Expand Down
1 change: 0 additions & 1 deletion .readthedocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ build:
- pip install --user tox
- python3 -m tox -e docs -- --strict --site-dir=_readthedocs/html/
python:
system_packages: false
install:
- method: pip
path: tox
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
- name: Demo
hosts: all
tasks:
- name: Demo
ansible.builtin.debug:
msg: |2
multi
line
message
10 changes: 10 additions & 0 deletions examples/playbooks/transform-block-indentation-indicator.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
- name: Demo
hosts: all
tasks:
- name: Demo
ansible.builtin.debug:
msg: |3
multi
line
message
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,15 @@
ansible.builtin.debug:
var: test_var
register: CamelCaseIsBad # invalid 7

- name: This should not trigger due to role name being dynamic (jinja)
ansible.builtin.include_role:
name: "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') | basename }}"
vars:
nginx_logrotate_conf_enable: true

- name: This should not trigger due to containing a dot in role name
ansible.builtin.include_role:
name: "foo.bar"
vars:
bar_foo: true
9 changes: 9 additions & 0 deletions examples/yamllint/.github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
name: ack
on: # <-- this is invalid by YAML 1.2 spec as it loads as true boolean.
pull_request_target:
types: [opened, labeled, unlabeled, synchronize]

jobs:
ack:
uses: ansible/devtools/.github/workflows/ack.yml@main
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ python_files = [
xfail_strict = true

[tool.ruff]
required-version = "0.0.285"
required-version = "0.0.286"
ignore = [
"E501", # we use black
"ERA001", # auto-removal of commented out code affects development and vscode integration
Expand Down
2 changes: 1 addition & 1 deletion src/ansiblelint/rules/name.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def transform(

if "pytest" in sys.modules:
from ansiblelint.config import options
from ansiblelint.file_utils import Lintable # noqa: F811
from ansiblelint.file_utils import Lintable
from ansiblelint.rules import RulesCollection
from ansiblelint.runner import Runner

Expand Down
43 changes: 30 additions & 13 deletions src/ansiblelint/rules/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import annotations

import logging
import re
import sys
from typing import TYPE_CHECKING, Any

Expand Down Expand Up @@ -193,25 +194,27 @@ def matchyaml(self, file: Lintable) -> list[MatchError]:
pytest.param(
"examples/.collection/galaxy.yml",
"galaxy",
["'GPL' is not one of"],
[r"^'GPL' is not one of.*https://"],
id="galaxy",
),
pytest.param(
"examples/roles/invalid_requirements_schema/meta/requirements.yml",
"requirements",
["{'foo': 'bar'} is not valid under any of the given schemas"],
[
r"^{'foo': 'bar'} is not valid under any of the given schemas.*https://",
],
id="requirements",
),
pytest.param(
"examples/roles/invalid_meta_schema/meta/main.yml",
"meta",
["False is not of type 'string'"],
[r"^False is not of type 'string'.*https://"],
id="meta",
),
pytest.param(
"examples/playbooks/vars/invalid_vars_schema.yml",
"vars",
["'123' does not match any of the regexes"],
[r"^'123' does not match any of the regexes.*https://"],
id="vars",
),
pytest.param(
Expand All @@ -223,14 +226,18 @@ def matchyaml(self, file: Lintable) -> list[MatchError]:
pytest.param(
"examples/ee_broken/execution-environment.yml",
"execution-environment",
["{'foo': 'bar'} is not valid under any of the given schemas"],
[
r"^{'foo': 'bar'} is not valid under any of the given schemas.*https://",
],
id="execution-environment-broken",
),
("examples/meta/runtime.yml", "meta-runtime", []),
pytest.param(
"examples/broken_collection_meta_runtime/meta/runtime.yml",
"meta-runtime",
["Additional properties are not allowed ('foo' was unexpected)"],
[
r"^Additional properties are not allowed \('foo' was unexpected\).*https://",
],
id="meta-runtime-broken",
),
pytest.param(
Expand All @@ -242,7 +249,9 @@ def matchyaml(self, file: Lintable) -> list[MatchError]:
pytest.param(
"examples/inventory/broken_dev_inventory.yml",
"inventory",
["Additional properties are not allowed ('foo' was unexpected)"],
[
r"^Additional properties are not allowed \('foo' was unexpected\).*https://",
],
id="inventory-broken",
),
pytest.param(
Expand All @@ -260,7 +269,9 @@ def matchyaml(self, file: Lintable) -> list[MatchError]:
pytest.param(
"examples/broken/.ansible-lint",
"ansible-lint-config",
["Additional properties are not allowed ('foo' was unexpected)"],
[
r"^Additional properties are not allowed \('foo' was unexpected\).*https://",
],
id="ansible-lint-config-broken",
),
pytest.param(
Expand All @@ -272,7 +283,9 @@ def matchyaml(self, file: Lintable) -> list[MatchError]:
pytest.param(
"examples/broken/ansible-navigator.yml",
"ansible-navigator-config",
["Additional properties are not allowed ('ansible' was unexpected)"],
[
r"^Additional properties are not allowed \('ansible' was unexpected\).*https://",
],
id="ansible-navigator-config-broken",
),
pytest.param(
Expand All @@ -284,20 +297,24 @@ def matchyaml(self, file: Lintable) -> list[MatchError]:
pytest.param(
"examples/roles/broken_argument_specs/meta/argument_specs.yml",
"role-arg-spec",
["Additional properties are not allowed ('foo' was unexpected)"],
[
r"^Additional properties are not allowed \('foo' was unexpected\).*https://",
],
id="role-arg-spec-broken",
),
pytest.param(
"examples/changelogs/changelog.yaml",
"changelog",
["Additional properties are not allowed ('foo' was unexpected)"],
[
r"^Additional properties are not allowed \('foo' was unexpected\).*https://",
],
id="changelog",
),
pytest.param(
"examples/rulebooks/rulebook-fail.yml",
"rulebook",
[
"Additional properties are not allowed ('that_should_not_be_here' was unexpected)",
r"^Additional properties are not allowed \('that_should_not_be_here' was unexpected\).*https://",
],
id="rulebook",
),
Expand Down Expand Up @@ -336,7 +353,7 @@ def test_schema(file: str, expected_kind: str, expected: list[str]) -> None:
assert len(results) == len(expected), results
for idx, result in enumerate(results):
assert result.filename.endswith(file)
assert expected[idx] in result.message
assert re.match(expected[idx], result.message)
assert result.tag == f"schema[{expected_kind}]"

@pytest.mark.parametrize(
Expand Down
16 changes: 13 additions & 3 deletions src/ansiblelint/rules/var_naming.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from ansiblelint.rules import AnsibleLintRule, RulesCollection
from ansiblelint.runner import Runner
from ansiblelint.skip_utils import get_rule_skips_from_line
from ansiblelint.text import has_jinja, is_fqcn_or_name
from ansiblelint.utils import parse_yaml_from_file

if TYPE_CHECKING:
Expand Down Expand Up @@ -160,10 +161,15 @@ def get_var_naming_matcherror(
rule=self,
)

if prefix and not ident.startswith(f"{prefix}_"):
if (
prefix
and not ident.startswith(f"{prefix}_")
and not has_jinja(prefix)
and is_fqcn_or_name(prefix)
):
return MatchError(
tag="var-naming[no-role-prefix]",
message="Variables names from within roles should use role_name_ as a prefix.",
message=f"Variables names from within roles should use {prefix}_ as a prefix.",
rule=self,
)
return None
Expand Down Expand Up @@ -298,7 +304,11 @@ def matchyaml(self, file: Lintable) -> list[MatchError]:
@pytest.mark.parametrize(
("file", "expected"),
(
pytest.param("examples/playbooks/rule-var-naming-fail.yml", 7, id="0"),
pytest.param(
"examples/playbooks/var-naming/rule-var-naming-fail.yml",
7,
id="0",
),
pytest.param("examples/Taskfile.yml", 0, id="1"),
),
)
Expand Down
12 changes: 12 additions & 0 deletions src/ansiblelint/rules/yaml_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ def matchyaml(self, file: Lintable) -> list[MatchError]:
self.severity = "VERY_LOW"
if problem.level == "error":
self.severity = "MEDIUM"
# Ignore truthy violation with github workflows ("on:" keys)
if problem.rule == "truthy" and file.path.parent.parts[-2:] == (
".github",
"workflows",
):
continue
matches.append(
self.create_matcherror(
# yamllint does return lower-case sentences
Expand Down Expand Up @@ -180,6 +186,12 @@ def _fetch_skips(data: Any, collector: dict[int, set[str]]) -> dict[int, set[str
[],
id="rule-yaml-pass",
),
pytest.param(
"examples/yamllint/.github/workflows/ci.yml",
"yaml",
[],
id="rule-yaml-github-workflow",
),
),
)
@pytest.mark.filterwarnings("ignore::ansible_compat.runtime.AnsibleWarning")
Expand Down
Loading

0 comments on commit 5c482d9

Please sign in to comment.