@ -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 `n Actual: $actual `n Expected: $expected "
$call_stack = ( Get-PSCallStack ) [ 1 ]
$error_msg = " AssertionError: `r `n Actual: `" $actual `" != Expected: `" $expected `" `r `n Line: $( $call_stack . ScriptLineNumber ) , Method: $( $call_stack . Position . Text ) "
Fail-Json -obj $result -message $error_msg
}
}
}
}
# Test- Fi lePath Hidden system file
# Test- Ansib lePath Hidden system file
$actual = Test- FilePath -p ath C: \ pagefile . sys
$actual = Test- AnsiblePath -P ath C: \ pagefile . sys
Assert-Equals -actual $actual -expected $true
Assert-Equals -actual $actual -expected $true
# Test- Fi lePath File that doesn't exist
# Test- Ansib lePath File that doesn't exist
$actual = Test- FilePath -p ath C: \ fakefile
$actual = Test- AnsiblePath -P ath C: \ fakefile
Assert-Equals -actual $actual -expected $false
Assert-Equals -actual $actual -expected $false
# Test- Fi lePath Normal directory
# Test- Ansib lePath Normal directory
$actual = Test- FilePath -p ath C: \ Windows
$actual = Test- AnsiblePath -P ath C: \ Windows
Assert-Equals -actual $actual -expected $true
Assert-Equals -actual $actual -expected $true
# Test- Fi lePath Normal file
# Test- Ansib lePath Normal file
$actual = Test- FilePath -p ath C: \ Windows \ System32 \ kernel32 . dll
$actual = Test- AnsiblePath -P ath 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-AnsibleItem doesn't exist with -ErrorAction SilentlyContinue param
$actual = Get-AnsibleItem -Path C: \ fakefile -ErrorAction SilentlyContinue
Assert-Equals -actual $actual -expected $null
# Get-FileItem file
# Get- Ansib leItem file
$actual = Get-FileItem -path C: \ pagefile . sys
$actual = Get- AnsibleItem -P ath C: \ pagefile . sys
Assert-Equals -actual $actual . FullName -expected C: \ pagefile . sys
Assert-Equals -actual $actual . FullName -expected C: \ pagefile . sys
Assert-Equals -actual $actual . PSIsContainer -expected $false
Assert-Equals -actual $actual . Attributes. HasFlag ( [ System.IO.FileAttributes ] :: Directory ) -expected $false
Assert-Equals -actual $actual . Exists -expected $true
Assert-Equals -actual $actual . Exists -expected $true
# Get- Fi leItem directory
# Get- Ansib leItem directory
$actual = Get- FileItem -p ath C: \ Windows
$actual = Get- AnsibleItem -P ath C: \ Windows
Assert-Equals -actual $actual . FullName -expected C: \ Windows
Assert-Equals -actual $actual . FullName -expected C: \ Windows
Assert-Equals -actual $actual . PSIsContainer -expected $true
Assert-Equals -actual $actual . Attributes. HasFlag ( [ System.IO.FileAttributes ] :: Directory ) -expected $true
Assert-Equals -actual $actual . Exists -expected $true
Assert-Equals -actual $actual . Exists -expected $true
# Get-FileItem doesn't exists
$result . data = " success "
$actual = Get-FileItem -path C: \ fakefile
Exit-Json -obj $result
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' }