blob: c38f4e61e90569f8cc1b888cad954b16e9084b2a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
#!powershell
#Requires -Module Ansible.ModuleUtils.Legacy
#Requires -Module Ansible.ModuleUtils.FileUtil
$ErrorActionPreference = "Stop"
$result = @{
changed = $false
}
Function Assert-Equal($actual, $expected) {
if ($actual -cne $expected) {
$call_stack = (Get-PSCallStack)[1]
$error_msg = -join @(
"AssertionError:`r`nActual: `"$actual`" != Expected: `"$expected`"`r`nLine: "
"$($call_stack.ScriptLineNumber), Method: $($call_stack.Position.Text)"
)
Fail-Json -obj $result -message $error_msg
}
}
Function Get-PagefilePath() {
$pagefile = $null
$cs = Get-CimInstance -ClassName Win32_ComputerSystem
if ($cs.AutomaticManagedPagefile) {
$pagefile = "$($env:SystemRoot.Substring(0, 1)):\pagefile.sys"
}
else {
$pf = Get-CimInstance -ClassName Win32_PageFileSetting
if ($null -ne $pf) {
$pagefile = $pf[0].Name
}
}
return $pagefile
}
$pagefile = Get-PagefilePath
if ($pagefile) {
# Test-AnsiblePath Hidden system file
$actual = Test-AnsiblePath -Path $pagefile
Assert-Equal -actual $actual -expected $true
# Get-AnsibleItem file
$actual = Get-AnsibleItem -Path $pagefile
Assert-Equal -actual $actual.FullName -expected $pagefile
Assert-Equal -actual $actual.Attributes.HasFlag([System.IO.FileAttributes]::Directory) -expected $false
Assert-Equal -actual $actual.Exists -expected $true
}
# Test-AnsiblePath File that doesn't exist
$actual = Test-AnsiblePath -Path C:\fakefile
Assert-Equal -actual $actual -expected $false
# Test-AnsiblePath Directory that doesn't exist
$actual = Test-AnsiblePath -Path C:\fakedirectory
Assert-Equal -actual $actual -expected $false
# Test-AnsiblePath file in non-existant directory
$actual = Test-AnsiblePath -Path C:\fakedirectory\fakefile.txt
Assert-Equal -actual $actual -expected $false
# Test-AnsiblePath Normal directory
$actual = Test-AnsiblePath -Path C:\Windows
Assert-Equal -actual $actual -expected $true
# Test-AnsiblePath Normal file
$actual = Test-AnsiblePath -Path C:\Windows\System32\kernel32.dll
Assert-Equal -actual $actual -expected $true
# Test-AnsiblePath fails with wildcard
$failed = $false
try {
Test-AnsiblePath -Path C:\Windows\*.exe
}
catch {
$failed = $true
Assert-Equal -actual $_.Exception.Message -expected "Exception calling `"GetAttributes`" with `"1`" argument(s): `"Illegal characters in path.`""
}
Assert-Equal -actual $failed -expected $true
# Test-AnsiblePath on non file PS Provider object
$actual = Test-AnsiblePath -Path Cert:\LocalMachine\My
Assert-Equal -actual $actual -expected $true
# Test-AnsiblePath on environment variable
$actual = Test-AnsiblePath -Path env:SystemDrive
Assert-Equal -actual $actual -expected $true
# Test-AnsiblePath on environment variable that does not exist
$actual = Test-AnsiblePath -Path env:FakeEnvValue
Assert-Equal -actual $actual -expected $false
# Get-AnsibleItem doesn't exist with -ErrorAction SilentlyContinue param
$actual = Get-AnsibleItem -Path C:\fakefile -ErrorAction SilentlyContinue
Assert-Equal -actual $actual -expected $null
# Get-AnsibleItem directory
$actual = Get-AnsibleItem -Path C:\Windows
Assert-Equal -actual $actual.FullName -expected C:\Windows
Assert-Equal -actual $actual.Attributes.HasFlag([System.IO.FileAttributes]::Directory) -expected $true
Assert-Equal -actual $actual.Exists -expected $true
# ensure Get-AnsibleItem doesn't fail in a try/catch and -ErrorAction SilentlyContinue - stop's a trap from trapping it
try {
$actual = Get-AnsibleItem -Path C:\fakepath -ErrorAction SilentlyContinue
}
catch {
Fail-Json -obj $result -message "this should not fire"
}
Assert-Equal -actual $actual -expected $null
$result.data = "success"
Exit-Json -obj $result
|