From 4622a6a53834b804726aae41760420413e49045b Mon Sep 17 00:00:00 2001 From: Markus Falb Date: Tue, 30 Jan 2024 22:44:09 +0100 Subject: [PATCH] Handle files in skeleton template dirs correctly (PR#82624) * If file and file.j2 exists in skeletons template dir, copy both * Add tests --- lib/ansible/cli/galaxy.py | 6 +++--- .../collection_skeleton/roles/common/templates/file-a | 1 + .../collection_skeleton/roles/common/templates/file-a.j2 | 2 ++ .../units/cli/test_data/role_skeleton/templates/test.conf | 2 ++ test/units/cli/test_galaxy.py | 8 ++++++++ test/units/galaxy/test_collection.py | 3 +++ 6 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 test/units/cli/test_data/collection_skeleton/roles/common/templates/file-a create mode 100644 test/units/cli/test_data/collection_skeleton/roles/common/templates/file-a.j2 create mode 100644 test/units/cli/test_data/role_skeleton/templates/test.conf diff --git a/lib/ansible/cli/galaxy.py b/lib/ansible/cli/galaxy.py index ce878df4aca..bc73339dcbd 100755 --- a/lib/ansible/cli/galaxy.py +++ b/lib/ansible/cli/galaxy.py @@ -1217,10 +1217,10 @@ class GalaxyCLI(CLI): b_rendered = to_bytes(templar.template(template_data), errors='surrogate_or_strict') with open(dest_file, 'wb') as df: df.write(b_rendered) + elif f + '.j2' in files and not in_templates_dir: + # We have two possible sources for the same file, we prefer the jinja template + continue else: - if f + '.j2' in files: - # We have two possible sources for the same file, we prefer the jinja template - continue f_rel_path = os.path.relpath(os.path.join(root, f), obj_skeleton) shutil.copyfile(os.path.join(root, f), os.path.join(obj_path, f_rel_path), follow_symlinks=False) diff --git a/test/units/cli/test_data/collection_skeleton/roles/common/templates/file-a b/test/units/cli/test_data/collection_skeleton/roles/common/templates/file-a new file mode 100644 index 00000000000..2f76e89bd5c --- /dev/null +++ b/test/units/cli/test_data/collection_skeleton/roles/common/templates/file-a @@ -0,0 +1 @@ +file-a diff --git a/test/units/cli/test_data/collection_skeleton/roles/common/templates/file-a.j2 b/test/units/cli/test_data/collection_skeleton/roles/common/templates/file-a.j2 new file mode 100644 index 00000000000..20b3159a94e --- /dev/null +++ b/test/units/cli/test_data/collection_skeleton/roles/common/templates/file-a.j2 @@ -0,0 +1,2 @@ +{{ ansible_managed }} +file-a.j2 diff --git a/test/units/cli/test_data/role_skeleton/templates/test.conf b/test/units/cli/test_data/role_skeleton/templates/test.conf new file mode 100644 index 00000000000..972660202a7 --- /dev/null +++ b/test/units/cli/test_data/role_skeleton/templates/test.conf @@ -0,0 +1,2 @@ +[defaults] +test_key = not_a_jinja_file_at_all diff --git a/test/units/cli/test_galaxy.py b/test/units/cli/test_galaxy.py index 25fdc7f0e4a..67144b3a491 100644 --- a/test/units/cli/test_galaxy.py +++ b/test/units/cli/test_galaxy.py @@ -445,6 +445,14 @@ class TestGalaxyInitSkeleton(unittest.TestCase, ValidRoleTests): expected_contents = '[defaults]\ntest_key = {{ test_variable }}' self.assertEqual(expected_contents, contents.strip(), msg="test.conf.j2 doesn't contain what it should, is it being rendered?") + def test_file_not_masked_by_template(self): + test_conf = os.path.join(self.role_dir, 'templates', 'test.conf') + self.assertTrue(os.path.exists(test_conf), msg="The test.conf file doesn't seem to exist, was it not copied because test.conf.j2 does exist too?") + with open(test_conf, 'r') as f: + contents = f.read() + expected_contents = '[defaults]\ntest_key = not_a_jinja_file_at_all' + self.assertEqual(expected_contents, contents.strip(), msg="test.conf doesn't contain what it should, is it being rendered?") + def test_template_ignore_jinja_subfolder(self): test_conf_j2 = os.path.join(self.role_dir, 'templates', 'subfolder', 'test.conf.j2') self.assertTrue(os.path.exists(test_conf_j2), msg="The test.conf.j2 template doesn't seem to exist, is it being rendered as test.conf?") diff --git a/test/units/galaxy/test_collection.py b/test/units/galaxy/test_collection.py index e99e6f47810..796a16efa76 100644 --- a/test/units/galaxy/test_collection.py +++ b/test/units/galaxy/test_collection.py @@ -443,6 +443,9 @@ def test_init_collection(collection_input): file_hash = secure_hash_s(open(os.path.join(input_dir, 'README.md')).read()) assert file_hash == '08f24200b9fbe18903e7a50930c9d0df0b8d7da3' # shasum test/units/cli/test_data/collection_skeleton/README.md.j2 + assert os.path.isfile(os.path.join(input_dir, 'roles/common/templates/file-a.j2')) + assert os.path.isfile(os.path.join(input_dir, 'roles/common/templates/file-a')) + def test_build_collection_no_galaxy_yaml(): fake_path = u'/fake/ÅÑŚÌβŁÈ/path'