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
|
"""Tests related to ansiblelint.__main__ module."""
import os
import shutil
import subprocess
import sys
import time
from pathlib import Path
from typing import Any
import pytest
from ansiblelint.config import get_version_warning
@pytest.mark.parametrize(
("expected_warning"),
(False, True),
ids=("normal", "isolated"),
)
def test_call_from_outside_venv(expected_warning: bool) -> None:
"""Asserts ability to be called w/ or w/o venv activation."""
git_location = shutil.which("git")
assert git_location
if expected_warning:
env = {"HOME": str(Path.home()), "PATH": str(os.path.dirname(git_location))}
else:
env = os.environ.copy()
for v in ("COVERAGE_FILE", "COVERAGE_PROCESS_START"):
if v in os.environ:
env[v] = os.environ[v]
py_path = os.path.dirname(sys.executable)
# Passing custom env prevents the process from inheriting PATH or other
# environment variables from the current process, so we emulate being
# called from outside the venv.
proc = subprocess.run(
[f"{py_path}/ansible-lint", "--version"],
check=False,
capture_output=True,
text=True,
env=env,
)
assert proc.returncode == 0, proc
warning_found = "PATH altered to include" in proc.stderr
assert warning_found is expected_warning
@pytest.mark.parametrize(
("ver_diff", "found", "check", "outlen"),
(
("v1.2.2", True, "pre-release", 1),
("v1.2.3", False, "", 1),
("v1.2.4", True, "new release", 2),
),
)
def test_get_version_warning(
mocker: Any, ver_diff: str, found: bool, check: str, outlen: int
) -> None:
"""Assert get_version_warning working as expected."""
data = '{"html_url": "https://127.0.0.1", "tag_name": "' + f"{ver_diff}" + '"}'
# simulate cache file
mocker.patch("os.path.exists", return_value=True)
mocker.patch("os.path.getmtime", return_value=time.time())
mocker.patch("builtins.open", mocker.mock_open(read_data=data))
# overwrite ansible-lint version
mocker.patch("ansiblelint.config.__version__", "1.2.3")
# overwrite install method to custom one. This one will increase msg line count
# to easily detect unwanted call to it.
mocker.patch("ansiblelint.config.guess_install_method", return_value="\n")
msg = get_version_warning()
if not found:
assert msg == check
else:
assert check in msg
assert len(msg.split("\n")) == outlen
|