Skip to content

Commit cbba081

Browse files
committed
Add some meta tests. Added basics tests tasks. Updated build.ps1 to it will cause CI/CD system to fail on failed pester tests.
1 parent 96241cc commit cbba081

7 files changed

Lines changed: 358 additions & 5 deletions

File tree

Build.ps1

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ function PrerequisitesLoaded {
4343
return $false
4444
}
4545
}
46+
4647
function CleanUp {
4748
try {
4849
Write-Host ''
@@ -84,7 +85,7 @@ switch ($psCmdlet.ParameterSetName) {
8485
}
8586
catch {
8687
Write-Host 'Build Failed with the following error:'
87-
Write-Host $_
88+
throw $_
8889
}
8990
}
9091

@@ -95,7 +96,7 @@ switch ($psCmdlet.ParameterSetName) {
9596
}
9697
catch {
9798
Write-Host 'Install and test of module failed:'
98-
Write-Host $_
99+
throw $_
99100
}
100101
}
101102

ModuleBuild.build.ps1

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ if ($Script:BuildEnv.OptionTranscriptEnabled) {
4545
Start-Transcript -Path $TranscriptLog -Append -WarningAction:SilentlyContinue
4646
}
4747

48+
#$Script:BuildRoot = $BuildRoot
4849
#region Configure
4950
#Synopsis: Validate system requirements are met
5051
task ValidateRequirements {
@@ -241,6 +242,36 @@ task RunPSScriptAnalyzeOnPublicSrcFunctions {
241242
}
242243
#endregion
243244

245+
#region Tests
246+
task RunMetaTests LoadRequiredModules, {
247+
Write-Description White 'Running meta tests with Pester' -accent
248+
$ENV:BuildRoot = $BuildRoot
249+
$invokePesterParams = @{
250+
Strict = $true
251+
PassThru = $true
252+
Verbose = $false
253+
EnableExit = $false
254+
}
255+
$testResults = Invoke-Pester -Tag 'MetaTest' $(Join-Path $BuildRoot 'Tests') @invokePesterParams
256+
$numberFails = $testResults.FailedCount
257+
assert($numberFails -eq 0) ('Failed "{0}" meta tests.' -f $numberFails)
258+
}
259+
260+
task RunUnitTests LoadRequiredModules, {
261+
Write-Description White 'Running meta tests with Pester' -accent
262+
$ENV:BuildRoot = $BuildRoot
263+
$invokePesterParams = @{
264+
Strict = $true
265+
PassThru = $true
266+
Verbose = $false
267+
EnableExit = $false
268+
}
269+
$testResults = Invoke-Pester -tag 'UnitTest' $(Join-Path $BuildRoot 'Tests') @invokePesterParams
270+
$numberFails = $testResults.FailedCount
271+
assert($numberFails -eq 0) ('Failed "{0}" unit tests.' -f $numberFails)
272+
}
273+
#endregion
274+
244275
#region Documentation/Help files
245276
# Synopsis: Build the markdown help files with PlatyPS
246277
task CreateMarkdownHelp GetPublicFunctions, {
@@ -826,6 +857,10 @@ task BuildSessionCleanup CleanScratchDirectory, {
826857
#endregion
827858

828859
#region Main tasks
860+
# Synopsis: Run all tests
861+
task Tests RunMetaTests, RunUnitTests, {
862+
863+
}
829864
# Synopsis: Build the module
830865
task Build Configure, CodeHealthReport, PrepareStage, GetPublicFunctions, SanitizeCode, CreateHelp, CreateModulePSM1, CreateModuleManifest, AnalyzeModuleRelease, PushVersionRelease, PushCurrentRelease, CreateProjectHelp, PostBuildTasks, BuildSessionCleanup, {
831866

@@ -847,5 +882,5 @@ task AddMissingCBH Configure, CleanScratchDirectory, InsertCBHInPublicFunctions,
847882
}
848883

849884
# Synopsis: Default task when running Invoke-Build
850-
task . Build
885+
task . Tests, Build
851886
#endregion

plugins/plaster/template/scaffold/Build.template

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ switch ($psCmdlet.ParameterSetName) {
8585
}
8686
catch {
8787
Write-Host 'Build Failed with the following error:'
88-
Write-Host $_
88+
throw $_
8989
}
9090
}
9191

@@ -96,7 +96,7 @@ switch ($psCmdlet.ParameterSetName) {
9696
}
9797
catch {
9898
Write-Host 'Install and test of module failed:'
99-
Write-Host $_
99+
throw $_
100100
}
101101
}
102102

tests/Meta/Help.tests.ps1

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# Taken with love from @juneb_get_help (https://raw.githubusercontent.com/juneb/PesterTDD/master/Module.Help.Tests.ps1)
2+
3+
# $outputDir = Join-Path -Path $ENV:BHProjectPath -ChildPath 'out'
4+
# $outputModDir = Join-Path -Path $outputDir -ChildPath $env:BHProjectName
5+
# $manifest = Import-PowerShellDataFile -Path $env:BHPSModuleManifest
6+
# $outputModVerDir = Join-Path -Path $outputModDir -ChildPath $manifest.ModuleVersion
7+
#
8+
# # Get module commands
9+
# # Remove all versions of the module from the session. Pester can't handle multiple versions.
10+
# #Get-Module $env:BHProjectName | Remove-Module -Force
11+
# #Import-Module -Name (Join-Path -Path $outputModVerDir -ChildPath "$($env:BHProjectName).psd1") -Verbose:$false -ErrorAction Stop
12+
# $commands = Get-Command -Module (Get-Module $env:BHProjectName) -CommandType Cmdlet, Function, Workflow # Not alias
13+
#
14+
# ## When testing help, remember that help is cached at the beginning of each session.
15+
# ## To test, restart session.
16+
#
17+
# foreach ($command in $commands) {
18+
# $commandName = $command.Name
19+
#
20+
# # The module-qualified command fails on Microsoft.PowerShell.Archive cmdlets
21+
# $help = Get-Help $commandName -ErrorAction SilentlyContinue
22+
#
23+
# Describe "Test help for $commandName" -Tags @('MetaTest') {
24+
#
25+
# # If help is not found, synopsis in auto-generated help is the syntax diagram
26+
# It 'should not be auto-generated' {
27+
# $help.Synopsis | Should Not BeLike '*`[`<CommonParameters`>`]*'
28+
# }
29+
#
30+
# # Should be a description for every function
31+
# It "gets description for $commandName" {
32+
# $help.Description | Should Not BeNullOrEmpty
33+
# }
34+
#
35+
# # Should be at least one example
36+
# It "gets example code from $commandName" {
37+
# ($help.Examples.Example | Select-Object -First 1).Code | Should Not BeNullOrEmpty
38+
# }
39+
#
40+
# # Should be at least one example description
41+
# It "gets example help from $commandName" {
42+
# ($help.Examples.Example.Remarks | Select-Object -First 1).Text | Should Not BeNullOrEmpty
43+
# }
44+
#
45+
# Context "Test parameter help for $commandName" {
46+
#
47+
# $common = 'Debug', 'ErrorAction', 'ErrorVariable', 'InformationAction', 'InformationVariable', 'OutBuffer',
48+
# 'OutVariable', 'PipelineVariable', 'Verbose', 'WarningAction', 'WarningVariable', 'Confirm', 'Whatif'
49+
#
50+
# $parameters = $command.ParameterSets.Parameters |
51+
# Sort-Object -Property Name -Unique |
52+
# Where-Object { $_.Name -notin $common }
53+
# $parameterNames = $parameters.Name
54+
#
55+
# ## Without the filter, WhatIf and Confirm parameters are still flagged in "finds help parameter in code" test
56+
# $helpParameters = $help.Parameters.Parameter |
57+
# Where-Object { $_.Name -notin $common } |
58+
# Sort-Object -Property Name -Unique
59+
# $helpParameterNames = $helpParameters.Name
60+
#
61+
# foreach ($parameter in $parameters) {
62+
# $parameterName = $parameter.Name
63+
# $parameterHelp = $help.parameters.parameter | Where-Object Name -EQ $parameterName
64+
#
65+
# # Should be a description for every parameter
66+
# It "gets help for parameter: $parameterName : in $commandName" {
67+
# $parameterHelp.Description.Text | Should Not BeNullOrEmpty
68+
# }
69+
#
70+
# # Required value in Help should match IsMandatory property of parameter
71+
# It "help for $parameterName parameter in $commandName has correct Mandatory value" {
72+
# $codeMandatory = $parameter.IsMandatory.toString()
73+
# $parameterHelp.Required | Should Be $codeMandatory
74+
# }
75+
#
76+
# # Parameter type in Help should match code
77+
# # It "help for $commandName has correct parameter type for $parameterName" {
78+
# # $codeType = $parameter.ParameterType.Name
79+
# # # To avoid calling Trim method on a null object.
80+
# # $helpType = if ($parameterHelp.parameterValue) { $parameterHelp.parameterValue.Trim() }
81+
# # $helpType | Should be $codeType
82+
# # }
83+
# }
84+
#
85+
# foreach ($helpParm in $HelpParameterNames) {
86+
# # Shouldn't find extra parameters in help.
87+
# It "finds help parameter in code: $helpParm" {
88+
# $helpParm -in $parameterNames | Should Be $true
89+
# }
90+
# }
91+
# }
92+
#
93+
# Context "Help Links should be Valid for $commandName" {
94+
# $link = $help.relatedLinks.navigationLink.uri
95+
#
96+
# foreach ($link in $links) {
97+
# if ($link) {
98+
# # Should have a valid uri if one is provided.
99+
# it "[$link] should have 200 Status Code for $commandName" {
100+
# $Results = Invoke-WebRequest -Uri $link -UseBasicParsing
101+
# $Results.StatusCode | Should Be '200'
102+
# }
103+
# }
104+
# }
105+
# }
106+
# }
107+
# }

tests/Meta/Manifest.tests.ps1

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Taken with love from https://github.com/poshbotio/PoshBot/blob/master/Tests/Manifest.tests.ps1
2+
3+
# $moduleName = $env:BHProjectName
4+
# $manifest = Import-PowerShellDataFile -Path $env:BHPSModuleManifest
5+
# $outputDir = Join-Path -Path $ENV:BHProjectPath -ChildPath 'out'
6+
# $outputModDir = Join-Path -Path $outputDir -ChildPath $env:BHProjectName
7+
# $outputModVerDir = Join-Path -Path $outputModDir -ChildPath $manifest.ModuleVersion
8+
# $outputManifestPath = Join-Path -Path $outputModVerDir -Child "$($moduleName).psd1"
9+
# $changelogPath = Join-Path -Path $env:BHProjectPath -Child 'CHANGELOG.md'
10+
#
11+
# Describe 'Module manifest' -Tags @('MetaTest') {
12+
# Context 'Validation' {
13+
#
14+
# $script:manifest = $null
15+
#
16+
# It 'has a valid manifest' {
17+
# {
18+
# $script:manifest = Test-ModuleManifest -Path $outputManifestPath -Verbose:$false -ErrorAction Stop -WarningAction SilentlyContinue
19+
# } | Should Not Throw
20+
# }
21+
#
22+
# It 'has a valid name in the manifest' {
23+
# $script:manifest.Name | Should Be $env:BHProjectName
24+
# }
25+
#
26+
# It 'has a valid root module' {
27+
# $script:manifest.RootModule | Should Be "$($moduleName).psm1"
28+
# }
29+
#
30+
# It 'has a valid version in the manifest' {
31+
# $script:manifest.Version -as [Version] | Should Not BeNullOrEmpty
32+
# }
33+
#
34+
# It 'has a valid description' {
35+
# $script:manifest.Description | Should Not BeNullOrEmpty
36+
# }
37+
#
38+
# It 'has a valid author' {
39+
# $script:manifest.Author | Should Not BeNullOrEmpty
40+
# }
41+
#
42+
# It 'has a valid guid' {
43+
# {
44+
# [guid]::Parse($script:manifest.Guid)
45+
# } | Should Not throw
46+
# }
47+
#
48+
# It 'has a valid copyright' {
49+
# $script:manifest.CopyRight | Should Not BeNullOrEmpty
50+
# }
51+
#
52+
# # Only for DSC modules
53+
# # It 'exports DSC resources' {
54+
# # $dscResources = ($Manifest.psobject.Properties | Where Name -eq 'ExportedDscResources').Value
55+
# # @($dscResources).Count | Should Not Be 0
56+
# # }
57+
#
58+
# $script:changelogVersion = $null
59+
# It 'has a valid version in the changelog' {
60+
# foreach ($line in (Get-Content $changelogPath)) {
61+
# if ($line -match "^##\s\[(?<Version>(\d+\.){1,3}\d+)\]") {
62+
# $script:changelogVersion = $matches.Version
63+
# break
64+
# }
65+
# }
66+
# $script:changelogVersion | Should Not BeNullOrEmpty
67+
# $script:changelogVersion -as [Version] | Should Not BeNullOrEmpty
68+
# }
69+
#
70+
# It 'changelog and manifest versions are the same' {
71+
# $script:changelogVersion -as [Version] | Should be ( $script:manifest.Version -as [Version] )
72+
# }
73+
#
74+
# if (Get-Command git.exe -ErrorAction SilentlyContinue) {
75+
# $script:tagVersion = $null
76+
# It 'is tagged with a valid version' -skip {
77+
# $thisCommit = git.exe log --decorate --oneline HEAD~1..HEAD
78+
#
79+
# if ($thisCommit -match 'tag:\s*(\d+(?:\.\d+)*)') {
80+
# $script:tagVersion = $matches[1]
81+
# }
82+
#
83+
# $script:tagVersion | Should Not BeNullOrEmpty
84+
# $script:tagVersion -as [Version] | Should Not BeNullOrEmpty
85+
# }
86+
#
87+
# It 'all versions are the same' {
88+
# $script:changelogVersion -as [Version] | Should be ( $script:manifest.Version -as [Version] )
89+
# #$script:manifest.Version -as [Version] | Should be ( $script:tagVersion -as [Version] )
90+
# }
91+
# }
92+
# }
93+
# }

tests/Meta/Meta.tests.ps1

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Taken with love from https://raw.githubusercontent.com/poshbotio/PoshBot/master/Tests/Meta.tests.ps1
2+
Set-StrictMode -Version latest
3+
4+
# Make sure MetaFixers.psm1 is loaded - it contains Get-TextFilesList
5+
Import-Module -Name (Join-Path -Path $PSScriptRoot -ChildPath 'MetaFixers.psm1') -Verbose:$false -Force
6+
7+
$projectRoot = $ENV:BuildRoot
8+
9+
if(-not $projectRoot) {
10+
$projectRoot = $PSScriptRoot
11+
}
12+
13+
Describe 'Text files formatting' -Tags @('MetaTest') {
14+
15+
$allTextFiles = Get-TextFilesList $projectRoot
16+
17+
Context 'Files encoding' {
18+
It "Doesn't use Unicode encoding" {
19+
$unicodeFilesCount = 0
20+
$allTextFiles | Foreach-Object {
21+
if (Test-FileUnicode $_) {
22+
$unicodeFilesCount += 1
23+
Write-Warning "File $($_.FullName) contains 0x00 bytes. It's probably uses Unicode and need to be converted to UTF-8. Use Fixer 'Get-UnicodeFilesList `$pwd | ConvertTo-UTF8'."
24+
}
25+
}
26+
$unicodeFilesCount | Should Be 0
27+
}
28+
}
29+
30+
Context 'Indentations' {
31+
It 'Uses spaces for indentation, not tabs' {
32+
$totalTabsCount = 0
33+
$allTextFiles | Foreach-Object {
34+
$fileName = $_.FullName
35+
(Get-Content $_.FullName -Raw) | Select-String "`t" | Foreach-Object {
36+
Write-Warning "There are tab in $fileName. Use Fixer 'Get-TextFilesList `$pwd | ConvertTo-SpaceIndentation'."
37+
$totalTabsCount++
38+
}
39+
}
40+
$totalTabsCount | Should Be 0
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)