Skip to content

Commit fa24024

Browse files
committed
chore: ✨ Fix up tests
* Build tests now run in TempDir * TestModule moved into fixtures folder. * Added test for docs
1 parent b0c8fee commit fa24024

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+245
-192
lines changed

.vscode/tasks.json

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,42 @@
22
// See https://go.microsoft.com/fwlink/?LinkId=733558
33
// for the documentation about the tasks.json format
44
"version": "2.0.0",
5-
65
// Start PowerShell (pwsh on *nix)
76
"windows": {
87
"options": {
98
"shell": {
10-
"executable": "powershell.exe",
11-
"args": [ "-NoProfile", "-ExecutionPolicy", "Bypass", "-Command" ]
9+
"executable": "pwsh.exe",
10+
"args": [
11+
"-NoProfile",
12+
"-ExecutionPolicy",
13+
"Bypass",
14+
"-Command"
15+
]
1216
}
1317
}
1418
},
1519
"linux": {
1620
"options": {
1721
"shell": {
1822
"executable": "/usr/bin/pwsh",
19-
"args": [ "-NoProfile", "-Command" ]
23+
"args": [
24+
"-NoProfile",
25+
"-Command"
26+
]
2027
}
2128
}
2229
},
2330
"osx": {
2431
"options": {
2532
"shell": {
2633
"executable": "/usr/local/bin/pwsh",
27-
"args": [ "-NoProfile", "-Command" ]
34+
"args": [
35+
"-NoProfile",
36+
"-Command"
37+
]
2838
}
2939
}
3040
},
31-
3241
"tasks": [
3342
{
3443
"label": "Clean",

PowerShellBuild/Public/Publish-PSBuildModule.ps1

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
function Publish-PSBuildModule {
2-
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute(
3-
'PSReviewUnusedParameter',
4-
'',
5-
Justification = 'Both Credential and NuGetApiKey are used just not via explicit variable call.'
6-
)]
72
<#
83
.SYNOPSIS
94
Publishes a module to the defined PowerShell repository.
@@ -32,6 +27,11 @@ function Publish-PSBuildModule {
3227
3328
Publish version 0.1.0 of the module at path .\Output\0.1.0\MyModule to the PSGallery repository using an API key and a PowerShell credential.
3429
#>
30+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute(
31+
'PSReviewUnusedParameter',
32+
'',
33+
Justification = 'Both Credential and NuGetApiKey are used just not via explicit variable call.'
34+
)]
3535
[CmdletBinding(DefaultParameterSetName = 'ApiKey')]
3636
param(
3737
[parameter(Mandatory)]

PowerShellBuild/Public/Test-PSBuildPester.ps1

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
function Test-PSBuildPester {
2-
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute(
3-
'PSReviewUnusedParameter',
4-
'CodeCoverageThreshold',
5-
Justification = 'Used inside a foreach method call.'
6-
)]
72
<#
83
.SYNOPSIS
94
Execute Pester tests for module.
@@ -37,6 +32,11 @@ function Test-PSBuildPester {
3732
Run Pester tests in ./tests and save results to ./out/testResults.xml
3833
#>
3934
[CmdletBinding()]
35+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute(
36+
'PSReviewUnusedParameter',
37+
'CodeCoverageThreshold',
38+
Justification = 'Used inside a foreach method call.'
39+
)]
4040
param(
4141
[parameter(Mandatory)]
4242
[string]$Path,

psakeFile.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Task Init {
1616
Task Test -depends Init, Analyze, Pester -description 'Run test suite'
1717

1818
Task Analyze -depends Build {
19-
$analysis = Invoke-ScriptAnalyzer -Path $settings.ModuleOutDir -Recurse -Verbose:$false -Settings ([IO.Path]::Combine('tests', 'ScriptAnalyzerSettings.psd1'))
19+
$analysis = Invoke-ScriptAnalyzer -Path $settings.ModuleOutDir -Recurse -Verbose:$false -Settings './tests/ScriptAnalyzerSettings.psd1'
2020
$errors = $analysis | Where-Object { $_.Severity -eq 'Error' }
2121
$warnings = $analysis | Where-Object { $_.Severity -eq 'Warning' }
2222
if (@($errors).Count -gt 0) {

requirements.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
}
55
BuildHelpers = '2.0.16'
66
Pester = @{
7-
MinimumVersion = '5.6.1'
7+
MinimumVersion = '5.7.1'
88
Parameters = @{
99
SkipPublisherCheck = $true
1010
}

tests/Help.tests.ps1

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
# Taken with love from @juneb_get_help (https://raw.githubusercontent.com/juneb/PesterTDD/master/Module.Help.Tests.ps1)
22

33
BeforeDiscovery {
4+
45
function global:FilterOutCommonParams {
56
param ($Params)
6-
$commonParams = [System.Management.Automation.PSCmdlet]::OptionalCommonParameters +
7-
[System.Management.Automation.PSCmdlet]::CommonParameters
8-
$params | Where-Object { $_.Name -notin $commonParams } | Sort-Object -Property Name -Unique
7+
$commonParameters = [System.Management.Automation.PSCmdlet]::CommonParameters + [System.Management.Automation.PSCmdlet]::OptionalCommonParameters
8+
$params | Where-Object { $_.Name -notin $commonParameters } | Sort-Object -Property Name -Unique
99
}
1010

11-
$manifest = Import-PowerShellDataFile -Path $env:BHPSModuleManifest
12-
$outputDir = Join-Path -Path $env:BHProjectPath -ChildPath 'Output'
13-
$outputModDir = Join-Path -Path $outputDir -ChildPath $env:BHProjectName
14-
$outputModVerDir = Join-Path -Path $outputModDir -ChildPath $manifest.ModuleVersion
11+
$manifest = Import-PowerShellDataFile -Path $env:BHPSModuleManifest
12+
$outputDir = Join-Path -Path $env:BHProjectPath -ChildPath 'Output'
13+
$outputModDir = Join-Path -Path $outputDir -ChildPath $env:BHProjectName
14+
$outputModVerDir = Join-Path -Path $outputModDir -ChildPath $manifest.ModuleVersion
1515
$outputModVerManifest = Join-Path -Path $outputModVerDir -ChildPath "$($env:BHProjectName).psd1"
1616

1717
# Get module commands
@@ -25,36 +25,32 @@ BeforeDiscovery {
2525
if ($PSVersionTable.PSVersion.Major -lt 6) {
2626
$params.CommandType[0] += 'Workflow'
2727
}
28-
$commands = Get-Command @params
28+
$script:commands = Get-Command @params
2929

3030
## When testing help, remember that help is cached at the beginning of each session.
3131
## To test, restart session.
3232
}
3333

34-
AfterAll {
35-
Remove-Item Function:/FilterOutCommonParams
36-
}
37-
3834
Describe "Test help for <_.Name>" -ForEach $commands {
3935

4036
BeforeDiscovery {
4137
# Get command help, parameters, and links
42-
$command = $_
43-
$commandHelp = Get-Help $command.Name -ErrorAction SilentlyContinue
44-
$commandParameters = global:FilterOutCommonParams -Params $command.ParameterSets.Parameters
45-
$commandParameterNames = $commandParameters.Name
46-
$helpLinks = $commandHelp.relatedLinks.navigationLink.uri
38+
$command = $_
39+
$commandHelp = Get-Help $command.Name -ErrorAction SilentlyContinue
40+
$commandParameters = global:FilterOutCommonParams -Params $command.ParameterSets.Parameters
41+
$script:commandParameterNames = $commandParameters.Name
42+
$script:helpLinks = $commandHelp.relatedLinks.navigationLink.uri
4743
}
4844

4945
BeforeAll {
5046
# These vars are needed in both discovery and test phases so we need to duplicate them here
51-
$command = $_
52-
$commandName = $_.Name
53-
$commandHelp = Get-Help $command.Name -ErrorAction SilentlyContinue
54-
$commandParameters = global:FilterOutCommonParams -Params $command.ParameterSets.Parameters
55-
$commandParameterNames = $commandParameters.Name
56-
$helpParameters = global:FilterOutCommonParams -Params $commandHelp.Parameters.Parameter
57-
$helpParameterNames = $helpParameters.Name
47+
$command = $_
48+
$script:commandName = $_.Name
49+
$commandHelp = Get-Help $command.Name -ErrorAction SilentlyContinue
50+
$commandParameters = global:FilterOutCommonParams -Params $command.ParameterSets.Parameters
51+
$script:commandParameterNames = $commandParameters.Name
52+
$helpParameters = global:FilterOutCommonParams -Params $commandHelp.Parameters.Parameter
53+
$script:helpParameterNames = $helpParameters.Name
5854
}
5955

6056
# If help is not found, synopsis in auto-generated help is the syntax diagram
@@ -81,13 +77,13 @@ Describe "Test help for <_.Name>" -ForEach $commands {
8177
(Invoke-WebRequest -Uri $_ -UseBasicParsing).StatusCode | Should -Be '200'
8278
}
8379

84-
Context "Parameter <_.Name>" -Foreach $commandParameters {
80+
Context "Parameter <_.Name>" -ForEach $commandParameters {
8581

8682
BeforeAll {
87-
$parameter = $_
88-
$parameterName = $parameter.Name
89-
$parameterHelp = $commandHelp.parameters.parameter | Where-Object Name -eq $parameterName
90-
$parameterHelpType = if ($parameterHelp.ParameterValue) { $parameterHelp.ParameterValue.Trim() }
83+
$parameter = $_
84+
$parameterName = $parameter.Name
85+
$parameterHelp = $commandHelp.parameters.parameter | Where-Object Name -EQ $parameterName
86+
$script:parameterHelpType = if ($parameterHelp.ParameterValue) { $parameterHelp.ParameterValue.Trim() }
9187
}
9288

9389
# Should be a description for every parameter
@@ -107,7 +103,7 @@ Describe "Test help for <_.Name>" -ForEach $commands {
107103
}
108104
}
109105

110-
Context "Test <_> help parameter help for <commandName>" -Foreach $helpParameterNames {
106+
Context "Test <_> help parameter help for <commandName>" -ForEach $helpParameterNames {
111107

112108
# Shouldn't find extra parameters in help.
113109
It "finds help parameter in code: <_>" {

tests/Manifest.tests.ps1

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
BeforeAll {
2+
Set-Location "$PSScriptRoot/.."
23
Set-BuildEnvironment -Force
3-
$moduleName = $env:BHProjectName
4-
$manifest = Import-PowerShellDataFile -Path $env:BHPSModuleManifest
5-
$outputDir = Join-Path -Path $ENV:BHProjectPath -ChildPath 'Output'
6-
$outputModDir = Join-Path -Path $outputDir -ChildPath $env:BHProjectName
7-
$outputModVerDir = Join-Path -Path $outputModDir -ChildPath $manifest.ModuleVersion
4+
$moduleName = $env:BHProjectName
5+
$manifest = Import-PowerShellDataFile -Path $env:BHPSModuleManifest
6+
$outputDir = Join-Path -Path $ENV:BHProjectPath -ChildPath 'Output'
7+
$outputModDir = Join-Path -Path $outputDir -ChildPath $env:BHProjectName
8+
$outputModVerDir = Join-Path -Path $outputModDir -ChildPath $manifest.ModuleVersion
89
$outputManifestPath = Join-Path -Path $outputModVerDir -Child "$($moduleName).psd1"
9-
$manifestData = Test-ModuleManifest -Path $outputManifestPath -Verbose:$false -ErrorAction Stop -WarningAction SilentlyContinue
10+
$manifestData = Test-ModuleManifest -Path $outputManifestPath -Verbose:$false -ErrorAction Stop -WarningAction SilentlyContinue
1011

11-
$changelogPath = Join-Path -Path $env:BHProjectPath -Child 'CHANGELOG.md'
12+
$changelogPath = Join-Path -Path $env:BHProjectPath -Child 'CHANGELOG.md'
1213
$changelogVersion = Get-Content $changelogPath | ForEach-Object {
1314
if ($_ -match "^##\s\[(?<Version>(\d+\.){1,3}\d+)\]") {
1415
$changelogVersion = $matches.Version
1516
break
1617
}
1718
}
1819

19-
$script:manifest = $null
20+
$script:manifest = $null
2021
}
2122
Describe 'Module manifest' {
2223

@@ -47,15 +48,15 @@ Describe 'Module manifest' {
4748
}
4849

4950
It 'Has a valid guid' {
50-
{[guid]::Parse($manifestData.Guid)} | Should -Not -Throw
51+
{ [guid]::Parse($manifestData.Guid) } | Should -Not -Throw
5152
}
5253

5354
It 'Has a valid copyright' {
5455
$manifestData.CopyRight | Should -Not -BeNullOrEmpty
5556
}
5657

5758
It 'Has a valid version in the changelog' {
58-
$changelogVersion | Should -Not -BeNullOrEmpty
59+
$changelogVersion | Should -Not -BeNullOrEmpty
5960
$changelogVersion -as [Version] | Should -Not -BeNullOrEmpty
6061
}
6162

@@ -76,7 +77,7 @@ Describe 'Git tagging' -Skip {
7677
}
7778

7879
It 'Is tagged with a valid version' {
79-
$gitTagVersion | Should -Not -BeNullOrEmpty
80+
$gitTagVersion | Should -Not -BeNullOrEmpty
8081
$gitTagVersion -as [Version] | Should -Not -BeNullOrEmpty
8182
}
8283

tests/TestModule/Tests/Help.tests.ps1

Lines changed: 0 additions & 93 deletions
This file was deleted.

0 commit comments

Comments
 (0)