Ansible.ModuleUtils.FileUtil - backport 2.5 Add ability to test non file system provider paths (#39203)

* Ansible.ModuleUtils.FileUtil - Add ability to test non file system provider paths (#39200)


(cherry picked from commit b6afe6946d)

* Added changelog fragment
pull/39230/head
Jordan Borean 7 years ago committed by GitHub
parent 250d5b74fa
commit 6ab0d60e81
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,2 @@
bugfixes:
- Ansible.ModuleUtils.FileUtil - support using Test-AnsiblePath with non file system providers (https://github.com/ansible/ansible/pull/39200)

@ -19,6 +19,10 @@ Function Test-AnsiblePath {
$file_attributes = [System.IO.File]::GetAttributes($Path) $file_attributes = [System.IO.File]::GetAttributes($Path)
} catch [System.IO.FileNotFoundException], [System.IO.DirectoryNotFoundException] { } catch [System.IO.FileNotFoundException], [System.IO.DirectoryNotFoundException] {
return $false return $false
} catch [NotSupportedException] {
# When testing a path like Cert:\LocalMachine\My, System.IO.File will
# not work, we just revert back to using Test-Path for this
return Test-Path -Path $Path
} }
if ([Int32]$file_attributes -eq -1) { if ([Int32]$file_attributes -eq -1) {

@ -62,6 +62,7 @@ Assert-Equals -actual $actual -expected $true
# Test-AnsiblePath Normal file # Test-AnsiblePath Normal file
$actual = Test-AnsiblePath -Path C:\Windows\System32\kernel32.dll $actual = Test-AnsiblePath -Path C:\Windows\System32\kernel32.dll
Assert-Equals -actual $actual -expected $true
# Test-AnsiblePath fails with wildcard # Test-AnsiblePath fails with wildcard
$failed = $false $failed = $false
@ -73,6 +74,18 @@ try {
} }
Assert-Equals -actual $failed -expected $true Assert-Equals -actual $failed -expected $true
# Test-AnsiblePath on non file PS Provider object
$actual = Test-AnsiblePath -Path Cert:\LocalMachine\My
Assert-Equals -actual $actual -expected $true
# Test-AnsiblePath on environment variable
$actual = Test-AnsiblePath -Path env:SystemDrive
Assert-Equals -actual $actual -expected $true
# Test-AnsiblePath on environment variable that does not exist
$actual = Test-AnsiblePath -Path env:FakeEnvValue
Assert-Equals -actual $actual -expected $false
# Get-AnsibleItem doesn't exist with -ErrorAction SilentlyContinue param # Get-AnsibleItem doesn't exist with -ErrorAction SilentlyContinue param
$actual = Get-AnsibleItem -Path C:\fakefile -ErrorAction SilentlyContinue $actual = Get-AnsibleItem -Path C:\fakefile -ErrorAction SilentlyContinue
Assert-Equals -actual $actual -expected $null Assert-Equals -actual $actual -expected $null

Loading…
Cancel
Save