forked from ansible/ansible-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconftest.py
92 lines (80 loc) · 2.72 KB
/
conftest.py
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
"""PyTest Fixtures."""
import importlib
import os
import subprocess
import sys
from typing import Any
import pytest
# checking if user is running pytest without installing test dependencies:
missing = []
for module in ["ansible", "black", "flake8", "flaky", "mypy", "pylint"]:
if not importlib.util.find_spec(module):
missing.append(module)
if missing:
print(
f"FATAL: Missing modules: {', '.join(missing)} -- probably you missed installing test requirements with: pip install -e '.[test]'",
file=sys.stderr,
)
sys.exit(1)
# we need to be sure that we have the requirements installed as some tests
# might depend on these.
try:
from ansible_compat.prerun import get_cache_dir
cache_dir = get_cache_dir(".")
subprocess.check_output(
[
"ansible-galaxy",
"collection",
"install",
"-p",
f"{cache_dir}/collections",
"-r",
"requirements.yml",
],
stderr=subprocess.PIPE,
text=True,
)
except subprocess.CalledProcessError as exc:
print(f"{exc}\n{exc.stderr}\n{exc.stdout}", file=sys.stderr)
sys.exit(1)
# flake8: noqa: E402
from ansible.module_utils.common.yaml import ( # pylint: disable=wrong-import-position
HAS_LIBYAML,
)
if not HAS_LIBYAML and sys.version_info >= (3, 9, 0):
# While presence of libyaml is not required for runtime, we keep this error
# fatal here in order to be sure that we spot libyaml errors during testing.
#
# For 3.8.x we do not do this check, as libyaml does not have an arm64 build for py38.
print(
"FATAL: For testing, we require pyyaml to be installed with its native extension, missing it would make testing 3x slower and risk missing essential bugs.",
file=sys.stderr,
)
sys.exit(1)
os.environ["NO_COLOR"] = "1"
# pylint: disable=unused-argument
def pytest_addoption(
parser: pytest.Parser, pluginmanager: pytest.PytestPluginManager
) -> None:
"""Add options to pytest."""
parser.addoption(
"--update-schemas",
action="store_true",
default=False,
dest="update_schemas",
help="update cached JSON schemas.",
)
def pytest_configure(config: Any) -> None:
"""Configure pytest."""
option = config.option
# run only on master node (xdist):
if option.update_schemas and not hasattr(config, "slaveinput"):
# pylint: disable=import-outside-toplevel
from ansiblelint.schemas import refresh_schemas
if refresh_schemas():
pytest.exit(
"Schemas are outdated, please update them in a separate pull request.",
1,
)
else:
pytest.exit("Schemas already updated", 0)