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 ability to install v1 requiremets.yml #1385

Merged
merged 1 commit into from Feb 20, 2021
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
2 changes: 2 additions & 0 deletions examples/reqs_v1/requirements.yml
@@ -0,0 +1,2 @@
# v1 requirements test file
- src: geerlingguy.mysql
5 changes: 5 additions & 0 deletions examples/reqs_v2/requirements.yml
@@ -0,0 +1,5 @@
---
roles:
- name: geerlingguy.mysql
collections:
- name: ssbarnea.molecule
48 changes: 27 additions & 21 deletions src/ansiblelint/_prerun.py
Expand Up @@ -102,6 +102,7 @@ def prepare_environment() -> None:

cmd = [
"ansible-galaxy",
"role",
"install",
"--roles-path",
".cache/roles",
Expand All @@ -115,31 +116,36 @@ def prepare_environment() -> None:
universal_newlines=True,
check=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
if run.returncode != 0:
print(run.stdout, file=sys.stderr)
sys.exit(run.returncode)

cmd = [
"ansible-galaxy",
"collection",
"install",
"-p",
".cache/collections",
"-vr",
"requirements.yml",
]

print("Running %s" % " ".join(cmd))
run = subprocess.run(
cmd,
universal_newlines=True,
check=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
if run.returncode != 0:
sys.exit(run.returncode)
# Run galaxy collection install works on v2 requirements.yml
if "collections" in yaml_from_file("requirements.yml"):

cmd = [
"ansible-galaxy",
"collection",
"install",
"-p",
".cache/collections",
"-vr",
"requirements.yml",
]

print("Running %s" % " ".join(cmd))
run = subprocess.run(
cmd,
universal_newlines=True,
check=False,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
if run.returncode != 0:
print(run.stdout, file=sys.stderr)
sys.exit(run.returncode)

_install_galaxy_role()
_perform_mockings()
Expand Down
2 changes: 1 addition & 1 deletion src/ansiblelint/rules/RoleNames.py
Expand Up @@ -76,7 +76,7 @@ def matchyaml(self, file: Lintable) -> List["MatchError"]:
role_name = _remove_prefix(role_name, "ansible-role-")
if role_name not in self.done:
self.done.append(role_name)
if not self._re.match(role_name):
if role_name and not self._re.match(role_name):
result.append(
self.create_matcherror(
filename=str(file.path),
Expand Down
30 changes: 30 additions & 0 deletions test/test_prerun.py
@@ -0,0 +1,30 @@
"""Tests related to prerun part of the linter."""
import os

from ansiblelint.testing import run_ansible_lint


def test_prerun_reqs_v1() -> None:
"""Checks that the linter can auto-install requirements v1 when found."""
cwd = os.path.realpath(
os.path.join(
os.path.dirname(os.path.realpath(__file__)), "..", "examples", "reqs_v1"
)
)
result = run_ansible_lint(".", cwd=cwd)
assert "Running ansible-galaxy role install" in result.stdout
assert "Running ansible-galaxy collection install" not in result.stdout
assert result.returncode == 0


def test_prerun_reqs_v2() -> None:
"""Checks that the linter can auto-install requirements v2 when found."""
cwd = os.path.realpath(
os.path.join(
os.path.dirname(os.path.realpath(__file__)), "..", "examples", "reqs_v2"
)
)
result = run_ansible_lint(".", cwd=cwd)
assert "Running ansible-galaxy role install" in result.stdout
assert "Running ansible-galaxy collection install" in result.stdout
assert result.returncode == 0