From 8fa9c248fba17aae30cbe012cce687dfdba3ddc0 Mon Sep 17 00:00:00 2001 From: SananGuliyev Date: Mon, 29 Jun 2020 11:03:10 +0200 Subject: [PATCH 1/2] Handle `name` variable for alias --- ansible_project_init/ansible_galaxy_init.py | 7 ++-- test/test_ansible_galaxy_init.py | 38 +++++++++++++++++++-- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/ansible_project_init/ansible_galaxy_init.py b/ansible_project_init/ansible_galaxy_init.py index 0910903..063a528 100755 --- a/ansible_project_init/ansible_galaxy_init.py +++ b/ansible_project_init/ansible_galaxy_init.py @@ -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: @@ -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): @@ -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: diff --git a/test/test_ansible_galaxy_init.py b/test/test_ansible_galaxy_init.py index 5943bd3..62f46b8 100644 --- a/test/test_ansible_galaxy_init.py +++ b/test/test_ansible_galaxy_init.py @@ -37,7 +37,39 @@ 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 ) @@ -78,7 +110,7 @@ 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 @@ -184,7 +216,7 @@ 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 ) From bfed5794285af0dc03725828395a0da400994d3d Mon Sep 17 00:00:00 2001 From: SananGuliyev Date: Mon, 29 Jun 2020 11:15:36 +0200 Subject: [PATCH 2/2] Fixed linting issue --- test/test_ansible_galaxy_init.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/test/test_ansible_galaxy_init.py b/test/test_ansible_galaxy_init.py index 62f46b8..16a7ab8 100644 --- a/test/test_ansible_galaxy_init.py +++ b/test/test_ansible_galaxy_init.py @@ -37,7 +37,11 @@ def test_init(self, mock_os_getcwd, mock_subprocess_call): [ "ansible-galaxy", "install", - "%s,%s,%s" % (expected_package, expected_version, expected_package) + "%s,%s,%s" % ( + expected_package, + expected_version, + expected_package + ) ], command ) @@ -69,7 +73,11 @@ def test_init_with_alias(self, mock_os_getcwd, mock_subprocess_call): [ "ansible-galaxy", "install", - "%s,%s,%s" % (expected_package, expected_version, expected_alias) + "%s,%s,%s" % ( + expected_package, + expected_version, + expected_alias + ) ], command ) @@ -110,7 +118,11 @@ def test_init_with_different_installed_version( [ "ansible-galaxy", "install", - "%s,%s,%s" % (expected_package, expected_version, expected_package), + "%s,%s,%s" % ( + expected_package, + expected_version, + expected_package + ), "--force" ], command @@ -216,7 +228,11 @@ def test_init_with_invalid_install_info_yaml( [ "ansible-galaxy", "install", - "%s,%s,%s" % (expected_package, expected_version, expected_package) + "%s,%s,%s" % ( + expected_package, + expected_version, + expected_package + ) ], command )