forked from pester/Pester
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOutput.Tests.ps1
405 lines (342 loc) · 15.8 KB
/
Output.Tests.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
Set-StrictMode -Version Latest
InModuleScope -ModuleName Pester -ScriptBlock {
Describe 'Has-Flag' -Fixture {
It 'Returns true when setting and value are the same' {
$setting = [Pester.OutputTypes]::Passed
$value = [Pester.OutputTypes]::Passed
$value | Has-Flag $setting | Should -Be $true
}
It 'Returns false when setting and value are the different' {
$setting = [Pester.OutputTypes]::Passed
$value = [Pester.OutputTypes]::Failed
$value | Has-Flag $setting | Should -Be $false
}
It 'Returns true when setting contains value' {
$setting = [Pester.OutputTypes]::Passed -bor [Pester.OutputTypes]::Failed
$value = [Pester.OutputTypes]::Passed
$value | Has-Flag $setting | Should -Be $true
}
It 'Returns false when setting does not contain the value' {
$setting = [Pester.OutputTypes]::Passed -bor [Pester.OutputTypes]::Failed
$value = [Pester.OutputTypes]::Summary
$value | Has-Flag $setting | Should -Be $false
}
It 'Returns true when at least one setting is contained in value' {
$setting = [Pester.OutputTypes]::Passed -bor [Pester.OutputTypes]::Failed
$value = [Pester.OutputTypes]::Summary -bor [Pester.OutputTypes]::Failed
$value | Has-Flag $setting | Should -Be $true
}
It 'Returns false when none of settings is contained in value' {
$setting = [Pester.OutputTypes]::Passed -bor [Pester.OutputTypes]::Failed
$value = [Pester.OutputTypes]::Summary -bor [Pester.OutputTypes]::Describe
$value | Has-Flag $setting | Should -Be $false
}
}
Describe 'Default OutputTypes' -Fixture {
It 'Fails output type contains all except passed' {
$expected = [Pester.OutputTypes]'Default, Failed, Pending, Skipped, Inconclusive, Describe, Context, Summary, Header'
[Pester.OutputTypes]::Fails | Should -Be $expected
}
It 'All output type contains all flags' {
$expected = [Pester.OutputTypes]'Default, Passed, Failed, Pending, Skipped, Inconclusive, Describe, Context, Summary, Header'
[Pester.OutputTypes]::All | Should -Be $expected
}
}
}
$thisScriptRegex = [regex]::Escape($MyInvocation.MyCommand.Path)
Describe 'ConvertTo-PesterResult' {
$getPesterResult = InModuleScope Pester { ${function:ConvertTo-PesterResult} }
Context 'failed tests in Tests file' {
#the $script scriptblock below is used as a position marker to determine
#on which line the test failed.
$errorRecord = $null
try {
$script = {}; 'something' | should -be 'nothing'
}
catch {
$errorRecord = $_
}
$result = & $getPesterResult -Time 0 -ErrorRecord $errorRecord
It 'records the correct stack line number' {
$result.StackTrace | should -match "${thisScriptRegex}: line $($script.startPosition.StartLine)"
}
It 'records the correct error record' {
$result.ErrorRecord -is [System.Management.Automation.ErrorRecord] | Should -be $true
$result.ErrorRecord.Exception.Message | Should -match "Expected: 'nothing'"
}
}
It 'Does not modify the error message from the original exception' {
$object = New-Object psobject
$message = 'I am an error.'
Add-Member -InputObject $object -MemberType ScriptMethod -Name ThrowSomething -Value { throw $message }
$errorRecord = $null
try {
$object.ThrowSomething()
}
catch {
$errorRecord = $_
}
$pesterResult = & $getPesterResult -Time 0 -ErrorRecord $errorRecord
$pesterResult.FailureMessage | Should -Be $errorRecord.Exception.Message
}
Context 'failed tests in another file' {
$errorRecord = $null
$testPath = Join-Path $TestDrive test.ps1
$escapedTestPath = [regex]::Escape($testPath)
Set-Content -Path $testPath -Value "$([System.Environment]::NewLine)'One' | Should -Be 'Two'"
try {
& $testPath
}
catch {
$errorRecord = $_
}
$result = & $getPesterResult -Time 0 -ErrorRecord $errorRecord
It 'records the correct stack line number' {
$result.StackTrace | should -match "${escapedTestPath}: line 2"
}
It 'records the correct error record' {
$result.ErrorRecord -is [System.Management.Automation.ErrorRecord] | Should -be $true
$result.ErrorRecord.Exception.Message | Should -match "Expected: 'Two'"
}
}
}
InModuleScope -ModuleName Pester -ScriptBlock {
Describe "Format-PesterPath" {
It "Writes path correctly when it is given `$null" {
Format-PesterPath -Path $null | Should -Be $null
}
If ( (GetPesterOS) -ne 'Windows') {
It "Writes path correctly when it is provided as string" {
Format-PesterPath -Path "/home/username/folder1" | Should -Be "/home/username/folder1"
}
It "Writes path correctly when it is provided as string[]" {
Format-PesterPath -Path @("/home/username/folder1", "/home/username/folder2") -Delimiter ', ' | Should -Be "/home/username/folder1, /home/username/folder2"
}
It "Writes path correctly when provided through hashtable" {
Format-PesterPath -Path @{ Path = "/home/username/folder1" } | Should -Be "/home/username/folder1"
}
It "Writes path correctly when provided through array of hashtable" {
Format-PesterPath -Path @{ Path = "/home/username/folder1" }, @{ Path = "/home/username/folder2" } -Delimiter ', ' | Should -Be "/home/username/folder1, /home/username/folder2"
}
}
Else {
It "Writes path correctly when it is provided as string" {
Format-PesterPath -Path "C:\path" | Should -Be "C:\path"
}
It "Writes path correctly when it is provided as string[]" {
Format-PesterPath -Path @("C:\path1", "C:\path2") -Delimiter ', ' | Should -Be "C:\path1, C:\path2"
}
It "Writes path correctly when provided through hashtable" {
Format-PesterPath -Path @{ Path = "C:\path" } | Should -Be "C:\path"
}
It "Writes path correctly when provided through array of hashtable" {
Format-PesterPath -Path @{ Path = "C:\path1" }, @{ Path = "C:\path2" } -Delimiter ', ' | Should -Be "C:\path1, C:\path2"
}
}
}
Describe "Write-PesterStart" {
It "uses Format-PesterPath with the provided path" {
Mock Format-PesterPath
if ((GetPesterOS) -ne 'Windows') {
$expected = "/tmp"
}
else {
$expected = "C:\temp"
}
Write-PesterStart -PesterState (New-PesterState) -Path $expected
Assert-MockCalled Format-PesterPath -ParameterFilter {$Path -eq $expected}
}
}
Describe ConvertTo-FailureLines {
& {
# disable the debugging prefrerence otherwise this would
# never pass
$global:PesterDebugPreference_ShowFullErrors = $false
$testPath = Join-Path $TestDrive test.ps1
$escapedTestPath = [regex]::Escape($testPath)
It 'produces correct message lines.' {
try {
throw 'message'
}
catch {
$e = $_
}
$r = $e | ConvertTo-FailureLines
$r.Message[0] | Should -be 'RuntimeException: message'
$r.Message.Count | Should -be 1
}
It 'failed should produces correct message lines.' {
try {
'One' | Should -be 'Two'
}
catch {
$e = $_
}
$r = $e | ConvertTo-FailureLines
$r.Message[0] | Should -be 'Expected strings to be the same, but they were different.'
$r.message[1] | Should -be 'String lengths are both 3.'
$r.message[2] | Should -be 'Strings differ at index 0.'
$r.Message[3] | Should -be "Expected: 'Two'"
$r.Message[4] | Should -be "But was: 'One'"
$r.Message[5] | Should -match "'One' | Should -be 'Two'"
$r.Message.Count | Should -be 6
}
# # todo: commented out because it does not work becuase of should, hopefully we can fix that later
# Context 'should fails in file' {
# Set-Content -Path $testPath -Value @'
# $script:IgnoreErrorPreference = 'SilentlyContinue'
# 'One' | Should -Be 'Two'
# '@
# try { & $testPath } catch { $e = $_ }
# $r = $e | ConvertTo-FailureLines
# It 'produces correct message lines.' {
# $r.Message[0] | Should -be 'String lengths are both 3. Strings differ at index 0.'
# $r.Message[1] | Should -be 'Expected: {Two}'
# $r.Message[2] | Should -be 'But was: {One}'
# $r.Message[3] | Should -be '-----------^'
# $r.Message[4] | Should -be "2: 'One' | Should -be 'Two'"
# $r.Message.Count | Should -be 5
# }
# if ( $e | Get-Member -Name ScriptStackTrace )
# {
# It 'produces correct trace lines.' {
# $r.Trace[0] | Should -be "at <ScriptBlock>, $testPath`: line 2"
# $r.Trace[1] -match 'at <ScriptBlock>, .*\\Functions\\Output.Tests.ps1: line [0-9]*$' |
# Should -be $true
# $r.Trace.Count | Should -be 3
# }
# }
# else
# {
# It 'produces correct trace lines.' {
# $r.Trace[0] | Should -be "at line: 2 in $testPath"
# $r.Trace.Count | Should -be 1
# }
# }
# }
Context 'exception thrown in nested functions in file' {
Set-Content -Path $testPath -Value @'
function f1 {
throw 'f1 message'
}
function f2 {
f1
}
f2
'@
try {
& $testPath
}
catch {
$e = $_
}
$r = $e | ConvertTo-FailureLines
It 'produces correct message lines.' {
$r.Message[0] | Should -be 'RuntimeException: f1 message'
}
if ( $e | Get-Member -Name ScriptStackTrace ) {
if ((GetPesterOS) -ne 'Windows') {
It 'produces correct trace lines.' {
$r.Trace[0] | Should -be "at f1, $testPath`: line 2"
$r.Trace[1] | Should -be "at f2, $testPath`: line 5"
$r.Trace[2] | Should -be "at <ScriptBlock>, $testPath`: line 7"
$r.Trace.Count | Should -be 4
}
}
else {
It 'produces correct trace lines.' {
$r.Trace[0] | Should -be "at f1, $testPath`: line 2"
$r.Trace[1] | Should -be "at f2, $testPath`: line 5"
$r.Trace[2] | Should -be "at <ScriptBlock>, $testPath`: line 7"
$r.Trace.Count | Should -be 4
}
}
}
else {
It 'produces correct trace lines.' {
$r.Trace[0] | Should -be "at line: 2 in $testPath"
$r.Trace.Count | Should -be 1
}
}
}
Context 'nested exceptions thrown in file' {
Set-Content -Path $testPath -Value @'
try
{
throw New-Object System.ArgumentException(
'inner message',
'param_name'
)
}
catch
{
throw New-Object System.FormatException(
'outer message',
$_.Exception
)
}
'@
try {
& $testPath
}
catch {
$e = $_
}
$r = $e | ConvertTo-FailureLines
It 'produces correct message lines.' {
$r.Message[0] | Should -be 'ArgumentException: inner message'
$r.Message[1] | Should -be 'Parameter name: param_name'
$r.Message[2] | Should -be 'FormatException: outer message'
}
if ( $e | Get-Member -Name ScriptStackTrace ) {
if ((GetPesterOS) -ne 'Windows') {
It 'produces correct trace line.' {
$r.Trace[0] | Should -be "at <ScriptBlock>, $testPath`: line 10"
$r.Trace.Count | Should -be 2
}
}
else {
It 'produces correct trace line.' {
$r.Trace[0] | Should -be "at <ScriptBlock>, $testPath`: line 10"
$r.Trace.Count | Should -be 2
}
}
}
else {
It 'produces correct trace line.' {
$r.Trace[0] | Should -be "at line: 10 in $testPath"
$r.Trace.Count | Should -be 1
}
}
}
Context 'Exceptions with no error message property set' {
$powershellVersion = $($PSVersionTable.PSVersion.Major)
try {
$exceptionWithNullMessage = New-Object -TypeName "System.Management.Automation.ParentContainsErrorRecordException"
throw $exceptionWithNullMessage
}
catch {
$exception = $_
}
$result = $exception | ConvertTo-FailureLines
if ($powershellVersion -lt 3) {
# Necessary because Microsoft changed the behaviour of System.Management.Automation.ParentContainsErrorRecordException at this point.
It 'produces correct message lines' {
$result.Message.Length | Should -Be 2
}
It 'produces correct trace line' {
$result.Trace.Count | Should -Be 1
}
}
else {
It 'produces correct message lines' {
$result.Message.Length | Should -Be 0
}
It 'produces correct trace line' {
$result.Trace.Count | Should -Be 1
}
}
}
}
}
}