Skip to content

Commit

Permalink
Add ability to install v1 requiremets.yml
Browse files Browse the repository at this point in the history
Fixes: #1376
  • Loading branch information
ssbarnea committed Feb 20, 2021
1 parent d3ab2af commit 25e8a36
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 22 deletions.
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
5 changes: 5 additions & 0 deletions requirements-v1.yml
@@ -0,0 +1,5 @@
---
roles:
- name: geerlingguy.dotfiles
- name: geerlingguy.homebrew
- name: geerlingguy.mas
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

0 comments on commit 25e8a36

Please sign in to comment.