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: