Last active
August 20, 2020 15:57
-
-
Save ephos/0d710171ac9ca87a17112f6e88117187 to your computer and use it in GitHub Desktop.
Module Tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Describe -Name 'Module Structure' { | |
BeforeAll { | |
# Test setup - Pester v5 Setup Syntax | |
$moduleRoot = (Get-Item -Path $PSCommandPath).Directory.Parent.FullName | |
$modulePath = Join-Path -Path $moduleRoot -ChildPath \src\ | |
$moduleName = (Get-Item -Path "$modulePath\*.psd1").BaseName | |
$moduleManifest = Join-Path -Path $modulePath -ChildPath "$moduleName.psd1" | |
$manifest = Test-ModuleManifest -Path $moduleManifest -ErrorAction Stop -WarningAction SilentlyContinue | |
$allFunctions = [System.Collections.Generic.List[System.IO.FileInfo]]::new() | |
$functionsPrivatePath = Join-Path -Path $modulePath -ChildPath 'Functions\Private\' | |
if (Test-Path -Path $functionsPrivatePath) { | |
$functionsPublicPath = Join-Path -Path $modulePath -ChildPath 'Functions\Public\' | |
$functionsPrivate = Get-ChildItem -Path "$functionsPrivatePath\*.ps1" | |
$functionsPrivate | ForEach-Object {$allFunctions.Add($_)} | |
} else { | |
$functionsPublicPath = Join-Path -Path $modulePath -ChildPath 'Functions\' | |
} | |
$functionsPublic = Get-ChildItem -Path "$functionsPublicPath\*.ps1" | |
$functionsPublic | ForEach-Object {$allFunctions.Add($_)} | |
} | |
Context 'Module Manifest' { | |
It -Name 'has a valid manifest' { | |
{ $manifest | Should -Not -Throw } | |
} | |
It -Name 'has a valid name in the manifest' { | |
$manifest.Name | Should -Be $moduleName | |
} | |
It -Name 'has a valid root module' { | |
$manifest.RootModule | Should -Be ($moduleName + ".psm1") | |
} | |
It -Name 'has a zero version in the manifest for CICD pipeline' { | |
$manifest.Version.ToString() | Should -Be '0.0.0' | |
} | |
It -Name 'has a valid description' { | |
$manifest.Description | Should -Not -BeNullOrEmpty | |
} | |
It -Name 'has a valid author' { | |
$manifest.Author | Should -Not -BeNullOrEmpty | |
} | |
It -Name 'has a valid guid' { | |
{ [guid]::Parse($manifest.Guid) } | Should -Not -Throw | |
} | |
It -Name 'has a valid copyright' { | |
$manifest.CopyRight | Should -Not -BeNullOrEmpty | |
} | |
} | |
Context "Module Functions" { | |
It -Name 'has the same number of exported public functions for function ps1 files' { | |
($manifest.ExportedFunctions.GetEnumerator() | Measure-Object).Count | Should -Be ($functionsPublic | Measure-Object).Count | |
} | |
foreach ($function in $allFunctions) { | |
It -Name "$($function.BaseName) function has no syntax errors" -TestCases @{function = $function} { | |
$functionContents = $null | |
$psParserErrorOutput = $null | |
$functionContents = Get-Content -Path $function.FullName | |
[System.Management.Automation.PSParser]::Tokenize($functionContents, [ref]$psParserErrorOutput) | |
($psParserErrorOutput | Measure-Object).Count | Should -Be 0 | |
Clear-Variable -Name psParserErrorOutput -Force -ErrorAction Stop | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment