-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtest.ps1
281 lines (254 loc) · 8.26 KB
/
test.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
[CmdletBinding(PositionalBinding = $false)]
param(
[string] $Tests = $env:test_cases,
[switch] $Inverse
)
$ErrorActionPreference = 'Stop'
if ($env:RUNNER_DEBUG) {
$VerbosePreference = $DebugPreference = $InformationPreference = 'Continue'
}
if ($env:ChocolateyInstall) {
$chocolateyProfile = Join-Path $env:ChocolateyInstall "helpers\chocolateyProfile.psm1"
if (Test-Path $chocolateyProfile) {
Import-Module $chocolateyProfile
Update-SessionEnvironment
}
}
# Public
function Start-Program {
[CmdletBinding(DefaultParameterSetName = 'Default', PositionalBinding = $false)]
param(
[Parameter(Mandatory, Position = 0)]
[ValidateNotNullOrEmpty()]
[string] $FilePath,
[Parameter(Position = 1)]
[string[]] $ArgumentList,
[Parameter(ParameterSetName = 'Default')]
[switch] $Shim,
[Parameter(ParameterSetName = 'Shortcut')]
[switch] $Shortcut,
[switch] $SplashScreen,
[ValidateNotNullOrEmpty()]
[string] $ProcessName,
[int] $TimeoutSec = 60,
[string] $ScreenshotPrefix = $env:screenshot_prefix
)
if ($Shortcut) {
$FilePath = Get-Shortcut $FilePath
}
"Starting $FilePath $ArgumentList..."
Add-Screenshot $ScreenshotPrefix "start.1"
$sw = [System.Diagnostics.Stopwatch]::StartNew()
if ($Shortcut -and $ProcessName) {
# Some shortcuts doesn't have an explicit target (but works anyway).
# In such case, we need a process name to proceed.
& $FilePath $ArgumentList
Get-Process -Name $ProcessName | Format-Process
} else {
Start-Process -FilePath $FilePath -ArgumentList $ArgumentList -PassThru
| Tee-Object -Variable initialProcess
| Format-Process
if ($Shim) {
$initialProcess | Wait-Process -Timeout $TimeoutSec
}
if (!$ProcessName) {
$ProcessName = if ($initialProcess.Name) {
$initialProcess.Name
} else {
Split-Path $FilePath -LeafBase
}
}
}
$splashScreenHandles = @()
for ($i = 1; ; $i++) {
Start-Sleep -Milliseconds 100
Get-Process -Name $ProcessName -ErrorAction SilentlyContinue -OutVariable processes
| Format-Process
if ($processes | Where-Object MainWindowHandle -NotIn 0, $splashScreenHandles) {
if (!$SplashScreen -or $splashScreenHandles) {
break
}
$splashScreenHandles = [int[]] $processes.MainWindowHandle | Where-Object { $_ -ne 0 }
Write-Verbose "Splash screen handles: $splashScreenHandles"
}
Add-Screenshot $ScreenshotPrefix "start.2.$i.starting"
if ($sw.Elapsed.TotalSeconds -gt $TimeoutSec) {
Add-Screenshot $ScreenshotPrefix "start.3.failed"
throw 'Process start timed out.'
}
}
'Started in {0}ms' -f $sw.ElapsedMilliseconds
Add-Screenshot $ScreenshotPrefix "start.3.done"
}
function Close-Program {
[CmdletBinding(PositionalBinding = $false)]
param(
[Parameter(Mandatory, Position = 0)]
[ValidateNotNullOrEmpty()]
[string] $ProcessName,
[int] $TimeoutSec = 60,
[string] $ScreenshotPrefix = $env:screenshot_prefix
)
"Closing $ProcessName..."
Add-Screenshot $ScreenshotPrefix "close.1"
Get-Process -Name $ProcessName -ErrorAction SilentlyContinue -OutVariable processes
| Format-Process
if (!$processes) {
throw 'Cannot find a process with the name "{0}".' -f $ProcessName
}
if (!($processes | Where-Object MainWindowHandle -NE 0)) {
throw 'Cannot find any windows on a process with the name "{0}".' -f $ProcessName
}
$sw = [System.Diagnostics.Stopwatch]::StartNew()
$processes.CloseMainWindow() | Write-Verbose
Add-Screenshot $ScreenshotPrefix "close.2.closing"
try {
Wait-Process -Name $ProcessName -Timeout $TimeoutSec
} catch {
Add-Screenshot $ScreenshotPrefix "close.3.failed"
throw
}
'Closed in {0}ms' -f $sw.ElapsedMilliseconds
Add-Screenshot $ScreenshotPrefix "close.3.done"
}
function Invoke-WebApp {
[CmdletBinding(PositionalBinding = $false)]
param(
[Parameter(Mandatory, Position = 0)]
[ValidateNotNull()]
[uri] $Uri,
[int] $TimeoutSec = 60
)
"Invoking $Uri..."
$sw = [System.Diagnostics.Stopwatch]::StartNew()
while ($true) {
try {
Invoke-WebRequest -Uri $Uri
break
} catch {
if ($sw.Elapsed.TotalSeconds -gt $TimeoutSec) {
Write-Error 'WebApp invoke timed out.' -ErrorAction Continue
throw
}
"WebApp can't be reached: {0}" -f $_.Exception.Message | Write-Warning
}
Start-Sleep -Milliseconds 100
}
'Invoked in {0}ms' -f $sw.ElapsedMilliseconds
}
# Private
function Add-Screenshot {
[CmdletBinding(PositionalBinding = $false)]
param(
[Parameter(Position = 0)]
[string] $Prefix,
[Parameter(Mandatory, Position = 1)]
[ValidateNotNullOrEmpty()]
[string] $Name
)
if ($Prefix -and $DebugPreference -eq 'Continue') {
if (!(Get-Command nircmd -ErrorAction SilentlyContinue)) {
choco install -y --no-progress nircmd
}
nircmd savescreenshotfull "$Prefix.$Name.png"
}
}
filter ConvertTo-TestCases {
[CmdletBinding(PositionalBinding = $false)]
param(
[Parameter(Position = 0, ValueFromPipeline)]
[string] $InputObject
)
$list = $InputObject -split '^-\s+(.+)\s*$', 0, 'Multiline'
for ($i = 1; $i -lt $list.length - 1; $i += 2) {
[pscustomobject] @{
Title = $list[$i]
Script = $list[$i + 1].Trim() ? $list[$i + 1].Trim() : $list[$i]
}
}
}
filter Format-Process {
[CmdletBinding(PositionalBinding = $false)]
param(
[Parameter(Mandatory, Position = 0, ValueFromPipeline)]
[ValidateNotNull()]
[System.Diagnostics.Process] $InputObject
)
"Process Id = {0,-10}, Name = {1,-16}, hWnd = {2,-10}" -f @(
$InputObject.Id
$InputObject.Name
$InputObject.MainWindowHandle
) | Write-Verbose
}
function Get-Shortcut {
[CmdletBinding(PositionalBinding = $false)]
param(
[Parameter(Mandatory, Position = 0)]
[ValidateNotNullOrEmpty()]
[string] $Name
)
$userStartMenu = [System.Environment]::GetFolderPath('StartMenu')
$commonStartMenu = [System.Environment]::GetFolderPath('CommonStartMenu')
$shortcut = Get-ChildItem $userStartMenu, $commonStartMenu -Filter *.lnk -File -Recurse
| Where-Object BaseName -Like $Name
| ForEach-Object FullName
if (!$shortcut) {
throw 'Cannot find a shortcut with the name "{0}".' -f $Name
}
if ($shortcut -is [array]) {
$shortcut | Write-Verbose
throw 'Found multiple shortcuts with the name "{0}".' -f $Name
}
return $shortcut
}
filter Invoke-TestCase {
[CmdletBinding(PositionalBinding = $false)]
param(
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[string] $Title,
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[string] $Script
)
$logs = @()
try {
Invoke-Expression $Script *>&1
| ForEach-Object {
$logs += $_
}
if ($LASTEXITCODE) {
throw "Test failed with exit code $LASTEXITCODE."
}
if (!$Inverse) {
Write-Outcome $Title -Success -Logs $logs
return
}
} catch {
if ($Inverse) {
Write-Outcome $Title -Success -Logs $logs, $_
return
}
Write-Outcome $Title -Logs $logs
throw
}
Write-Outcome $Title -Logs $logs
throw 'Test failed.'
}
function Write-Outcome {
[CmdletBinding(PositionalBinding = $false)]
param(
[Parameter(Mandatory, Position = 0)]
[ValidateNotNullOrEmpty()]
[string] $Title,
[switch] $Success,
[psobject[]] $Logs
)
$icon = $Success ? '✅' : '❌'
"::group::$icon $Title"
$Logs
'::endgroup::'
}
$Tests
| ConvertTo-TestCases
| Invoke-TestCase