mirror of https://github.com/ansible/ansible.git
windows: fix for checking locked system files (#30665)
* fix for checking locked system files * moved functions to share module util and created tests * fixed windows-paths test based on win_stat changespull/32955/head
parent
5e20fd0943
commit
e16e6313c7
@ -0,0 +1,41 @@
|
||||
# Copyright (c) 2017 Ansible Project
|
||||
# Simplified BSD License (see licenses/simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause)
|
||||
|
||||
<#
|
||||
Test-Path/Get-Item cannot find/return info on files that are locked like
|
||||
C:\pagefile.sys. These 2 functions are designed to work with these files and
|
||||
provide similar functionality with the normal cmdlets with as minimal overhead
|
||||
as possible. They work by using Get-ChildItem with a filter and return the
|
||||
result from that.
|
||||
#>
|
||||
|
||||
Function Test-FilePath($path) {
|
||||
# Basic replacement for Test-Path that tests if the file/folder exists at the path
|
||||
$directory = Split-Path -Path $path -Parent
|
||||
$filename = Split-Path -Path $path -Leaf
|
||||
|
||||
$file = Get-ChildItem -Path $directory -Filter $filename -Force -ErrorAction SilentlyContinue
|
||||
if ($file -ne $null) {
|
||||
if ($file -is [Array] -and $file.Count -gt 1) {
|
||||
throw "found multiple files at path '$path', make sure no wildcards are set in the path"
|
||||
}
|
||||
return $true
|
||||
} else {
|
||||
return $false
|
||||
}
|
||||
}
|
||||
|
||||
Function Get-FileItem($path) {
|
||||
# Replacement for Get-Item
|
||||
$directory = Split-Path -Path $path -Parent
|
||||
$filename = Split-Path -Path $path -Leaf
|
||||
|
||||
$file = Get-ChildItem -Path $directory -Filter $filename -Force -ErrorAction SilentlyContinue
|
||||
if ($file -is [Array] -and $file.Count -gt 1) {
|
||||
throw "found multiple files at path '$path', make sure no wildcards are set in the path"
|
||||
}
|
||||
|
||||
return $file
|
||||
}
|
||||
|
||||
Export-ModuleMember -Function Test-FilePath, Get-FileItem
|
@ -0,0 +1,59 @@
|
||||
#!powershell
|
||||
|
||||
#Requires -Module Ansible.ModuleUtils.Legacy
|
||||
#Requires -Module Ansible.ModuleUtils.FileUtil
|
||||
|
||||
Function Assert-Equals($actual, $expected) {
|
||||
if ($actual -cne $expected) {
|
||||
Fail-Json @{} "actual != expected`nActual: $actual`nExpected: $expected"
|
||||
}
|
||||
}
|
||||
|
||||
# Test-FilePath Hidden system file
|
||||
$actual = Test-FilePath -path C:\pagefile.sys
|
||||
Assert-Equals -actual $actual -expected $true
|
||||
|
||||
# Test-FilePath File that doesn't exist
|
||||
$actual = Test-FilePath -path C:\fakefile
|
||||
Assert-Equals -actual $actual -expected $false
|
||||
|
||||
# Test-FilePath Normal directory
|
||||
$actual = Test-FilePath -path C:\Windows
|
||||
Assert-Equals -actual $actual -expected $true
|
||||
|
||||
# Test-FilePath Normal file
|
||||
$actual = Test-FilePath -path C:\Windows\System32\kernel32.dll
|
||||
|
||||
# Test-FilePath fails with wildcard
|
||||
try {
|
||||
Test-FilePath -Path C:\Windows\*.exe
|
||||
Fail-Json @{} "exception was not thrown with wildcard search for Test-FilePath"
|
||||
} catch {
|
||||
Assert-Equals -actual $_.Exception.Message -expected "found multiple files at path 'C:\Windows\*.exe', make sure no wildcards are set in the path"
|
||||
}
|
||||
|
||||
# Get-FileItem file
|
||||
$actual = Get-FileItem -path C:\pagefile.sys
|
||||
Assert-Equals -actual $actual.FullName -expected C:\pagefile.sys
|
||||
Assert-Equals -actual $actual.PSIsContainer -expected $false
|
||||
Assert-Equals -actual $actual.Exists -expected $true
|
||||
|
||||
# Get-FileItem directory
|
||||
$actual = Get-FileItem -path C:\Windows
|
||||
Assert-Equals -actual $actual.FullName -expected C:\Windows
|
||||
Assert-Equals -actual $actual.PSIsContainer -expected $true
|
||||
Assert-Equals -actual $actual.Exists -expected $true
|
||||
|
||||
# Get-FileItem doesn't exists
|
||||
$actual = Get-FileItem -path C:\fakefile
|
||||
Assert-Equals -actual $actual -expected $null
|
||||
|
||||
# Get-FileItem fails with wildcard
|
||||
try {
|
||||
Get-FileItem -Path C:\Windows\*.exe
|
||||
Fail-Json @{} "exception was not thrown with wildcard search for Get-FileItem"
|
||||
} catch {
|
||||
Assert-Equals -actual $_.Exception.Message -expected "found multiple files at path 'C:\Windows\*.exe', make sure no wildcards are set in the path"
|
||||
}
|
||||
|
||||
Exit-Json @{ data = 'success' }
|
Loading…
Reference in New Issue