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

Fix offline mode #2643

Merged
merged 1 commit into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ repos:
# empty args needed in order to match mypy cli behavior
args: [--strict]
additional_dependencies:
- ansible-compat>=2.2.0
- ansible-compat>=2.2.3
- ansible-core
- black
- filelock
Expand All @@ -167,7 +167,7 @@ repos:
hooks:
- id: pylint
additional_dependencies:
- ansible-compat>=2.2.0
- ansible-compat>=2.2.3
- ansible-core
- black
- docutils
Expand Down
19 changes: 17 additions & 2 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from typing import Any

import pytest
from ansible.module_utils.common.yaml import HAS_LIBYAML

# checking if user is running pytest without installing test dependencies:
missing = []
Expand All @@ -22,15 +21,31 @@
# 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", "-r", "requirements.yml"],
[
"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.
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# pip-compile --extra=docs --extra=test --no-annotate --output-file=requirements.txt --strip-extras --unsafe-package=ansible-core setup.cfg
#
alabaster==0.7.12
ansible-compat==2.2.1
ansible-compat==2.2.3
ansible-pygments==0.1.1
astroid==2.12.12
attrs==22.1.0
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ zip_safe = False

# These are required in actual runtime:
install_requires =
ansible-compat>=2.2.1 # GPLv3
ansible-compat>=2.2.3 # GPLv3
ansible-core>=2.12.0 # GPLv3
black>=22.1.0 # MIT
filelock # The Unlicense
Expand Down
12 changes: 8 additions & 4 deletions src/ansiblelint/testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,27 @@
else:
CompletedProcess = subprocess.CompletedProcess

# Emulate command line execution initialization as without it Ansible module
# would be loaded with incomplete module/role/collection list.
app = get_app(offline=True)

# pylint: disable=wrong-import-position
from ansiblelint.runner import Runner # noqa: E402


class RunFromText:
"""Use Runner on temp files created from testing text snippets."""

app = None

def __init__(self, collection: RulesCollection) -> None:
"""Initialize a RunFromText instance with rules collection."""
# Emulate command line execution initialization as without it Ansible module
# would be loaded with incomplete module/role/collection list.
if not self.app:
self.app = get_app(offline=True)

self.collection = collection

def _call_runner(self, path: str) -> list[MatchError]:
runner = Runner(path, rules=self.collection)
# breakpoint()
return runner.run()

def run(self, filename: str) -> list[MatchError]:
Expand Down
4 changes: 2 additions & 2 deletions test/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ def test_example_syntax_error(

def test_example_custom_module(default_rules_collection: RulesCollection) -> None:
"""custom_module.yml is expected to pass."""
get_app(offline=True)
app = get_app(offline=True)
result = Runner(
"examples/playbooks/custom_module.yml", rules=default_rules_collection
).run()
assert len(result) == 0
assert len(result) == 0, f"{app.runtime.cache_dir}"


def test_full_vault(default_rules_collection: RulesCollection) -> None:
Expand Down