forked from pester/Pester
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestRegistry.ps1
173 lines (148 loc) · 6.56 KB
/
TestRegistry.ps1
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
function New-TestRegistry {
param(
[Switch]
$PassThru,
[string]
$Path
)
if ($Path -notmatch '\S') {
$directory = New-RandomTempRegistry
}
else {
if (-not (& $SafeCommands['Test-Path'] -Path $Path)) {
# the pester registry root path HKCU:\Pester is created once
# and then stays in place, in TestDrive we use system Temp folder,
# but no such folder exists for registry so we create our own.
# removing the folder after test run would be possible but we potentially
# running into conflict with other instance of Pester that is running
# so keeping it in place is a small price to pay for being able to run
# parallel pester sessions easily.
# Also don't use -Force parameter here
# because that deletes the folder and creates a race condition see
# https://github.com/pester/Pester/issues/1181
$null = & $SafeCommands['New-Item'] -Path $Path
}
$directory = & $SafeCommands['Get-Item'] $Path
}
$DriveName = "TestRegistry"
#setup the test drive
if ( -not (& $SafeCommands['Test-Path'] "${DriveName}:\") ) {
try {
$null = & $SafeCommands['New-PSDrive'] -Name $DriveName -PSProvider Registry -Root $directory -Scope Global -Description "Pester test registry" -ErrorAction Stop
}
catch {
if ($_.FullyQualifiedErrorId -like 'DriveAlreadyExists*') {
# it can happen that Test-Path reports false even though the drive
# exists. I don't know why but I see it in "Context Teardown fails"
# it would be possible to use Get-PsDrive directly for the test but it
# is about 10ms slower and we do it in every Describe and It so it would
# quickly add up
# so if that happens just ignore the error, the goal of this function is to
# create the testdrive and the testdrive already exists, so all is good.
}
else {
& $SafeCommands['Write-Error'] $_ -ErrorAction 'Stop'
}
}
}
$directory.PSPath
}
function Clear-TestRegistry {
param(
[String[]]$Exclude,
[string]$TestRegistryPath
)
# if the setup fails before we mark test registry added
# we would be trying to teardown something that does not
# exist and fail in Get-TestRegistryPath
if (-not (& $SafeCommands['Test-Path'] "TestRegistry:\")) {
return
}
$path = $TestRegistryPath
if ($null -ne $path -and (& $SafeCommands['Test-Path'] -Path $Path)) {
#Get-ChildItem -Exclude did not seem to work with full paths
& $SafeCommands['Get-ChildItem'] -Recurse -Path $Path |
& $SafeCommands['Sort-Object'] -Descending -Property 'PSPath' |
& $SafeCommands['Where-Object'] { $Exclude -NotContains $_.PSPath } |
& $SafeCommands['Remove-Item'] -Force -Recurse
}
}
function Get-TestRegistryChildItem ([string]$TestRegistryPath) {
& $SafeCommands['Get-ChildItem'] -Recurse -Path $TestRegistryPath |
& $SafeCommands['Select-Object'] -ExpandProperty PSPath
}
function New-RandomTempRegistry {
do {
$tempPath = Get-TempRegistry
$Path = & $SafeCommands['Join-Path'] -Path $tempPath -ChildPath ([Guid]::NewGuid())
} until (-not (& $SafeCommands['Test-Path'] -Path $Path ))
try {
& $SafeCommands['New-Item'] -Path $Path
}
catch [System.IO.IOException] {
# when running in parallel this occasionally triggers
# IOException: No more data is available
# let's just retry the operation
& $SafeCommands['New-Item'] -Path $Path
}
}
function Remove-TestRegistry ($TestRegistryPath) {
$DriveName = "TestRegistry"
$Drive = & $SafeCommands['Get-PSDrive'] -Name $DriveName -ErrorAction Ignore
if ($null -eq $Drive) {
# the drive does not exist, someone must have removed it instead of us,
# most likely a test that tests pester itself, so we just hope that the
# one who removed this removed also the contents of it correctly
return
}
$path = $TestRegistryPath
if ($pwd -like "$DriveName*" ) {
#will staying in the test drive cause issues?
#TODO: review this
& $SafeCommands['Write-Warning'] -Message "Your current path is set to ${pwd}:. You should leave ${DriveName}:\ before leaving Describe."
}
if ( $Drive ) {
$Drive | & $SafeCommands['Remove-PSDrive'] -Force #This should fail explicitly as it impacts future pester runs
}
if (& $SafeCommands['Test-Path'] -Path $path) {
& $SafeCommands['Remove-Item'] -Path $path -Force -Recurse
}
if (& $SafeCommands['Get-Variable'] -Name $DriveName -Scope Global -ErrorAction Ignore) {
& $SafeCommands['Remove-Variable'] -Scope Global -Name $DriveName -Force
}
}
function Get-TestRegistryPlugin {
# TODO: add OnStart block and put this in it
if (& $script:SafeCommands['Test-Path'] TestRegistry:\) {
& $SafeCommands['Remove-Item'] (& $SafeCommands['Get-PSDrive'] TestRegistry -ErrorAction Stop).Root -Force -Recurse -Confirm:$false -ErrorAction Ignore
& $SafeCommands['Remove-PSDrive'] TestRegistry
}
New-PluginObject -Name "TestRegistry" -EachBlockSetupStart {
param($Context)
if ($Context.Block.IsRoot) {
# this is top-level block setup test drive
$path = New-TestRegistry
$Context.Block.PluginData.Add('TestRegistry', @{
TestRegistryAdded = $true
TestRegistryContent = $null
TestRegistryPath = $path
})
}
else {
$testRegistryPath = $Context.Block.Parent.PluginData.TestRegistry.TestRegistryPath
$Context.Block.PluginData.Add('TestRegistry', @{
TestRegistryAdded = $false
TestRegistryContent = Get-TestRegistryChildItem -TestRegistryPath $testRegistryPath
TestRegistryPath = $testRegistryPath
})
}
} -EachBlockTearDownEnd {
if ($Context.Block.IsRoot) {
# this is top-level block remove test drive
Remove-TestRegistry -TestRegistryPath $Context.Block.PluginData.TestRegistry.TestRegistryPath
}
else {
Clear-TestRegistry -TestRegistryPath $Context.Block.PluginData.TestRegistry.TestRegistryPath -Exclude ( $Context.Block.PluginData.TestRegistry.TestRegistryContent )
}
}
}