From 06dac60bdd7998d1cde1621cc1b4fef369011b75 Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Mon, 31 Oct 2022 23:04:14 +0000 Subject: [PATCH] Fix offline mode (#2643) --- .pre-commit-config.yaml | 4 ++-- conftest.py | 19 +++++++++++++++++-- requirements.txt | 2 +- setup.cfg | 2 +- src/ansiblelint/testing/__init__.py | 12 ++++++++---- test/test_examples.py | 4 ++-- 6 files changed, 31 insertions(+), 12 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index a76ccd516c..219bd4eacb 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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 @@ -167,7 +167,7 @@ repos: hooks: - id: pylint additional_dependencies: - - ansible-compat>=2.2.0 + - ansible-compat>=2.2.3 - ansible-core - black - docutils diff --git a/conftest.py b/conftest.py index 6221106caa..93eba1fda6 100644 --- a/conftest.py +++ b/conftest.py @@ -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 = [] @@ -22,8 +21,19 @@ # 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, ) @@ -31,6 +41,11 @@ 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. diff --git a/requirements.txt b/requirements.txt index 880ce228f3..5b627b6b74 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 diff --git a/setup.cfg b/setup.cfg index d1ba1e75cf..0abb759483 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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 diff --git a/src/ansiblelint/testing/__init__.py b/src/ansiblelint/testing/__init__.py index dd7c88ec9a..704e25adb2 100644 --- a/src/ansiblelint/testing/__init__.py +++ b/src/ansiblelint/testing/__init__.py @@ -19,10 +19,6 @@ 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 @@ -30,12 +26,20 @@ 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]: diff --git a/test/test_examples.py b/test/test_examples.py index aedd71f2d4..cd2d935de7 100644 --- a/test/test_examples.py +++ b/test/test_examples.py @@ -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: