From 68e44e082e9ae1a52668c228f093a9c2de6a9602 Mon Sep 17 00:00:00 2001 From: Jordan Borean Date: Tue, 27 Mar 2018 16:19:45 +1000 Subject: [PATCH] Ansible.ModuleUtils.FileUtil: catch DirectoryNotFoundException when testing a path (#37968) --- .../powershell/Ansible.ModuleUtils.FileUtil.psm1 | 2 +- .../win_module_utils/library/file_util_test.ps1 | 8 ++++++++ test/integration/targets/win_shell/tasks/main.yml | 13 +++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) 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: