forked from pester/Pester
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestRegistry.ps1
124 lines (104 loc) · 4.54 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
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 {
Write-Error $_ -ErrorAction 'Stop'
}
}
}
if ( $PassThru ) {
& $SafeCommands['Get-PSDrive'] -Name $DriveName
}
}
function Get-TestRegistryPath () {
"Microsoft.PowerShell.Core\Registry::" + (& $SafeCommands['Get-PSDrive'] -Name TestRegistry -ErrorAction Stop).Root
}
function Clear-TestRegistry {
param(
[String[]]
$Exclude
)
$path = Get-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 {
$path = Get-TestRegistryPath
& $SafeCommands['Get-ChildItem'] -Recurse -Path $path
}
function New-RandomTempRegistry {
do {
$tempPath = Get-TempRegistry
$Path = & $SafeCommands['Join-Path'] -Path $tempPath -ChildPath ([Guid]::NewGuid())
} until (-not (& $SafeCommands['Test-Path'] -Path $Path ))
& $SafeCommands['New-Item'] -Path $Path
}
function Remove-TestRegistry {
$DriveName = "TestRegistry"
$Drive = & $SafeCommands['Get-PSDrive'] -Name $DriveName -ErrorAction $script:IgnoreErrorPreference
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 = Get-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 $script:IgnoreErrorPreference) {
& $SafeCommands['Remove-Variable'] -Scope Global -Name $DriveName -Force
}
}