From d0f1a23c458d2752e9ef1dc629275a609b9922dc Mon Sep 17 00:00:00 2001 From: Jordan Borean Date: Tue, 3 Apr 2018 06:30:53 +1000 Subject: [PATCH] Ansible.ModuleUtils.FileUtil - backport catch DirectoryNotFoundException (#37976) * Ansible.ModuleUtils.FileUtil: catch DirectoryNotFoundException when testing a path (#37968) (cherry picked from commit 68e44e082e9ae1a52668c228f093a9c2de6a9602) * Added changelog fragments --- .../win_file_util-test-exception-handler.yaml | 3 +++ .../powershell/Ansible.ModuleUtils.FileUtil.psm1 | 2 +- .../win_module_utils/library/file_util_test.ps1 | 8 ++++++++ test/integration/targets/win_shell/tasks/main.yml | 13 +++++++++++++ 4 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/win_file_util-test-exception-handler.yaml diff --git a/changelogs/fragments/win_file_util-test-exception-handler.yaml b/changelogs/fragments/win_file_util-test-exception-handler.yaml new file mode 100644 index 00000000000..03082c2de1f --- /dev/null +++ b/changelogs/fragments/win_file_util-test-exception-handler.yaml @@ -0,0 +1,3 @@ +bugfixes: +- Ansible.ModuleUtils.FileUtil - Catch DirectoryNotFoundException with Test-AnsiblePath + https://github.com/ansible/ansible/pull/37968 diff --git a/lib/ansible/module_utils/powershell/Ansible.ModuleUtils.FileUtil.psm1 b/lib/ansible/module_utils/powershell/Ansible.ModuleUtils.FileUtil.psm1 index 2e905593ebd..727da967f66 100644 --- a/lib/ansible/module_utils/powershell/Ansible.ModuleUtils.FileUtil.psm1 +++ b/lib/ansible/module_utils/powershell/Ansible.ModuleUtils.FileUtil.psm1 @@ -17,7 +17,7 @@ Function Test-AnsiblePath { # Replacement for Test-Path try { $file_attributes = [System.IO.File]::GetAttributes($Path) - } catch [System.IO.FileNotFoundException] { + } catch [System.IO.FileNotFoundException], [System.IO.DirectoryNotFoundException] { return $false } diff --git a/test/integration/targets/win_module_utils/library/file_util_test.ps1 b/test/integration/targets/win_module_utils/library/file_util_test.ps1 index 28e091c3809..b2a2fb8d20d 100644 --- a/test/integration/targets/win_module_utils/library/file_util_test.ps1 +++ b/test/integration/targets/win_module_utils/library/file_util_test.ps1 @@ -48,6 +48,14 @@ if ($pagefile) { $actual = Test-AnsiblePath -Path C:\fakefile Assert-Equals -actual $actual -expected $false +# Test-AnsiblePath Directory that doesn't exist +$actual = Test-AnsiblePath -Path C:\fakedirectory +Assert-Equals -actual $actual -expected $false + +# Test-AnsiblePath file in non-existant directory +$actual = Test-AnsiblePath -Path C:\fakedirectory\fakefile.txt +Assert-Equals -actual $actual -expected $false + # Test-AnsiblePath Normal directory $actual = Test-AnsiblePath -Path C:\Windows Assert-Equals -actual $actual -expected $true diff --git a/test/integration/targets/win_shell/tasks/main.yml b/test/integration/targets/win_shell/tasks/main.yml index b1162794d13..4680e417eda 100644 --- a/test/integration/targets/win_shell/tasks/main.yml +++ b/test/integration/targets/win_shell/tasks/main.yml @@ -144,6 +144,19 @@ that: - statout.stat.exists == true +# https://github.com/ansible/ansible/issues/37967 +- name: test creates with file in missing directory + win_shell: echo hi + args: + creates: c:\fakefolder\fakefolder2\fakefile.txt + register: shellout + +- name: validate result + assert: + that: + - shellout.skipped is not defined + - shellout.changed + - name: run with removes, should remove win_shell: Remove-Item c:\testfile.txt args: