Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for setting nodeps through cli and .ansible-lint #4280

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
5 changes: 5 additions & 0 deletions src/ansiblelint/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ def initialize_options(arguments: list[str] | None = None) -> None | FileLock:
for k, v in new_options.__dict__.items():
setattr(options, k, v)

if options.nodeps is None or options.nodeps is False:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not how env vars are supposed to work. When defined, they should always override the config options.

Preference order should always be: cli > env > config.

options.nodeps = bool(int(os.environ.get("ANSIBLE_LINT_NODEPS", "0")))
if options.nodeps:
options.offline = True

# rename deprecated ids/tags to newer names
options.tags = [normalize_tag(tag) for tag in options.tags]
options.skip_list = [normalize_tag(tag) for tag in options.skip_list]
Expand Down
9 changes: 9 additions & 0 deletions src/ansiblelint/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,14 @@ def get_cli_parser() -> argparse.ArgumentParser:
const=True,
help="Disable installation of requirements.yml and schema refreshing",
)
parser.add_argument(
"--nodeps",
dest="nodeps",
action="store_const",
const=True,
default=None,
help="Disable external dependency checking",
)
parser.add_argument(
"--version",
action="store_true",
Expand All @@ -486,6 +494,7 @@ def merge_config(file_config: dict[Any, Any], cli_config: Options) -> Options:
"strict",
"use_default_rules",
"offline",
"nodeps",
)
# maps lists to their default config values
lists_map = {
Expand Down
9 changes: 1 addition & 8 deletions src/ansiblelint/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ class Options: # pylint: disable=too-many-instance-attributes
only_builtins_allow_modules: list[str] = field(default_factory=list)
var_naming_pattern: str | None = None
offline: bool = False
nodeps: bool | None = None
project_dir: str = "." # default should be valid folder (do not use None here)
extra_vars: dict[str, Any] | None = None
enable_list: list[str] = field(default_factory=list)
Expand All @@ -178,16 +179,8 @@ class Options: # pylint: disable=too-many-instance-attributes
_default_supported = ["2.15.", "2.16.", "2.17."]
supported_ansible_also: list[str] = field(default_factory=list)

@property
def nodeps(self) -> bool:
"""Returns value of nodeps feature."""
# We do not want this to be cached as it would affect our testings.
return bool(int(os.environ.get("ANSIBLE_LINT_NODEPS", "0")))

def __post_init__(self) -> None:
"""Extra initialization logic."""
if self.nodeps:
self.offline = True

@property
def supported_ansible(self) -> list[str]:
Expand Down
5 changes: 5 additions & 0 deletions src/ansiblelint/schemas/ansible-lint-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@
"title": "Offline",
"type": "boolean"
},
"nodeps": {
"default": null,
"title": "Nodeps",
"type": ["boolean", "null"]
},
"only_builtins_allow_collections": {
"items": {
"type": "string"
Expand Down
10 changes: 9 additions & 1 deletion test/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,16 @@ def test_get_version_warning_no_pip(mocker: MockerFixture) -> None:
def test_nodeps(lintable: str) -> None:
"""Asserts ability to be called w/ or w/o venv activation."""
env = os.environ.copy()
env["ANSIBLE_LINT_NODEPS"] = "1"
py_path = Path(sys.executable).parent
proc = subprocess.run(
[str(py_path / "ansible-lint"), "--nodeps", lintable],
check=False,
capture_output=True,
text=True,
env=env,
)
assert proc.returncode == 0, proc
env["ANSIBLE_LINT_NODEPS"] = "1"
proc = subprocess.run(
[str(py_path / "ansible-lint"), lintable],
check=False,
Expand Down
2 changes: 1 addition & 1 deletion test/test_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def fixture_runner_result(
pytest.param(
"examples/playbooks/4114/transform-with-missing-role-and-modules.yml",
1,
True,
False,
True,
id="4114",
),
Expand Down
Loading