From 1b209d742e39900e676e6a43f900801e67cc9154 Mon Sep 17 00:00:00 2001 From: Jordan Borean Date: Thu, 22 Feb 2024 04:09:24 +0000 Subject: [PATCH] Galaxy trailing slash (#82732) * fix: ensure path argument doesn't have a backslash * feat: add changelog * Update lib/ansible/cli/__init__.py Co-authored-by: Matt Clay * Simplify logic and add tests --------- Co-authored-by: dorkamotorka Co-authored-by: Teodor Janez Podobnik <48418580+dorkamotorka@users.noreply.github.com> Co-authored-by: Matt Clay --- ...lection-argument-doesnt-have-backslash.yml | 2 ++ lib/ansible/cli/__init__.py | 4 +++ .../tasks/download.yml | 27 +++++++++++++++++++ .../tasks/install.yml | 27 +++++++++++++++++++ 4 files changed, 60 insertions(+) create mode 100644 changelogs/fragments/79705-fix-ensure-path-to-collection-argument-doesnt-have-backslash.yml diff --git a/changelogs/fragments/79705-fix-ensure-path-to-collection-argument-doesnt-have-backslash.yml b/changelogs/fragments/79705-fix-ensure-path-to-collection-argument-doesnt-have-backslash.yml new file mode 100644 index 00000000000..c80ef4674ff --- /dev/null +++ b/changelogs/fragments/79705-fix-ensure-path-to-collection-argument-doesnt-have-backslash.yml @@ -0,0 +1,2 @@ +bugfixes: + - ansible-galaxy - ensure path to ansible collection when installing or downloading doesn't have a backslash (https://github.com/ansible/ansible/pull/79705). diff --git a/lib/ansible/cli/__init__.py b/lib/ansible/cli/__init__.py index 64ab78fb0ab..b8da2dbd50f 100644 --- a/lib/ansible/cli/__init__.py +++ b/lib/ansible/cli/__init__.py @@ -425,6 +425,10 @@ class CLI(ABC): skip_tags.add(tag.strip()) options.skip_tags = list(skip_tags) + # Make sure path argument doesn't have a backslash + if hasattr(options, 'action') and options.action in ['install', 'download'] and hasattr(options, 'args'): + options.args = [path.rstrip("/") for path in options.args] + # process inventory options except for CLIs that require their own processing if hasattr(options, 'inventory') and not self.SKIP_INVENTORY_DEFAULTS: diff --git a/test/integration/targets/ansible-galaxy-collection/tasks/download.yml b/test/integration/targets/ansible-galaxy-collection/tasks/download.yml index a554c277b2a..8a2fa5652e4 100644 --- a/test/integration/targets/ansible-galaxy-collection/tasks/download.yml +++ b/test/integration/targets/ansible-galaxy-collection/tasks/download.yml @@ -170,6 +170,33 @@ - '"Downloading collection ''ansible_test.my_collection:1.0.0'' to" in download_collection.stdout' - download_collection_actual.stat.exists +- block: + - name: create skeleton collection for trailing slash test + command: ansible-galaxy collection init trailing_dir.name --init-path "{{ galaxy_dir }}" + + - name: install collection with directory source and trailing slash - {{ test_id }} + command: ansible-galaxy collection download '{{ galaxy_dir }}/trailing_dir/name/' {{ galaxy_verbosity }} + args: + chdir: '{{ galaxy_dir }}/download' + register: download_dir_slash + + - name: get result of install collections with with trailing slash - {{ test_id }} + stat: + path: '{{ galaxy_dir }}/download/collections/trailing_dir-name-1.0.0.tar.gz' + register: download_dir_slash_actual + + - name: assert install collections with with trailing slash - {{ test_id }} + assert: + that: + - '"Downloading collection ''trailing_dir.name:1.0.0'' to" in download_dir_slash.stdout' + - download_dir_slash_actual.stat.exists + + always: + - name: remove trailing dir skeleton + file: + path: '{{ galaxy_dir }}/trailing_dir' + state: absent + - name: remove test download dir file: path: '{{ galaxy_dir }}/download' diff --git a/test/integration/targets/ansible-galaxy-collection/tasks/install.yml b/test/integration/targets/ansible-galaxy-collection/tasks/install.yml index 92378266c11..595911086e9 100644 --- a/test/integration/targets/ansible-galaxy-collection/tasks/install.yml +++ b/test/integration/targets/ansible-galaxy-collection/tasks/install.yml @@ -1141,6 +1141,33 @@ - (install_concrete_pre_actual.results[0].content | b64decode | from_json).collection_info.version == '1.1.0-beta.1' - (install_concrete_pre_actual.results[1].content | b64decode | from_json).collection_info.version == '1.0.0' +- block: + - name: create skeleton collection for trailing slash test + command: ansible-galaxy collection init trailing_dir.name --init-path "{{ galaxy_dir }}/scratch" + + - name: install collection with directory source and trailing slash - {{ test_id }} + command: ansible-galaxy collection install '{{ galaxy_dir }}/scratch/trailing_dir/name/' {{ galaxy_verbosity }} + environment: + ANSIBLE_COLLECTIONS_PATHS: '{{ galaxy_dir }}/ansible_collections' + register: install_dir_slash + + - name: get result of install collections with with trailing slash - {{ test_id }} + slurp: + path: '{{ galaxy_dir }}/ansible_collections/trailing_dir/name/MANIFEST.json' + register: install_dir_slash_actual + + - name: assert install collections with with trailing slash - {{ test_id }} + assert: + that: + - '"Installing ''trailing_dir.name:1.0.0'' to" in install_dir_slash.stdout' + - (install_dir_slash_actual.content | b64decode | from_json).collection_info.version == '1.0.0' + + always: + - name: remove trailing dir skeleton + file: + path: '{{ galaxy_dir }}/scratch/trailing_dir' + state: absent + - name: remove collection dir after round of testing - {{ test_id }} file: path: '{{ galaxy_dir }}/ansible_collections'