1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
"""Tests for loaders submodule."""
import os
import tempfile
import uuid
from pathlib import Path
from textwrap import dedent
import pytest
from ansiblelint.loaders import (
IGNORE_FILE,
IgnoreRule,
IgnoreRuleQualifier,
load_ignore_txt,
)
def test_load_ignore_txt_default_empty() -> None:
"""Test load_ignore_txt when no ignore-file is present."""
with tempfile.TemporaryDirectory() as temporary_directory:
cwd = Path.cwd()
try:
os.chdir(temporary_directory)
result = load_ignore_txt()
finally:
os.chdir(cwd)
assert not result
def test_load_ignore_txt_default_success() -> None:
"""Test load_ignore_txt with an existing ignore-file in the default location."""
with tempfile.TemporaryDirectory() as temporary_directory:
ignore_file = Path(temporary_directory) / IGNORE_FILE.default
with ignore_file.open("w", encoding="utf-8") as _ignore_file:
_ignore_file.write(
dedent(
"""
# See https://ansible.readthedocs.io/projects/lint/configuring/#ignoring-rules-for-entire-files
playbook2.yml package-latest # comment
playbook2.yml foo-bar
playbook2.yml another-role skip # rule with qualifier
""",
),
)
cwd = Path.cwd()
try:
os.chdir(temporary_directory)
result = load_ignore_txt()
finally:
os.chdir(cwd)
assert result == {
"playbook2.yml": {
IgnoreRule("package-latest", frozenset()),
IgnoreRule("foo-bar", frozenset()),
IgnoreRule("another-role", frozenset([IgnoreRuleQualifier.SKIP])),
}
}
def test_load_ignore_txt_default_success_alternative() -> None:
"""Test load_ignore_txt with an ignore-file in the alternative location ('.config' subdirectory)."""
with tempfile.TemporaryDirectory() as temporary_directory:
ignore_file = Path(temporary_directory) / IGNORE_FILE.alternative
ignore_file.parent.mkdir(parents=True)
with ignore_file.open("w", encoding="utf-8") as _ignore_file:
_ignore_file.write(
dedent(
"""
playbook.yml foo-bar
playbook.yml more-foo # what-the-foo?
tasks/main.yml more-bar
""",
),
)
cwd = Path.cwd()
try:
os.chdir(temporary_directory)
result = load_ignore_txt()
finally:
os.chdir(cwd)
assert result == {
"playbook.yml": {
IgnoreRule("more-foo", frozenset()),
IgnoreRule("foo-bar", frozenset()),
},
"tasks/main.yml": {IgnoreRule("more-bar", frozenset())},
}
def test_load_ignore_txt_custom_success() -> None:
"""Test load_ignore_txt with an ignore-file in a user defined location."""
with tempfile.TemporaryDirectory() as temporary_directory:
ignore_file = Path(temporary_directory) / "subdir" / "my_ignores.txt"
ignore_file.parent.mkdir(parents=True, exist_ok=True)
with ignore_file.open("w", encoding="utf-8") as _ignore_file:
_ignore_file.write(
dedent(
"""
playbook.yml hector
vars/main.yml tuco
roles/guzman/tasks/main.yml lalo
roles/eduardo/tasks/main.yml lalo
""",
),
)
cwd = Path.cwd()
try:
os.chdir(temporary_directory)
result = load_ignore_txt(Path(ignore_file))
finally:
os.chdir(cwd)
assert result == {
"playbook.yml": {IgnoreRule("hector", frozenset())},
"roles/eduardo/tasks/main.yml": {IgnoreRule("lalo", frozenset())},
"roles/guzman/tasks/main.yml": {IgnoreRule("lalo", frozenset())},
"vars/main.yml": {IgnoreRule("tuco", frozenset())},
}
def test_load_ignore_txt_custom_fail() -> None:
"""Test load_ignore_txt with a user defined but invalid ignore-file location."""
result = load_ignore_txt(Path(str(uuid.uuid4())))
assert not result
def test_load_ignore_txt_invalid_tags(monkeypatch: pytest.MonkeyPatch) -> None:
"""Test load_ignore_txt with an existing ignore-file in the default location."""
with tempfile.TemporaryDirectory() as temporary_directory:
ignore_file = Path(temporary_directory) / IGNORE_FILE.default
with ignore_file.open("w", encoding="utf-8") as _ignore_file:
_ignore_file.write(
dedent(
"""
playbook2.yml package-latest invalid-tag
""",
),
)
monkeypatch.chdir(temporary_directory)
with pytest.raises(RuntimeError, match="Unable to parse line"):
load_ignore_txt()
|