mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-26 22:51:23 -07:00
standardise the powershell FileUtils (#34969)
This commit is contained in:
parent
944ae47701
commit
6f9f337a67
8 changed files with 86 additions and 73 deletions
|
@ -9,33 +9,39 @@ as possible. They work by using Get-ChildItem with a filter and return the
|
||||||
result from that.
|
result from that.
|
||||||
#>
|
#>
|
||||||
|
|
||||||
Function Test-FilePath($path) {
|
Function Test-AnsiblePath {
|
||||||
# Basic replacement for Test-Path that tests if the file/folder exists at the path
|
[CmdletBinding()]
|
||||||
$directory = Split-Path -Path $path -Parent
|
Param(
|
||||||
$filename = Split-Path -Path $path -Leaf
|
[Parameter(Mandatory=$true)][string]$Path
|
||||||
|
)
|
||||||
$file = Get-ChildItem -Path $directory -Filter $filename -Force -ErrorAction SilentlyContinue
|
# Replacement for Test-Path
|
||||||
if ($file -ne $null) {
|
try {
|
||||||
if ($file -is [Array] -and $file.Count -gt 1) {
|
$file_attributes = [System.IO.File]::GetAttributes($Path)
|
||||||
throw "found multiple files at path '$path', make sure no wildcards are set in the path"
|
} catch [System.IO.FileNotFoundException] {
|
||||||
}
|
|
||||||
return $true
|
|
||||||
} else {
|
|
||||||
return $false
|
return $false
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
Function Get-FileItem($path) {
|
if ([Int32]$file_attributes -eq -1) {
|
||||||
# Replacement for Get-Item
|
return $false
|
||||||
$directory = Split-Path -Path $path -Parent
|
} else {
|
||||||
$filename = Split-Path -Path $path -Leaf
|
return $true
|
||||||
|
|
||||||
$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
|
Function Get-AnsibleItem {
|
||||||
|
[CmdletBinding()]
|
||||||
|
Param(
|
||||||
|
[Parameter(Mandatory=$true)][string]$Path
|
||||||
|
)
|
||||||
|
# Replacement for Get-Item
|
||||||
|
$file_attributes = [System.IO.File]::GetAttributes($Path)
|
||||||
|
if ([Int32]$file_attributes -eq -1) {
|
||||||
|
throw New-Object -TypeName System.Management.Automation.ItemNotFoundException -ArgumentList "Cannot find path '$Path' because it does not exist."
|
||||||
|
} elseif ($file_attributes.HasFlag([System.IO.FileAttributes]::Directory)) {
|
||||||
|
return New-Object -TypeName System.IO.DirectoryInfo -ArgumentList $Path
|
||||||
|
} else {
|
||||||
|
return New-Object -TypeName System.IO.FileInfo -ArgumentList $Path
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Export-ModuleMember -Function Test-AnsiblePath, Get-AnsibleItem
|
||||||
|
|
|
@ -28,11 +28,11 @@ $result = @{
|
||||||
cmd = $raw_command_line
|
cmd = $raw_command_line
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($creates -and $(Test-FilePath -path $creates)) {
|
if ($creates -and $(Test-AnsiblePath -Path $creates)) {
|
||||||
Exit-Json @{msg="skipped, since $creates exists";cmd=$raw_command_line;changed=$false;skipped=$true;rc=0}
|
Exit-Json @{msg="skipped, since $creates exists";cmd=$raw_command_line;changed=$false;skipped=$true;rc=0}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($removes -and -not $(Test-FilePath -path $removes)) {
|
if ($removes -and -not $(Test-AnsiblePath -Path $removes)) {
|
||||||
Exit-Json @{msg="skipped, since $removes does not exist";cmd=$raw_command_line;changed=$false;skipped=$true;rc=0}
|
Exit-Json @{msg="skipped, since $removes does not exist";cmd=$raw_command_line;changed=$false;skipped=$true;rc=0}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -56,11 +56,11 @@ $result = @{
|
||||||
cmd = $raw_command_line
|
cmd = $raw_command_line
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($creates -and $(Test-FilePath -path $creates)) {
|
if ($creates -and $(Test-AnsiblePath -Path $creates)) {
|
||||||
Exit-Json @{msg="skipped, since $creates exists";cmd=$raw_command_line;changed=$false;skipped=$true;rc=0}
|
Exit-Json @{msg="skipped, since $creates exists";cmd=$raw_command_line;changed=$false;skipped=$true;rc=0}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($removes -and -not $(Test-FilePath -path $removes)) {
|
if ($removes -and -not $(Test-AnsiblePath -Path $removes)) {
|
||||||
Exit-Json @{msg="skipped, since $removes does not exist";cmd=$raw_command_line;changed=$false;skipped=$true;rc=0}
|
Exit-Json @{msg="skipped, since $removes does not exist";cmd=$raw_command_line;changed=$false;skipped=$true;rc=0}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -34,7 +34,7 @@ if (Get-Member -inputobject $params -name "get_md5") {
|
||||||
Add-DepreactionWarning -obj $result -message "get_md5 has been deprecated along with the md5 return value, use get_checksum=True and checksum_algorithm=md5 instead" -version 2.9
|
Add-DepreactionWarning -obj $result -message "get_md5 has been deprecated along with the md5 return value, use get_checksum=True and checksum_algorithm=md5 instead" -version 2.9
|
||||||
}
|
}
|
||||||
|
|
||||||
$info = Get-FileItem -path $path
|
$info = Get-AnsibleItem -Path $path -ErrorAction SilentlyContinue
|
||||||
If ($info -ne $null) {
|
If ($info -ne $null) {
|
||||||
$epoch_date = Get-Date -Date "01/01/1970"
|
$epoch_date = Get-Date -Date "01/01/1970"
|
||||||
$attributes = @()
|
$attributes = @()
|
||||||
|
@ -74,7 +74,7 @@ If ($info -ne $null) {
|
||||||
$stat.owner = $info.GetAccessControl().Owner
|
$stat.owner = $info.GetAccessControl().Owner
|
||||||
|
|
||||||
# values that are set according to the type of file
|
# values that are set according to the type of file
|
||||||
if ($info.PSIsContainer) {
|
if ($info.Attributes.HasFlag([System.IO.FileAttributes]::Directory)) {
|
||||||
$stat.isdir = $true
|
$stat.isdir = $true
|
||||||
$share_info = Get-WmiObject -Class Win32_Share -Filter "Path='$($stat.path -replace '\\', '\\')'"
|
$share_info = Get-WmiObject -Class Win32_Share -Filter "Path='$($stat.path -replace '\\', '\\')'"
|
||||||
if ($share_info -ne $null) {
|
if ($share_info -ne $null) {
|
||||||
|
@ -82,11 +82,14 @@ If ($info -ne $null) {
|
||||||
$stat.sharename = $share_info.Name
|
$stat.sharename = $share_info.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
$dir_files_sum = Get-ChildItem $stat.path -Recurse | Measure-Object -property length -sum
|
try {
|
||||||
if ($dir_files_sum -eq $null) {
|
$size = 0
|
||||||
|
foreach ($file in $info.EnumerateFiles("*", [System.IO.SearchOption]::AllDirectories)) {
|
||||||
|
$size += $file.Length
|
||||||
|
}
|
||||||
|
$stat.size = $size
|
||||||
|
} catch {
|
||||||
$stat.size = 0
|
$stat.size = 0
|
||||||
} else {
|
|
||||||
$stat.size = $dir_files_sum.Sum
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$stat.extension = $info.Extension
|
$stat.extension = $info.Extension
|
||||||
|
|
|
@ -119,7 +119,7 @@ if ($path -eq $null -and $port -eq $null -and $state -eq "drained") {
|
||||||
$complete = $false
|
$complete = $false
|
||||||
while (((Get-Date) - $start_time).TotalSeconds -lt $timeout) {
|
while (((Get-Date) - $start_time).TotalSeconds -lt $timeout) {
|
||||||
$attempts += 1
|
$attempts += 1
|
||||||
if (Test-FilePath -path $path) {
|
if (Test-AnsiblePath -Path $path) {
|
||||||
if ($search_regex -eq $null) {
|
if ($search_regex -eq $null) {
|
||||||
$complete = $true
|
$complete = $true
|
||||||
break
|
break
|
||||||
|
@ -150,7 +150,7 @@ if ($path -eq $null -and $port -eq $null -and $state -eq "drained") {
|
||||||
$complete = $false
|
$complete = $false
|
||||||
while (((Get-Date) - $start_time).TotalSeconds -lt $timeout) {
|
while (((Get-Date) - $start_time).TotalSeconds -lt $timeout) {
|
||||||
$attempts += 1
|
$attempts += 1
|
||||||
if (Test-FilePath -path $path) {
|
if (Test-AnsiblePath -Path $path) {
|
||||||
if ($search_regex -ne $null) {
|
if ($search_regex -ne $null) {
|
||||||
$file_contents = Get-Content -Path $path -Raw
|
$file_contents = Get-Content -Path $path -Raw
|
||||||
if ($file_contents -notmatch $search_regex) {
|
if ($file_contents -notmatch $search_regex) {
|
||||||
|
|
|
@ -3,57 +3,61 @@
|
||||||
#Requires -Module Ansible.ModuleUtils.Legacy
|
#Requires -Module Ansible.ModuleUtils.Legacy
|
||||||
#Requires -Module Ansible.ModuleUtils.FileUtil
|
#Requires -Module Ansible.ModuleUtils.FileUtil
|
||||||
|
|
||||||
|
$ErrorActionPreference = "Stop"
|
||||||
|
|
||||||
|
$result = @{
|
||||||
|
changed = $false
|
||||||
|
}
|
||||||
|
|
||||||
Function Assert-Equals($actual, $expected) {
|
Function Assert-Equals($actual, $expected) {
|
||||||
if ($actual -cne $expected) {
|
if ($actual -cne $expected) {
|
||||||
Fail-Json @{} "actual != expected`nActual: $actual`nExpected: $expected"
|
$call_stack = (Get-PSCallStack)[1]
|
||||||
|
$error_msg = "AssertionError:`r`nActual: `"$actual`" != Expected: `"$expected`"`r`nLine: $($call_stack.ScriptLineNumber), Method: $($call_stack.Position.Text)"
|
||||||
|
Fail-Json -obj $result -message $error_msg
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# Test-FilePath Hidden system file
|
# Test-AnsiblePath Hidden system file
|
||||||
$actual = Test-FilePath -path C:\pagefile.sys
|
$actual = Test-AnsiblePath -Path C:\pagefile.sys
|
||||||
Assert-Equals -actual $actual -expected $true
|
Assert-Equals -actual $actual -expected $true
|
||||||
|
|
||||||
# Test-FilePath File that doesn't exist
|
# Test-AnsiblePath File that doesn't exist
|
||||||
$actual = Test-FilePath -path C:\fakefile
|
$actual = Test-AnsiblePath -Path C:\fakefile
|
||||||
Assert-Equals -actual $actual -expected $false
|
Assert-Equals -actual $actual -expected $false
|
||||||
|
|
||||||
# Test-FilePath Normal directory
|
# Test-AnsiblePath Normal directory
|
||||||
$actual = Test-FilePath -path C:\Windows
|
$actual = Test-AnsiblePath -Path C:\Windows
|
||||||
Assert-Equals -actual $actual -expected $true
|
Assert-Equals -actual $actual -expected $true
|
||||||
|
|
||||||
# Test-FilePath Normal file
|
# Test-AnsiblePath Normal file
|
||||||
$actual = Test-FilePath -path C:\Windows\System32\kernel32.dll
|
$actual = Test-AnsiblePath -Path C:\Windows\System32\kernel32.dll
|
||||||
|
|
||||||
# Test-FilePath fails with wildcard
|
# Test-AnsiblePath fails with wildcard
|
||||||
|
$failed = $false
|
||||||
try {
|
try {
|
||||||
Test-FilePath -Path C:\Windows\*.exe
|
Test-AnsiblePath -Path C:\Windows\*.exe
|
||||||
Fail-Json @{} "exception was not thrown with wildcard search for Test-FilePath"
|
|
||||||
} catch {
|
} catch {
|
||||||
Assert-Equals -actual $_.Exception.Message -expected "found multiple files at path 'C:\Windows\*.exe', make sure no wildcards are set in the path"
|
$failed = $true
|
||||||
|
Assert-Equals -actual $_.Exception.Message -expected "Exception calling `"GetAttributes`" with `"1`" argument(s): `"Illegal characters in path.`""
|
||||||
}
|
}
|
||||||
|
Assert-Equals -actual $failed -expected $true
|
||||||
|
|
||||||
# Get-FileItem file
|
# Get-AnsibleItem doesn't exist with -ErrorAction SilentlyContinue param
|
||||||
$actual = Get-FileItem -path C:\pagefile.sys
|
$actual = Get-AnsibleItem -Path C:\fakefile -ErrorAction SilentlyContinue
|
||||||
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
|
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' }
|
# Get-AnsibleItem file
|
||||||
|
$actual = Get-AnsibleItem -Path C:\pagefile.sys
|
||||||
|
Assert-Equals -actual $actual.FullName -expected C:\pagefile.sys
|
||||||
|
Assert-Equals -actual $actual.Attributes.HasFlag([System.IO.FileAttributes]::Directory) -expected $false
|
||||||
|
Assert-Equals -actual $actual.Exists -expected $true
|
||||||
|
|
||||||
|
# Get-AnsibleItem directory
|
||||||
|
$actual = Get-AnsibleItem -Path C:\Windows
|
||||||
|
Assert-Equals -actual $actual.FullName -expected C:\Windows
|
||||||
|
Assert-Equals -actual $actual.Attributes.HasFlag([System.IO.FileAttributes]::Directory) -expected $true
|
||||||
|
Assert-Equals -actual $actual.Exists -expected $true
|
||||||
|
|
||||||
|
$result.data = "success"
|
||||||
|
Exit-Json -obj $result
|
||||||
|
|
|
@ -107,7 +107,7 @@
|
||||||
name: '{{win_stat_user}}'
|
name: '{{win_stat_user}}'
|
||||||
state: absent
|
state: absent
|
||||||
|
|
||||||
- name: ensure testy user profile is deleted
|
- name: ensure test user profile is deleted
|
||||||
win_shell: rmdir /S /Q {{profile_dir_out.stdout_lines[0]}}
|
win_shell: rmdir /S /Q {{profile_dir_out.stdout_lines[0]}}
|
||||||
args:
|
args:
|
||||||
executable: cmd.exe
|
executable: cmd.exe
|
||||||
|
|
|
@ -196,7 +196,7 @@
|
||||||
- stat_directory.stat.nlink == 1
|
- stat_directory.stat.nlink == 1
|
||||||
- stat_directory.stat.owner == 'BUILTIN\Administrators'
|
- stat_directory.stat.owner == 'BUILTIN\Administrators'
|
||||||
- stat_directory.stat.path == win_stat_dir + '\\nested'
|
- stat_directory.stat.path == win_stat_dir + '\\nested'
|
||||||
- stat_directory.stat.size == 21
|
- stat_directory.stat.size == 24
|
||||||
|
|
||||||
- name: test win_stat on empty directory
|
- name: test win_stat on empty directory
|
||||||
win_stat:
|
win_stat:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue