Tests/Module.Test.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 |
$ProjectRoot = (Resolve-Path "$PSScriptRoot\..").path $ModuleName = Split-Path $ProjectRoot -Leaf $ModuleRoot = "$ProjectRoot\$ModuleName.psm1" $ModuleRoot = $(gci $ModuleRoot).Fullname $ManifestRoot = "$ProjectRoot\$ModuleName.psd1" BeforeAll { Try{ Import-module PSScriptAnalyzer -ErrorAction STOP } Catch{ Install-Module -Name PSScriptAnalyzer } IF(!(Get-Module PSScriptAnalyzer)){ Install-Module -Name PSScriptAnalyzer -Force } #$Scripts = Get-ChildItem -Include *.ps1 -Exclude *WiP.ps1 -Recurse $ProjectRoot = (Resolve-Path "$PSScriptRoot\..").path $ModuleName = Split-Path $ProjectRoot -Leaf $ModuleRoot = "$ProjectRoot\$ModuleName.psm1" $ManifestRoot = "$ProjectRoot\$ModuleName.psd1" } Describe "General project validation: $ModuleName" { $Scripts = Get-ChildItem $ProjectRoot -Include *.ps1 -Exclude *WiP.ps1 -Recurse # TestCases are splatted to the script $TestCase = $Scripts | Foreach-Object {@{file=$_}} It "Script <file> should be valid powershell" -TestCases $TestCase { param($File) $File.Fullname | Should -Exist $Contents = Get-Content -Path $File.Fullname -ErrorAction Stop $Errors = $null $null = [System.Management.Automation.PSParser]::Tokenize($Contents, [ref]$Errors) $Errors.Count | Should -Be 0 } It "Script <file> should exist" -TestCases $TestCase { param($File) Test-Path $File.Fullname | Should -Be $True } It "Script <file> should have help block" -TestCases $TestCase { param($File) $File.Fullname | Should -FileContentMatch '<#' $File.Fullname | Should -FileContentMatch '#>' } It "Script <file> should have Synopsis" -TestCases $TestCase { param($File) $File.Fullname | Should -FileContentMatch '.SYNOPSIS' } It "Script <file> should have help DESCRIPTION" -TestCases $TestCase { param($File) $File.Fullname | Should -FileContentMatch 'DESCRIPTION' } It "Script <file> should have help EXAMPLE" -TestCases $TestCase { param($File) $File.Fullname | Should -FileContentMatch 'EXAMPLE' } It "Test-Path $Moduleroot should be True" { Test-Path $Moduleroot | Should -Be $True } It "Test-Path $ManifestRoot should be True" { Test-Path $ManifestRoot | Should -Be $True } it 'Passes all default PSScriptAnalyzer rules' { Invoke-ScriptAnalyzer -Path $(gci $ModuleRoot).Fullname | should -BeNullOrEmpty } } |