Skip to content

Commit ce90dd2

Browse files
author
Friedrich Weinmann
committed
updated tests
1 parent d93bb12 commit ce90dd2

File tree

5 files changed

+222
-242
lines changed

5 files changed

+222
-242
lines changed

PSModuleDevelopment/tests/general/FileIntegrity.Tests.ps1

Lines changed: 54 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,94 @@
1-
$moduleRoot = (Resolve-Path "$PSScriptRoot\..\..").Path
1+
$moduleRoot = (Resolve-Path "$global:testroot\..").Path
22

3-
. "$PSScriptRoot\FileIntegrity.Exceptions.ps1"
4-
5-
function Get-FileEncoding
6-
{
7-
<#
8-
.SYNOPSIS
9-
Tests a file for encoding.
10-
11-
.DESCRIPTION
12-
Tests a file for encoding.
13-
14-
.PARAMETER Path
15-
The file to test
16-
#>
17-
[CmdletBinding()]
18-
Param (
19-
[Parameter(Mandatory = $True, ValueFromPipelineByPropertyName = $True)]
20-
[Alias('FullName')]
21-
[string]
22-
$Path
23-
)
24-
25-
[byte[]]$byte = get-content -Encoding byte -ReadCount 4 -TotalCount 4 -Path $Path
26-
27-
if ($byte[0] -eq 0xef -and $byte[1] -eq 0xbb -and $byte[2] -eq 0xbf) { 'UTF8' }
28-
elseif ($byte[0] -eq 0xfe -and $byte[1] -eq 0xff) { 'Unicode' }
29-
elseif ($byte[0] -eq 0 -and $byte[1] -eq 0 -and $byte[2] -eq 0xfe -and $byte[3] -eq 0xff) { 'UTF32' }
30-
elseif ($byte[0] -eq 0x2b -and $byte[1] -eq 0x2f -and $byte[2] -eq 0x76) { 'UTF7' }
31-
else { 'Unknown, possible ASCII' }
32-
}
3+
. "$global:testroot\general\FileIntegrity.Exceptions.ps1"
334

