Skip to content

Commit

Permalink
Upgraded linters (#2898)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea authored Jan 17, 2023
1 parent fd71b3f commit 024e8bf
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 57 deletions.
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ repos:
args: [--relative, --no-progress, --no-summary]
name: Spell check with cspell
- repo: https://github.com/sirosen/check-jsonschema
rev: 0.19.2
rev: 0.20.0
hooks:
- id: check-github-workflows
- repo: https://github.com/pre-commit/pre-commit-hooks.git
Expand Down Expand Up @@ -112,7 +112,7 @@ repos:
hooks:
- id: doc8
- repo: https://github.com/adrienverge/yamllint.git
rev: v1.28.0
rev: v1.29.0
hooks:
- id: yamllint
exclude: >
Expand Down Expand Up @@ -181,7 +181,7 @@ repos:
plugins/.*
)$
- repo: https://github.com/pycqa/pylint
rev: v2.15.9
rev: v2.16.0b0
hooks:
- id: pylint
args:
Expand Down
10 changes: 5 additions & 5 deletions plugins/modules/fake_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
def main() -> None:
"""Return the module instance."""
return AnsibleModule(
argument_spec=dict(
data=dict(default=None),
path=dict(default=None, type=str),
file=dict(default=None, type=str),
)
argument_spec={
"data": {"default": None},
"path": {"default": None},
"file": {"default": None},
}
)
19 changes: 10 additions & 9 deletions src/ansiblelint/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ def play_children(
v = template(
os.path.abspath(basedir),
v,
dict(playbook_dir=PLAYBOOK_DIR or os.path.abspath(basedir)),
{"playbook_dir": PLAYBOOK_DIR or os.path.abspath(basedir)},
fail_on_undefined=False,
)
return delegate_map[k](basedir, k, v, parent_type)
Expand Down Expand Up @@ -523,7 +523,7 @@ def _look_for_role_files(

def _kv_to_dict(v: str) -> dict[str, Any]:
(command, args, kwargs) = tokenize(v)
return dict(__ansible_module__=command, __ansible_arguments__=args, **kwargs)
return {"__ansible_module__": command, "__ansible_arguments__": args, **kwargs}


def _sanitize_task(task: dict[str, Any]) -> dict[str, Any]:
Expand Down Expand Up @@ -564,10 +564,10 @@ def normalize_task_v2(task: dict[str, Any]) -> dict[str, Any]:
if is_nested_task(task):
_extract_ansible_parsed_keys_from_task(result, task, ansible_parsed_keys)
# Add dummy action for block/always/rescue statements
result["action"] = dict(
__ansible_module__="block/always/rescue",
__ansible_module_original__="block/always/rescue",
)
result["action"] = {
"__ansible_module__": "block/always/rescue",
"__ansible_module_original__": "block/always/rescue",
}

return result

Expand Down Expand Up @@ -604,9 +604,10 @@ def normalize_task_v2(task: dict[str, Any]) -> dict[str, Any]:
# the opposite. Mainly we currently consider normalized the module listing
# used by `ansible-doc -t module -l 2>/dev/null`
action = removeprefix(action, "ansible.builtin.")
result["action"] = dict(
__ansible_module__=action, __ansible_module_original__=action_unnormalized
)
result["action"] = {
"__ansible_module__": action,
"__ansible_module_original__": action_unnormalized,
}

if "_raw_params" in arguments:
# Doing a split here is really bad as it would break jinja2 templating
Expand Down
2 changes: 1 addition & 1 deletion test/test_ansiblelintrule.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_unjinja() -> None:
assert AnsibleLintRule.unjinja(text) == output


@pytest.mark.parametrize("rule_config", ({}, dict(foo=True, bar=1)))
@pytest.mark.parametrize("rule_config", ({}, {"foo": True, "bar": 1}))
def test_rule_config(rule_config: dict[str, Any], monkeypatch: MonkeyPatch) -> None:
"""Check that a rule config is inherited from options."""
rule_id = "rule-0"
Expand Down
2 changes: 1 addition & 1 deletion test/test_matcherrror.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def test_matcherror_invalid() -> None:
r"^MatchError\(\) missing a required argument: one of 'message' or 'rule'$"
)
with pytest.raises(TypeError, match=expected_err):
MatchError()
raise MatchError()


@pytest.mark.parametrize(
Expand Down
20 changes: 10 additions & 10 deletions test/test_skiputils.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,31 +189,31 @@ def test_append_skipped_rules(
("task", "expected"),
(
pytest.param(
dict(
name="ensure apache is at the latest version",
yum={"name": "httpd", "state": "latest"},
),
{
"name": "ensure apache is at the latest version",
"yum": {"name": "httpd", "state": "latest"},
},
False,
),
pytest.param(
dict(
name="Attempt and graceful roll back",
block=[
{
"name": "Attempt and graceful roll back",
"block": [
{"name": "Force a failure", "ansible.builtin.command": "/bin/false"}
],
rescue=[
"rescue": [
{
"name": "Force a failure in middle of recovery!",
"ansible.builtin.command": "/bin/false",
}
],
always=[
"always": [
{
"name": "Always do this",
"ansible.builtin.debug": {"msg": "This always executes"},
}
],
),
},
True,
),
),
Expand Down
57 changes: 29 additions & 28 deletions test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ def test_tokenize(
("reference_form", "alternate_forms"),
(
pytest.param(
dict(name="hello", action="command chdir=abc echo hello world"),
(dict(name="hello", command="chdir=abc echo hello world"),),
{"name": "hello", "action": "command chdir=abc echo hello world"},
({"name": "hello", "command": "chdir=abc echo hello world"},),
id="simple_command",
),
pytest.param(
Expand Down Expand Up @@ -113,12 +113,13 @@ def test_normalize(

def test_normalize_complex_command() -> None:
"""Test that tasks specified differently are normalized same way."""
task1 = dict(
name="hello", action={"module": "pip", "name": "df", "editable": "false"}
)
task2 = dict(name="hello", pip={"name": "df", "editable": "false"})
task3 = dict(name="hello", pip="name=df editable=false")
task4 = dict(name="hello", action="pip name=df editable=false")
task1 = {
"name": "hello",
"action": {"module": "pip", "name": "df", "editable": "false"},
}
task2 = {"name": "hello", "pip": {"name": "df", "editable": "false"}}
task3 = {"name": "hello", "pip": "name=df editable=false"}
task4 = {"name": "hello", "action": "pip name=df editable=false"}
assert utils.normalize_task(task1, "tasks.yml") == utils.normalize_task(
task2, "tasks.yml"
)
Expand All @@ -134,47 +135,47 @@ def test_normalize_complex_command() -> None:
("task", "expected_form"),
(
pytest.param(
dict(
name="ensure apache is at the latest version",
yum={"name": "httpd", "state": "latest"},
),
dict(
delegate_to=Sentinel,
name="ensure apache is at the latest version",
action={
{
"name": "ensure apache is at the latest version",
"yum": {"name": "httpd", "state": "latest"},
},
{
"delegate_to": Sentinel,
"name": "ensure apache is at the latest version",
"action": {
"__ansible_module__": "yum",
"__ansible_module_original__": "yum",
"__ansible_arguments__": [],
"name": "httpd",
"state": "latest",
},
),
},
),
pytest.param(
dict(
name="Attempt and graceful roll back",
block=[
{
"name": "Attempt and graceful roll back",
"block": [
{
"name": "Install httpd and memcached",
"ansible.builtin.yum": ["httpd", "memcached"],
"state": "present",
}
],
),
dict(
name="Attempt and graceful roll back",
block=[
},
{
"name": "Attempt and graceful roll back",
"block": [
{
"name": "Install httpd and memcached",
"ansible.builtin.yum": ["httpd", "memcached"],
"state": "present",
}
],
action={
"action": {
"__ansible_module__": "block/always/rescue",
"__ansible_module_original__": "block/always/rescue",
},
),
},
),
),
)
Expand Down Expand Up @@ -241,15 +242,15 @@ def test_template(template: str, output: str) -> None:
result = utils.template(
basedir="/base/dir",
value=template,
variables=dict(playbook_dir="/a/b/c"),
variables={"playbook_dir": "/a/b/c"},
fail_on_error=False,
)
assert result == output


def test_task_to_str_unicode() -> None:
"""Ensure that extracting messages from tasks preserves Unicode."""
task = dict(fail=dict(msg="unicode é ô à"))
task = {"fail": {"msg": "unicode é ô à"}}
result = utils.task_to_str(utils.normalize_task(task, "filename.yml"))
assert result == "fail msg=unicode é ô à"

Expand Down

0 comments on commit 024e8bf

Please sign in to comment.