Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

win_unzip - LiteralPath fix #66972

Merged
merged 2 commits into from Jan 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/win_unzip-paths.yaml
@@ -0,0 +1,2 @@
bugfixes:
- win_unzip - Fix support for paths with square brackets not being detected properly
8 changes: 4 additions & 4 deletions lib/ansible/modules/windows/win_unzip.ps1
Expand Up @@ -40,7 +40,7 @@ Function Extract-Zip($src, $dest) {
$entry_target_path = [System.IO.Path]::Combine($dest, $archive_name)
$entry_dir = [System.IO.Path]::GetDirectoryName($entry_target_path)

if (-not (Test-Path -Path $entry_dir)) {
if (-not (Test-Path -LiteralPath $entry_dir)) {
New-Item -Path $entry_dir -ItemType Directory -WhatIf:$check_mode | Out-Null
$result.changed = $true
}
Expand Down Expand Up @@ -142,14 +142,14 @@ If ($ext -eq ".zip" -And $recurse -eq $false) {
}

If ($recurse) {
Get-ChildItem $dest -recurse | Where-Object {$pcx_extensions -contains $_.extension} | ForEach-Object {
Get-ChildItem -LiteralPath $dest -recurse | Where-Object {$pcx_extensions -contains $_.extension} | ForEach-Object {
Try {
Expand-Archive $_.FullName -OutputPath $dest -Force -WhatIf:$check_mode
} Catch {
Fail-Json -obj $result -message "Error recursively expanding '$src' to '$dest'! Msg: $($_.Exception.Message)"
}
If ($delete_archive) {
Remove-Item $_.FullName -Force -WhatIf:$check_mode
Remove-Item -LiteralPath $_.FullName -Force -WhatIf:$check_mode
$result.removed = $true
}
}
Expand All @@ -160,7 +160,7 @@ If ($ext -eq ".zip" -And $recurse -eq $false) {

If ($delete_archive){
try {
Remove-Item $src -Recurse -Force -WhatIf:$check_mode
Remove-Item -LiteralPath $src -Recurse -Force -WhatIf:$check_mode
} catch {
Fail-Json -obj $result -message "failed to delete archive at '$src': $($_.Exception.Message)"
}
Expand Down
1 change: 1 addition & 0 deletions test/integration/targets/win_unzip/defaults/main.yml
@@ -0,0 +1 @@
win_unzip_dir: '{{ remote_tmp_dir }}\win_unzip .ÅÑŚÌβŁÈ [$!@^&test(;)]'
28 changes: 28 additions & 0 deletions test/integration/targets/win_unzip/files/create_zip.py
@@ -0,0 +1,28 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright: (c) 2019, Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

import sys
import tempfile
import zipfile


def main():
filename = b"caf\xc3\xa9.txt"

with tempfile.NamedTemporaryFile() as temp:
with open(temp.name, mode="wb") as fd:
fd.write(filename)

with open(sys.argv[1], mode="wb") as fd:
with zipfile.ZipFile(fd, "w") as zip:
zip.write(temp.name, filename.decode('utf-8'))


if __name__ == '__main__':
main()
2 changes: 2 additions & 0 deletions test/integration/targets/win_unzip/meta/main.yml
@@ -0,0 +1,2 @@
dependencies:
- setup_remote_tmp_dir
15 changes: 0 additions & 15 deletions test/integration/targets/win_unzip/tasks/clean.yml

This file was deleted.

124 changes: 112 additions & 12 deletions test/integration/targets/win_unzip/tasks/main.yml
@@ -1,16 +1,116 @@
- name: Clean slate
import_tasks: clean.yml
---
- name: create test directory
win_file:
path: '{{ win_unzip_dir }}\output'
state: directory

- name: Test in normal mode
import_tasks: tests.yml
vars:
in_check_mode: no
- name: create local zip file with non-ascii chars
script: create_zip.py {{ output_dir + '/win_unzip.zip' | quote }}
delegate_to: localhost

- name: Clean slate
import_tasks: clean.yml
- name: copy across zip to Windows host
win_copy:
src: '{{ output_dir }}/win_unzip.zip'
dest: '{{ win_unzip_dir }}\win_unzip.zip'

- name: Test in check-mode
import_tasks: tests.yml
vars:
in_check_mode: yes
- name: unarchive zip (check)
win_unzip:
src: '{{ win_unzip_dir }}\win_unzip.zip'
dest: '{{ win_unzip_dir }}\output'
register: unzip_check
check_mode: yes

- name: get result of unarchive zip (check)
win_stat:
path: '{{ win_unzip_dir }}\output\café.txt'
register: unzip_actual_check

- name: assert result of unarchive zip (check)
assert:
that:
- unzip_check is changed
- not unzip_check.removed
- not unzip_actual_check.stat.exists

- name: unarchive zip
win_unzip:
src: '{{ win_unzip_dir }}\win_unzip.zip'
dest: '{{ win_unzip_dir }}\output'
register: unzip

- name: get result of unarchive zip
slurp:
path: '{{ win_unzip_dir }}\output\café.txt'
register: unzip_actual

- name: assert result of unarchive zip
assert:
that:
- unzip is changed
- not unzip.removed
- unzip_actual.content | b64decode == 'café.txt'

# Module is not idempotent, will always change without creates
- name: unarchive zip again without creates
win_unzip:
src: '{{ win_unzip_dir }}\win_unzip.zip'
dest: '{{ win_unzip_dir }}\output'
register: unzip_again

- name: assert unarchive zip again without creates
assert:
that:
- unzip_again is changed
- not unzip_again.removed

- name: unarchive zip with creates
win_unzip:
src: '{{ win_unzip_dir }}\win_unzip.zip'
dest: '{{ win_unzip_dir }}\outout'
creates: '{{ win_unzip_dir }}\output\café.txt'
register: unzip_again_creates

- name: assert unarchive zip with creates
assert:
that:
- not unzip_again_creates is changed
- not unzip_again_creates.removed

- name: unarchive zip with delete (check)
win_unzip:
src: '{{ win_unzip_dir }}\win_unzip.zip'
dest: '{{ win_unzip_dir }}\output'
delete_archive: yes
register: unzip_delete_check
check_mode: yes

- name: get result of unarchive zip with delete (check)
win_stat:
path: '{{ win_unzip_dir }}\win_unzip.zip'
register: unzip_delete_actual_check

- name: assert unarchive zip with delete (check)
assert:
that:
- unzip_delete_check is changed
- unzip_delete_check.removed
- unzip_delete_actual_check.stat.exists

- name: unarchive zip with delete
win_unzip:
src: '{{ win_unzip_dir }}\win_unzip.zip'
dest: '{{ win_unzip_dir }}\output'
delete_archive: yes
register: unzip_delete

- name: get result of unarchive zip with delete
win_stat:
path: '{{ win_unzip_dir }}\win_unzip.zip'
register: unzip_delete_actual

- name: assert unarchive zip with delete
assert:
that:
- unzip_delete is changed
- unzip_delete.removed
- not unzip_delete_actual.stat.exists
99 changes: 0 additions & 99 deletions test/integration/targets/win_unzip/tasks/tests.yml

This file was deleted.

1 change: 0 additions & 1 deletion test/sanity/ignore.txt
Expand Up @@ -7643,7 +7643,6 @@ lib/ansible/modules/windows/win_share.ps1 pslint:PSCustomUseLiteralPath
lib/ansible/modules/windows/win_shell.ps1 pslint:PSUseApprovedVerbs
lib/ansible/modules/windows/win_shortcut.ps1 pslint:PSCustomUseLiteralPath
lib/ansible/modules/windows/win_snmp.ps1 pslint:PSCustomUseLiteralPath
lib/ansible/modules/windows/win_unzip.ps1 pslint:PSCustomUseLiteralPath
lib/ansible/modules/windows/win_unzip.ps1 pslint:PSUseApprovedVerbs
lib/ansible/modules/windows/win_updates.ps1 pslint:PSCustomUseLiteralPath
lib/ansible/modules/windows/win_uri.ps1 pslint:PSAvoidUsingEmptyCatchBlock # Keep
Expand Down