345
Describe "Verifying integrity of module files" {
6+
BeforeAll {
7+
function Get-FileEncoding
8+
{
9+
<#
10+
.SYNOPSIS
11+
Tests a file for encoding.
12+
13+
.DESCRIPTION
14+
Tests a file for encoding.
15+
16+
.PARAMETER Path
17+
The file to test
18+
#>
19+
[CmdletBinding()]
20+
Param (
21+
[Parameter(Mandatory = $True, ValueFromPipelineByPropertyName = $True)]
22+
[Alias('FullName')]
23+
[string]
24+
$Path
25+
)
26+
27+
if ($PSVersionTable.PSVersion.Major -lt 6)
28+
{
29+
[byte[]]$byte = get-content -Encoding byte -ReadCount 4 -TotalCount 4 -Path $Path
30+
}
31+
else
32+
{
33+
[byte[]]$byte = Get-Content -AsByteStream -ReadCount 4 -TotalCount 4 -Path $Path
34+
}
35+
36+
if ($byte[0] -eq 0xef -and $byte[1] -eq 0xbb -and $byte[2] -eq 0xbf) { 'UTF8 BOM' }
37+
elseif ($byte[0] -eq 0xfe -and $byte[1] -eq 0xff) { 'Unicode' }
38+
elseif ($byte[0] -eq 0 -and $byte[1] -eq 0 -and $byte[2] -eq 0xfe -and $byte[3] -eq 0xff) { 'UTF32' }
39+
elseif ($byte[0] -eq 0x2b -and $byte[1] -eq 0x2f -and $byte[2] -eq 0x76) { 'UTF7' }
40+
else { 'Unknown' }
41+
}
42+
}
43+
3544
Context "Validating PS1 Script files" {
36-
$allFiles = Get-ChildItem -Path $moduleRoot -Recurse -Filter "*.ps1" | Where-Object FullName -NotLike "$moduleRoot\tests\*"
45+
$allFiles = Get-ChildItem -Path $moduleRoot -Recurse | Where-Object Name -like "*.ps1" | Where-Object FullName -NotLike "$moduleRoot\tests\*"
3746

3847
foreach ($file in $allFiles)
3948
{
4049
$name = $file.FullName.Replace("$moduleRoot\", '')
4150

42-
It "[$name] Should have UTF8 encoding" {
43-
Get-FileEncoding -Path $file.FullName | Should Be 'UTF8'
51+
It "[$name] Should have UTF8 encoding with Byte Order Mark" -TestCases @{ file = $file } {
52+
Get-FileEncoding -Path $file.FullName | Should -Be 'UTF8 BOM'
4453
}
4554

46-
It "[$name] Should have no trailing space" {
47-
($file | Select-String "\s$" | Where-Object { $_.Line.Trim().Length -gt 0} | Measure-Object).Count | Should Be 0
55+
It "[$name] Should have no trailing space" -TestCases @{ file = $file } {
56+
($file | Select-String "\s$" | Where-Object { $_.Line.Trim().Length -gt 0}).LineNumber | Should -BeNullOrEmpty
4857
}
4958

5059
$tokens = $null
5160
$parseErrors = $null
5261
$ast = [System.Management.Automation.Language.Parser]::ParseFile($file.FullName, [ref]$tokens, [ref]$parseErrors)
5362

54-
It "[$name] Should have no syntax errors" {
55-
$parseErrors | Should Be $Null
63+
It "[$name] Should have no syntax errors" -TestCases @{ parseErrors = $parseErrors } {
64+
$parseErrors | Should -BeNullOrEmpty
5665
}
5766

5867
foreach ($command in $global:BannedCommands)
5968
{
6069
if ($global:MayContainCommand["$command"] -notcontains $file.Name)
6170
{
62-
It "[$name] Should not use $command" {
63-
$tokens | Where-Object Text -EQ $command | Should Be $null
71+
It "[$name] Should not use $command" -TestCases @{ tokens = $tokens; command = $command } {
72+
$tokens | Where-Object Text -EQ $command | Should -BeNullOrEmpty
6473
}
6574
}
6675
}
67-
68-
It "[$name] Should not contain aliases" {
69-
$tokens | Where-Object TokenFlags -eq CommandName | Where-Object { Test-Path "alias:\$($_.Text)" } | Measure-Object | Select-Object -ExpandProperty Count | Should Be 0
70-
}
7176
}
7277
}
7378

7479
Context "Validating help.txt help files" {
75-
$allFiles = Get-ChildItem -Path $moduleRoot -Recurse -Filter "*.help.txt" | Where-Object FullName -NotLike "$moduleRoot\tests\*"
80+
$allFiles = Get-ChildItem -Path $moduleRoot -Recurse | Where-Object Name -like "*.help.txt" | Where-Object FullName -NotLike "$moduleRoot\tests\*"
7681

7782
foreach ($file in $allFiles)
7883
{
7984
$name = $file.FullName.Replace("$moduleRoot\", '')
8085

81-
It "[$name] Should have UTF8 encoding" {
82-
Get-FileEncoding -Path $file.FullName | Should Be 'UTF8'
86+
It "[$name] Should have UTF8 encoding" -TestCases @{ file = $file } {
87+
Get-FileEncoding -Path $file.FullName | Should -Be 'UTF8 BOM'
8388
}
8489

85-
It "[$name] Should have no trailing space" {
86-
($file | Select-String "\s$" | Where-Object { $_.Line.Trim().Length -gt 0 } | Measure-Object).Count | Should Be 0
90+
It "[$name] Should have no trailing space" -TestCases @{ file = $file } {
91+
($file | Select-String "\s$" | Where-Object { $_.Line.Trim().Length -gt 0 } | Measure-Object).Count | Should -Be 0
8792
}
8893
}
8994
}

PSModuleDevelopment/tests/general/Help.Tests.ps1

Lines changed: 45 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ Param (
3636
$SkipTest,
3737

3838
[string[]]
39-
$CommandPath = @("$PSScriptRoot\..\..\functions", "$PSScriptRoot\..\..\internal\functions"),
39+
$CommandPath = @("$global:testroot\..\functions", "$global:testroot\..\internal\functions"),
4040

4141
[string]
4242
$ModuleName = "PSModuleDevelopment",
4343

4444
[string]
45-
$ExceptionsFile = "$PSScriptRoot\Help.Exceptions.ps1"
45+
$ExceptionsFile = "$global:testroot\general\Help.Exceptions.ps1"
4646
)
4747
if ($SkipTest) { return }
4848
. $ExceptionsFile
@@ -62,138 +62,83 @@ foreach ($command in $commands) {
6262

6363
# The module-qualified command fails on Microsoft.PowerShell.Archive cmdlets
6464
$Help = Get-Help $commandName -ErrorAction SilentlyContinue
65-
$testhelperrors = 0
66-
$testhelpall = 0
67-
Describe "Test help for $commandName" {
68-
69-
$testhelpall += 1
70-
if ($Help.Synopsis -like '*`[`<CommonParameters`>`]*') {
71-
# If help is not found, synopsis in auto-generated help is the syntax diagram
72-
It "should not be auto-generated" {
73-
$Help.Synopsis | Should Not BeLike '*`[`<CommonParameters`>`]*'
74-
}
75-
$testhelperrors += 1
76-
}
77-
78-
$testhelpall += 1
79-
if ([String]::IsNullOrEmpty($Help.Description.Text)) {
80-
# Should be a description for every function
81-
It "gets description for $commandName" {
82-
$Help.Description | Should Not BeNullOrEmpty
83-
}
84-
$testhelperrors += 1
85-
}
65+
66+
Describe "Test help for $commandName" {
8667

87-
$testhelpall += 1
88-
if ([String]::IsNullOrEmpty(($Help.Examples.Example | Select-Object -First 1).Code)) {
89-
# Should be at least one example
90-
It "gets example code from $commandName" {
91-
($Help.Examples.Example | Select-Object -First 1).Code | Should Not BeNullOrEmpty
92-
}
93-
$testhelperrors += 1
94-
}
68+
# If help is not found, synopsis in auto-generated help is the syntax diagram
69+
It "should not be auto-generated" -TestCases @{ Help = $Help } {
70+
$Help.Synopsis | Should -Not -BeLike '*`[`<CommonParameters`>`]*'
71+
}
9572

96-
$testhelpall += 1
97-
if ([String]::IsNullOrEmpty(($Help.Examples.Example.Remarks | Select-Object -First 1).Text)) {
98-
# Should be at least one example description
99-
It "gets example help from $commandName" {
100-
($Help.Examples.Example.Remarks | Select-Object -First 1).Text | Should Not BeNullOrEmpty
101-
}
102-
$testhelperrors += 1
103-
}
73+
# Should be a description for every function
74+
It "gets description for $commandName" -TestCases @{ Help = $Help } {
75+
$Help.Description | Should -Not -BeNullOrEmpty
76+
}
10477

105-
if ($testhelperrors -eq 0) {
106-
It "Ran silently $testhelpall tests" {
107-
$testhelperrors | Should be 0
108-
}
109-
}
78+
# Should be at least one example
79+
It "gets example code from $commandName" -TestCases @{ Help = $Help } {
80+
($Help.Examples.Example | Select-Object -First 1).Code | Should -Not -BeNullOrEmpty
81+
}
82+
83+
# Should be at least one example description
84+
It "gets example help from $commandName" -TestCases @{ Help = $Help } {
85+
($Help.Examples.Example.Remarks | Select-Object -First 1).Text | Should -Not -BeNullOrEmpty
86+
}
11087

111-
$testparamsall = 0
112-
$testparamserrors = 0
11388
Context "Test parameter help for $commandName" {
11489

115-
$Common = 'Debug', 'ErrorAction', 'ErrorVariable', 'InformationAction', 'InformationVariable', 'OutBuffer', 'OutVariable',
116-
'PipelineVariable', 'Verbose', 'WarningAction', 'WarningVariable'
90+
$common = 'Debug', 'ErrorAction', 'ErrorVariable', 'InformationAction', 'InformationVariable', 'OutBuffer', 'OutVariable', 'PipelineVariable', 'Verbose', 'WarningAction', 'WarningVariable'
11791

11892
$parameters = $command.ParameterSets.Parameters | Sort-Object -Property Name -Unique | Where-Object Name -notin $common
11993
$parameterNames = $parameters.Name
12094
$HelpParameterNames = $Help.Parameters.Parameter.Name | Sort-Object -Unique
12195
foreach ($parameter in $parameters) {
12296
$parameterName = $parameter.Name
12397
$parameterHelp = $Help.parameters.parameter | Where-Object Name -EQ $parameterName
98+
99+
# Should be a description for every parameter
100+
It "gets help for parameter: $parameterName : in $commandName" -TestCases @{ parameterHelp = $parameterHelp } {
101+
$parameterHelp.Description.Text | Should -Not -BeNullOrEmpty
102+
}
124103

125-
$testparamsall += 1
126-
if ([String]::IsNullOrEmpty($parameterHelp.Description.Text)) {
127-
# Should be a description for every parameter
128-
It "gets help for parameter: $parameterName : in $commandName" {
129-
$parameterHelp.Description.Text | Should Not BeNullOrEmpty
130-
}
131-
$testparamserrors += 1
132-
}
133-
134-
$testparamsall += 1
135104
$codeMandatory = $parameter.IsMandatory.toString()
136-
if ($parameterHelp.Required -ne $codeMandatory) {
137-
# Required value in Help should match IsMandatory property of parameter
138-
It "help for $parameterName parameter in $commandName has correct Mandatory value" {
139-
$parameterHelp.Required | Should Be $codeMandatory
140-
}
141-
$testparamserrors += 1
142-
}
105+
It "help for $parameterName parameter in $commandName has correct Mandatory value" -TestCases @{ parameterHelp = $parameterHelp; codeMandatory = $codeMandatory } {
106+
$parameterHelp.Required | Should -Be $codeMandatory
107+
}
143108

144109
if ($HelpTestSkipParameterType[$commandName] -contains $parameterName) { continue }
145110

146111
$codeType = $parameter.ParameterType.Name
147112

148-
$testparamsall += 1
149113
if ($parameter.ParameterType.IsEnum) {
150114
# Enumerations often have issues with the typename not being reliably available
151115
$names = $parameter.ParameterType::GetNames($parameter.ParameterType)
152-
if ($parameterHelp.parameterValueGroup.parameterValue -ne $names) {
153-
# Parameter type in Help should match code
154-
It "help for $commandName has correct parameter type for $parameterName" {
155-
$parameterHelp.parameterValueGroup.parameterValue | Should be $names
156-
}
157-
$testparamserrors += 1
158-
}
116+
# Parameter type in Help should match code
117+
It "help for $commandName has correct parameter type for $parameterName" -TestCases @{ parameterHelp = $parameterHelp; names = $names } {
118+
$parameterHelp.parameterValueGroup.parameterValue | Should -be $names
119+
}
159120
}
160121
elseif ($parameter.ParameterType.FullName -in $HelpTestEnumeratedArrays) {
161122
# Enumerations often have issues with the typename not being reliably available
162123
$names = [Enum]::GetNames($parameter.ParameterType.DeclaredMembers[0].ReturnType)
163-
if ($parameterHelp.parameterValueGroup.parameterValue -ne $names) {
164-
# Parameter type in Help should match code
165-
It "help for $commandName has correct parameter type for $parameterName" {
166-
$parameterHelp.parameterValueGroup.parameterValue | Should be $names
167-
}
168-
$testparamserrors += 1
169-
}
124+
It "help for $commandName has correct parameter type for $parameterName" -TestCases @{ parameterHelp = $parameterHelp; names = $names } {
125+
$parameterHelp.parameterValueGroup.parameterValue | Should -be $names
126+
}
170127
}
171128
else {
172129
# To avoid calling Trim method on a null object.
173130
$helpType = if ($parameterHelp.parameterValue) { $parameterHelp.parameterValue.Trim() }
174-
if ($helpType -ne $codeType) {
175-
# Parameter type in Help should match code
176-
It "help for $commandName has correct parameter type for $parameterName" {
177-
$helpType | Should be $codeType
178-
}
179-
$testparamserrors += 1
180-
}
131+
# Parameter type in Help should match code
132+
It "help for $commandName has correct parameter type for $parameterName" -TestCases @{ helpType = $helpType; codeType = $codeType } {
133+
$helpType | Should -be $codeType
134+
}
181135
}
182136
}
183137
foreach ($helpParm in $HelpParameterNames) {
184-
$testparamsall += 1
185-
if ($helpParm -notin $parameterNames) {
186-
# Shouldn't find extra parameters in help.
187-
It "finds help parameter in code: $helpParm" {
188-
$helpParm -in $parameterNames | Should Be $true
189-
}
190-
$testparamserrors += 1
191-
}
192-
}
193-
if ($testparamserrors -eq 0) {
194-
It "Ran silently $testparamsall tests" {
195-
$testparamserrors | Should be 0
196-
}
138+
# Shouldn't find extra parameters in help.
139+
It "finds help parameter in code: $helpParm" -TestCases @{ helpParm = $helpParm; parameterNames = $parameterNames } {
140+
$helpParm -in $parameterNames | Should -Be $true
141+
}
197142
}
198143
}
199144
}

PSModuleDevelopment/tests/general/PSScriptAnalyzer.Tests.ps1

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,15 @@ Param (
44
$SkipTest,
55

66
[string[]]
7-
$CommandPath = @("$PSScriptRoot\..\..\functions", "$PSScriptRoot\..\..\internal\functions")
7+
$CommandPath = @("$global:testroot\..\functions", "$global:testroot\..\internal\functions")
88
)
99

1010
if ($SkipTest) { return }
1111

12-
if ($env:BUILD_BUILDURI -like "vstfs*") { Install-Module PSScriptAnalyzer -Force -SkipPublisherCheck }
13-
14-
$list = New-Object System.Collections.ArrayList
12+
$global:__pester_data.ScriptAnalyzer = New-Object System.Collections.ArrayList
1513

1614
Describe 'Invoking PSScriptAnalyzer against commandbase' {
17-
$commandFiles = Get-ChildItem -Path $CommandPath -Recurse -Filter "*.ps1"
15+
$commandFiles = Get-ChildItem -Path $CommandPath -Recurse | Where-Object Name -like "*.ps1"
1816
$scriptAnalyzerRules = Get-ScriptAnalyzerRule
1917

2018
foreach ($file in $commandFiles)
@@ -24,10 +22,10 @@ Describe 'Invoking PSScriptAnalyzer against commandbase' {
2422

2523
forEach ($rule in $scriptAnalyzerRules)
2624
{
27-
It "Should pass $rule" {
25+
It "Should pass $rule" -TestCases @{ analysis = $analysis; rule = $rule } {
2826
If ($analysis.RuleName -contains $rule)
2927
{
30-
$analysis | Where-Object RuleName -EQ $rule -outvariable failures | ForEach-Object { $list.Add($_) }
28+
$analysis | Where-Object RuleName -EQ $rule -outvariable failures | ForEach-Object { $null = $global:__pester_data.ScriptAnalyzer.Add($_) }
3129

3230
1 | Should -Be 0
3331
}
@@ -39,6 +37,4 @@ Describe 'Invoking PSScriptAnalyzer against commandbase' {
3937
}
4038
}
4139
}
42-
}
43-
44-
$list | Out-Default
40+
}

0 commit comments

Comments
 (0)