forked from it-praktyk/Pester
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pester.Tests.ps1
330 lines (264 loc) · 12.9 KB
/
Pester.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
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$manifestPath = (Join-Path $here 'Pester.psd1')
$changeLogPath = (Join-Path $here 'CHANGELOG.md')
# DO NOT CHANGE THIS TAG NAME; IT AFFECTS THE CI BUILD.
Describe -Tags 'VersionChecks' "Pester manifest and changelog" {
$script:manifest = $null
$script:tagVersion = $null
$script:tagVersionShort = $null
$script:changelogVersion = $null
$script:changelogVersionShort = $null
It "has a valid manifest" {
{
$script:manifest = Test-ModuleManifest -Path $manifestPath -ErrorAction Stop -WarningAction SilentlyContinue
} | Should Not Throw
}
It "has a valid name in the manifest" {
$script:manifest.Name | Should Be Pester
}
It "has a valid guid in the manifest" {
$script:manifest.Guid | Should Be 'a699dea5-2c73-4616-a270-1f7abb777e71'
}
if (Get-Command git.exe -ErrorAction SilentlyContinue) {
$skipVersionTest = -not [bool]((git remote -v 2>&1) -match "github.com/Pester/")
It "is tagged with a valid version" -skip:$skipVersionTest {
$thisCommit = git.exe log --decorate --oneline HEAD~1..HEAD
if ($thisCommit -match 'tag:\s*(\d+(?:\.\d+)*)')
{
$script:tagVersion = $matches[1]
$script:tagVersionShort = $script:tagVersion -replace "-.*$", ''
}
$script:tagVersion | Should Not BeNullOrEmpty
$script:tagVersionShort -as [Version] | Should Not BeNullOrEmpty
}
}
It "has valid release notes in the manifest" {
$script:manifest.PrivateData.PSData.ReleaseNotes | Should -Be "https://github.com/pester/Pester/releases/tag/$script:tagVersion"
}
It "has a valid version in the changelog" {
foreach ($line in (Get-Content $changeLogPath))
{
if ($line -match "^\s*##\s+(?<Version>.*?)\s")
{
$script:changelogVersion = $matches.Version
$script:changelogVersionShort = $script:changelogVersion -replace "-.*$", ''
break
}
}
$script:changelogVersion | Should Not BeNullOrEmpty
$script:changelogVersionShort -as [Version] | Should Not BeNullOrEmpty
}
It "tag and changelog versions are the same" {
$script:changelogVersion | Should -Be $script:tagVersion
}
It "all versions are the same" -skip:$skipVersionTest {
$script:changelogVersionShort -as [Version] | Should -Be ( $script:manifest.Version -as [Version] )
$script:manifest.Version -as [Version] | Should -Be ( $script:tagVersionShort -as [Version] )
}
}
if ($PSVersionTable.PSVersion.Major -ge 3)
{
$error.Clear()
Describe 'Clean treatment of the $error variable' {
Context 'A Context' {
It 'Performs a successful test' {
$true | Should Be $true
}
}
It 'Did not add anything to the $error variable' {
$error.Count | Should Be 0
}
}
InModuleScope Pester {
Describe 'SafeCommands table' {
$path = $ExecutionContext.SessionState.Module.ModuleBase
$filesToCheck = Get-ChildItem -Path $path -Recurse -Include *.ps1,*.psm1 -Exclude *.Tests.ps1
$callsToSafeCommands = @(
foreach ($file in $filesToCheck)
{
$tokens = $parseErrors = $null
$ast = [System.Management.Automation.Language.Parser]::ParseFile($file.FullName, [ref] $tokens, [ref] $parseErrors)
$filter = {
$args[0] -is [System.Management.Automation.Language.CommandAst] -and
$args[0].InvocationOperator -eq [System.Management.Automation.Language.TokenKind]::Ampersand -and
$args[0].CommandElements[0] -is [System.Management.Automation.Language.IndexExpressionAst] -and
$args[0].CommandElements[0].Target -is [System.Management.Automation.Language.VariableExpressionAst] -and
$args[0].CommandElements[0].Target.VariablePath.UserPath -match '^(?:script:)?SafeCommands$'
}
$ast.FindAll($filter, $true)
}
)
$uniqueSafeCommands = $callsToSafeCommands | ForEach-Object { $_.CommandElements[0].Index.Value } | Select-Object -Unique
$missingSafeCommands = $uniqueSafeCommands | Where { -not $script:SafeCommands.ContainsKey($_) }
# These commands are conditionally added to the safeCommands table due to Nano / Core versus PSv2 compatibility; one will always
# be missing, and can be ignored.
$missingSafeCommands = $missingSafeCommands | Where { @('Get-WmiObject', 'Get-CimInstance') -notcontains $_ }
It 'The SafeCommands table contains all commands that are called from the module' {
$missingSafeCommands | Should Be $null
}
}
}
}
Describe 'Public API' {
It 'all non-deprecated, non-internal public commands use CmdletBinding' {
$r = Get-Command -Module Pester |
? { $_.CommandType -ne 'Alias' } | # Get-Command outputs aliases in PowerShell 2
? { -not $_.CmdletBinding } |
% { $_.Name } |
? {
@(
'Get-TestDriveItem' # deprecated in 4.0
'SafeGetCommand' # Pester internal
'Setup' # deprecated
) -notcontains $_
}
$r | Should beNullOrEmpty
}
}
Describe 'Style rules' -Tag StyleRules {
$pesterRoot = (Get-Module Pester).ModuleBase
$files = @(
Get-ChildItem $pesterRoot\* -Include *.ps1,*.psm1, *.psd1
Get-ChildItem (Join-Path $pesterRoot 'Functions') -Include *.ps1,*.psm1, *.psd1 -Recurse
Get-ChildItem (Join-Path $pesterRoot 'en-US') -Include *.ps1,*.psm1, *.psd1 -Recurse
)
It 'Pester source files contain no trailing whitespace' {
$badLines = @(
foreach ($file in $files)
{
$lines = [System.IO.File]::ReadAllLines($file.FullName)
$lineCount = $lines.Count
for ($i = 0; $i -lt $lineCount; $i++)
{
if ($lines[$i] -match '\s+$') {
'File: {0}, Line: {1}' -f $file.FullName, ($i + 1)
}
}
}
)
if ($badLines.Count -gt 0)
{
throw "The following $($badLines.Count) lines contain trailing whitespace: `r`n`r`n$($badLines -join "`r`n")"
}
}
It 'Spaces are used for indentation in all code files, not tabs' {
$badLines = @(
foreach ($file in $files)
{
$lines = [System.IO.File]::ReadAllLines($file.FullName)
$lineCount = $lines.Count
for ($i = 0; $i -lt $lineCount; $i++)
{
if ($lines[$i] -match '^[ ]*\t|^\t|^\t[ ]*') {
'File: {0}, Line: {1}' -f $file.FullName, ($i + 1)
}
}
}
)
if ($badLines.Count -gt 0)
{
throw "The following $($badLines.Count) lines start with a tab character: `r`n`r`n$($badLines -join "`r`n")"
}
}
It 'Pester Source Files all end with a newline' {
$badFiles = @(
foreach ($file in $files) {
$string = [System.IO.File]::ReadAllText($file.FullName)
if ($string.Length -gt 0 -and $string[-1] -ne "`n") {
$file.FullName
}
}
)
if ($badFiles.Count -gt 0) {
throw "The following files do not end with a newline: `r`n`r`n$($badFiles -join "`r`n")"
}
}
}
InModuleScope Pester {
Describe 'ResolveTestScripts' {
Setup -File SomeFile.ps1
Setup -File SomeFile.Tests.ps1
Setup -File SomeOtherFile.ps1
Setup -File SomeOtherFile.Tests.ps1
It 'Resolves non-wildcarded file paths regardless of whether the file ends with Tests.ps1' {
$result = @(ResolveTestScripts (Join-Path $TestDrive 'SomeOtherFile.ps1'))
$result.Count | Should Be 1
$result[0].Path | Should Be (Join-Path $TestDrive 'SomeOtherFile.ps1')
}
It 'Finds only *.Tests.ps1 files when the path contains wildcards' {
$result = @(ResolveTestScripts (Join-Path $TestDrive '*.ps1'))
$result.Count | Should Be 2
$paths = $result | Select-Object -ExpandProperty Path
($paths -contains (Join-Path $TestDrive 'SomeFile.Tests.ps1')) | Should Be $true
($paths -contains (Join-Path $TestDrive 'SomeOtherFile.Tests.ps1')) | Should Be $true
}
It 'Finds only *.Tests.ps1 files when the path refers to a directory and does not contain wildcards' {
$result = @(ResolveTestScripts $TestDrive)
$result.Count | Should Be 2
$paths = $result | Select-Object -ExpandProperty Path
($paths -contains ( Join-Path $TestDrive 'SomeFile.Tests.ps1')) | Should Be $true
($paths -contains ( Join-Path $TestDrive 'SomeOtherFile.Tests.ps1')) | Should Be $true
}
It 'Assigns empty array and hashtable to the Arguments and Parameters properties when none are specified by the caller' {
$result = @(ResolveTestScripts (Join-Path $TestDrive 'SomeFile.ps1'))
$result.Count | Should Be 1
$result[0].Path | Should Be (Join-Path $TestDrive 'SomeFile.ps1')
,$result[0].Arguments | Should Not Be $null
,$result[0].Parameters | Should Not Be $null
$result[0].Arguments.GetType() | Should Be ([object[]])
$result[0].Arguments.Count | Should Be 0
$result[0].Parameters.GetType() | Should Be ([hashtable])
$result[0].Parameters.PSBase.Count | Should Be 0
}
Context 'Passing in Dictionaries instead of Strings' {
It 'Allows the use of a "P" key instead of "Path"' {
$result = @(ResolveTestScripts @{ P = (Join-Path $TestDrive 'SomeFile.ps1') })
$result.Count | Should Be 1
$result[0].Path | Should Be (Join-Path $TestDrive 'SomeFile.ps1')
}
$testArgs = @('I am a string')
It 'Allows the use of an "Arguments" key in the dictionary' {
$result = @(ResolveTestScripts @{ Path = (Join-Path $TestDrive 'SomeFile.ps1'); Arguments = $testArgs })
$result.Count | Should Be 1
$result[0].Path | Should Be (Join-Path $TestDrive 'SomeFile.ps1')
$result[0].Arguments.Count | Should Be 1
$result[0].Arguments[0] | Should Be 'I am a string'
}
It 'Allows the use of an "Args" key in the dictionary' {
$result = @(ResolveTestScripts @{ Path = (Join-Path $TestDrive 'SomeFile.ps1'); Args = $testArgs })
$result.Count | Should Be 1
$result[0].Path | Should Be (Join-Path $TestDrive 'SomeFile.ps1')
$result[0].Arguments.Count | Should Be 1
$result[0].Arguments[0] | Should Be 'I am a string'
}
It 'Allows the use of an "A" key in the dictionary' {
$result = @(ResolveTestScripts @{ Path = (Join-Path $TestDrive 'SomeFile.ps1'); A = $testArgs })
$result.Count | Should Be 1
$result[0].Path | Should Be (Join-Path $TestDrive 'SomeFile.ps1')
$result[0].Arguments.Count | Should Be 1
$result[0].Arguments[0] | Should Be 'I am a string'
}
$testParams = @{ MyKey = 'MyValue' }
It 'Allows the use of a "Parameters" key in the dictionary' {
$result = @(ResolveTestScripts @{ Path = (Join-Path $TestDrive 'SomeFile.ps1'); Parameters = $testParams })
$result.Count | Should Be 1
$result[0].Path | Should Be (Join-Path $TestDrive 'SomeFile.ps1')
$result[0].Parameters.PSBase.Count | Should Be 1
$result[0].Parameters['MyKey'] | Should Be 'MyValue'
}
It 'Allows the use of a "Params" key in the dictionary' {
$result = @(ResolveTestScripts @{ Path = (Join-Path $TestDrive 'SomeFile.ps1'); Params = $testParams })
$result.Count | Should Be 1
$result[0].Path | Should Be (Join-Path $TestDrive 'SomeFile.ps1')
$result[0].Parameters.PSBase.Count | Should Be 1
$result[0].Parameters['MyKey'] | Should Be 'MyValue'
}
It 'Throws an error if no Path is specified' {
{ ResolveTestScripts @{} } | Should Throw
}
It 'Throws an error if a Parameters key is used, but does not contain an IDictionary object' {
{ ResolveTestScripts @{ P='P'; Params = 'A string' } } | Should Throw
}
}
}
}