win_slurp - fix glob like paths (#53831)

(cherry picked from commit d00418c924)
pull/54037/head
Jordan Borean 6 years ago committed by Toshio Kuratomi
parent edfc44ec51
commit 19385f1ee1

@ -0,0 +1,2 @@
bugfixes:
- slurp - Fix issues when using paths on Windows with glob like characters, e.g. ``[``, ``]``

@ -11,14 +11,14 @@ $result = @{
changed = $false; changed = $false;
} }
If (Test-Path -Path $src -PathType Leaf) If (Test-Path -LiteralPath $src -PathType Leaf)
{ {
$bytes = [System.IO.File]::ReadAllBytes($src); $bytes = [System.IO.File]::ReadAllBytes($src);
$result.content = [System.Convert]::ToBase64String($bytes); $result.content = [System.Convert]::ToBase64String($bytes);
$result.encoding = "base64"; $result.encoding = "base64";
Exit-Json $result; Exit-Json $result;
} }
ElseIf (Test-Path -Path $src -PathType Container) ElseIf (Test-Path -LiteralPath $src -PathType Container)
{ {
Fail-Json $result "Path $src is a directory"; Fail-Json $result "Path $src is a directory";
} }

@ -0,0 +1 @@
test_win_slurp_dir: C:\ansible\win_slurp .ÅÑŚÌβŁÈ [$!@^&test(;)]

@ -0,0 +1,4 @@
- name: remove test directory
win_file:
path: '{{ test_win_slurp_dir }}'
state: absent

@ -16,20 +16,37 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
- name: create test directory
win_file:
path: '{{ test_win_slurp_dir }}'
state: directory
notify: remove test directory
# removes reliance on win_copy, set back once win_copy supports glob like chars
- name: create test file
win_shell: |
$file = '{{ test_win_slurp_dir }}\slurp.txt'
if (Test-Path -LiteralPath $file) {
Remove-Item -LiteralPath $file -Force
}
Set-Content -LiteralPath $file -Value 'Slurp this!'
- name: test slurping an existing file - name: test slurping an existing file
slurp: src="C:/Windows/win.ini" slurp:
src: '{{ test_win_slurp_dir }}\slurp.txt'
register: slurp_existing register: slurp_existing
- name: check slurp existing result - name: check slurp existing result
assert: assert:
that: that:
- "slurp_existing.content" - "slurp_existing.content == 'U2x1cnAgdGhpcyENCg=='"
- "slurp_existing.encoding == 'base64'" - "slurp_existing.encoding == 'base64'"
- "slurp_existing is not changed" - "slurp_existing is not changed"
- "slurp_existing is not failed" - "slurp_existing is not failed"
- name: test slurping a large binary file with path param and backslashes - name: test slurping a large binary file with path param and backslashes
slurp: path="C:\Windows\explorer.exe" slurp:
path: C:\Windows\explorer.exe
register: slurp_path_backslashes register: slurp_path_backslashes
no_log: true no_log: true
@ -42,7 +59,8 @@
- "slurp_path_backslashes is not failed" - "slurp_path_backslashes is not failed"
- name: test slurping a non-existent file - name: test slurping a non-existent file
slurp: src="C:/this_file_should_not_exist.txt" slurp:
src: C:\this_file_should_not_exist.txt
register: slurp_missing register: slurp_missing
ignore_errors: true ignore_errors: true
@ -54,7 +72,8 @@
- "slurp_missing is not changed" - "slurp_missing is not changed"
- name: test slurping a directory - name: test slurping a directory
slurp: src="C:/Windows" slurp:
src: '{{ test_win_slurp_dir }}\missing'
register: slurp_dir register: slurp_dir
ignore_errors: true ignore_errors: true

Loading…
Cancel
Save