Skip to content

Commit

Permalink
Merge pull request #7 from nl2go/dependency_name
Browse files Browse the repository at this point in the history
Handle `name` as an alias
  • Loading branch information
build-failure authored Jun 29, 2020
2 parents 581c4b1 + bfed579 commit 4d48d48
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
7 changes: 4 additions & 3 deletions ansible_project_init/ansible_galaxy_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def install_dependencies(requirements_file):

for role in required_roles:
src = role.get("src")
name = role.get("name") if role.get("name") else src
req_version = role.get("version")

if not src:
Expand All @@ -42,7 +43,7 @@ def install_dependencies(requirements_file):
print("%s (%s) is already installed." % (src, cur_version))
continue

install_dependency(src, cur_version, req_version)
install_dependency(src, cur_version, req_version, name)


def is_version_installed(cur_version, req_version):
Expand All @@ -63,11 +64,11 @@ def get_yaml_file_content(file):
return data


def install_dependency(src, cur_version, req_version):
def install_dependency(src, cur_version, req_version, name):
command = [
"ansible-galaxy",
"install",
"%s,%s" % (src, req_version)
"%s,%s,%s" % (src, req_version, name)
]

if cur_version and cur_version != req_version:
Expand Down
54 changes: 51 additions & 3 deletions test/test_ansible_galaxy_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,47 @@ def test_init(self, mock_os_getcwd, mock_subprocess_call):
[
"ansible-galaxy",
"install",
"%s,%s" % (expected_package, expected_version)
"%s,%s,%s" % (
expected_package,
expected_version,
expected_package
)
],
command
)
)

ansible_galaxy_init.init()

base_dir.cleanup()

@mock.patch("subprocess.call")
@mock.patch("os.getcwd")
def test_init_with_alias(self, mock_os_getcwd, mock_subprocess_call):
expected_package = "some.package.name"
expected_version = "1991"
expected_alias = "some.package.alias"

base_dir = tempfile.TemporaryDirectory("r")
requirement_lines = [
"- src: %s\n" % expected_package,
" version: %s\n" % expected_version,
" name: %s\n" % expected_alias,
]
requirements_file = "%s/roles/requirements.yml" % base_dir.name
create_file(requirements_file, requirement_lines)

mock_os_getcwd.return_value = base_dir.name
mock_subprocess_call.side_effect = (
lambda command: self.assertEqual(
[
"ansible-galaxy",
"install",
"%s,%s,%s" % (
expected_package,
expected_version,
expected_alias
)
],
command
)
Expand Down Expand Up @@ -78,7 +118,11 @@ def test_init_with_different_installed_version(
[
"ansible-galaxy",
"install",
"%s,%s" % (expected_package, expected_version),
"%s,%s,%s" % (
expected_package,
expected_version,
expected_package
),
"--force"
],
command
Expand Down Expand Up @@ -184,7 +228,11 @@ def test_init_with_invalid_install_info_yaml(
[
"ansible-galaxy",
"install",
"%s,%s" % (expected_package, expected_version)
"%s,%s,%s" % (
expected_package,
expected_version,
expected_package
)
],
command
)
Expand Down

0 comments on commit 4d48d48

Please sign in to comment.