Skip to content

Commit

Permalink
Fix offline mode (#2643)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea committed Oct 31, 2022
1 parent e5a256a commit 06dac60
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 12 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
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
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
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
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
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
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

0 comments on commit 06dac60

Please sign in to comment.