-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #335 from fflaten/update-tests
- Loading branch information
Showing
10 changed files
with
221 additions
and
210 deletions.
There are no files selected for viewing
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
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
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
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
6 changes: 2 additions & 4 deletions
6
...ests/functions/Invoke-MtMaester.Tests.ps1 → .../tests/functions/Invoke-Maester.Tests.ps1
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,7 @@ | ||
Describe 'Invoke-MtMaester' { | ||
Describe 'Invoke-Maester' { | ||
It 'Not connected to graph should return error' { | ||
|
||
if (Get-MgContext) { Disconnect-Graph } # Ensure we are disconnected | ||
|
||
{ Invoke-MtMaester } | Should -Throw "Not connected to Microsoft Graph.*" | ||
{ Invoke-Maester } | Should -Throw 'Not connected to Microsoft Graph.*' | ||
} | ||
} | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,56 @@ | ||
Describe "Validating the module manifest" { | ||
$moduleRoot = (Resolve-Path "$global:testroot\..").Path | ||
$manifest = ((Get-Content "$moduleRoot\Maester.psd1") -join "`n") | Invoke-Expression | ||
Context "Basic resources validation" { | ||
$files = Get-ChildItem "$moduleRoot\public" -Recurse -File | Where-Object Name -like "*.ps1" | ||
It "Exports all functions in the public folder" -TestCases @{ files = $files; manifest = $manifest } { | ||
|
||
$functions = (Compare-Object -ReferenceObject $files.BaseName -DifferenceObject $manifest.FunctionsToExport | Where-Object SideIndicator -Like '<=').InputObject | ||
$functions | Should -BeNullOrEmpty | ||
} | ||
It "Exports no function that isn't also present in the public folder" -TestCases @{ files = $files; manifest = $manifest } { | ||
$functions = (Compare-Object -ReferenceObject $files.BaseName -DifferenceObject $manifest.FunctionsToExport | Where-Object SideIndicator -Like '=>').InputObject | ||
$functions | Should -BeNullOrEmpty | ||
} | ||
|
||
It "Exports none of its internal functions" -TestCases @{ moduleRoot = $moduleRoot; manifest = $manifest } { | ||
$files = Get-ChildItem "$moduleRoot/internal" -Recurse -File -Filter "*.ps1" | ||
$files | Where-Object BaseName -In $manifest.FunctionsToExport | Should -BeNullOrEmpty | ||
} | ||
} | ||
|
||
Context "Individual file validation" { | ||
It "The root module file exists" -TestCases @{ moduleRoot = $moduleRoot; manifest = $manifest } { | ||
Test-Path "$moduleRoot\$($manifest.RootModule)" | Should -Be $true | ||
} | ||
|
||
foreach ($format in $manifest.FormatsToProcess) | ||
{ | ||
It "The file $format should exist" -TestCases @{ moduleRoot = $moduleRoot; format = $format } { | ||
Test-Path "$moduleRoot\$format" | Should -Be $true | ||
} | ||
} | ||
|
||
foreach ($type in $manifest.TypesToProcess) | ||
{ | ||
It "The file $type should exist" -TestCases @{ moduleRoot = $moduleRoot; type = $type } { | ||
Test-Path "$moduleRoot\$type" | Should -Be $true | ||
} | ||
} | ||
|
||
foreach ($tag in $manifest.PrivateData.PSData.Tags) | ||
{ | ||
It "Tags should have no spaces in name" -TestCases @{ tag = $tag } { | ||
$tag -match " " | Should -Be $false | ||
} | ||
} | ||
} | ||
BeforeDiscovery { | ||
$moduleRoot = "$PSScriptRoot/../.." | ||
# Using Import-PowerShellDataFile over Test-ModuleManifest as it's easier to navigate | ||
$manifest = Import-PowerShellDataFile -Path (Join-Path -Path $moduleRoot -ChildPath 'Maester.psd1') | ||
} | ||
|
||
Describe 'Validating the module manifest' -ForEach @{ moduleRoot = $moduleRoot; manifest = $manifest } { | ||
Context 'Basic resources validation' { | ||
BeforeAll { | ||
$files = Get-ChildItem -Path "$moduleRoot/public" -Recurse -File -Filter '*.ps1' | ||
} | ||
|
||
It 'Manifest is valid' { | ||
Test-ModuleManifest -Path (Join-Path -Path $moduleRoot -ChildPath 'Maester.psd1') | ||
# Throws if not valid = failure. Success if not. | ||
} | ||
|
||
It 'Exports all functions in the public folder' { | ||
$functions = (Compare-Object -ReferenceObject $files.BaseName -DifferenceObject $manifest.FunctionsToExport | Where-Object SideIndicator -Like '<=').InputObject | ||
$functions | Should -BeNullOrEmpty | ||
} | ||
It "Exports no function that isn't also present in the public folder" { | ||
$functions = (Compare-Object -ReferenceObject $files.BaseName -DifferenceObject $manifest.FunctionsToExport | Where-Object SideIndicator -Like '=>').InputObject | ||
$functions | Should -BeNullOrEmpty | ||
} | ||
|
||
It 'Exports none of its internal functions' { | ||
$files = Get-ChildItem "$moduleRoot/internal" -Recurse -File -Filter '*.ps1' | ||
$files | Where-Object BaseName -In $manifest.FunctionsToExport | Should -BeNullOrEmpty | ||
} | ||
} | ||
|
||
Context 'Testing tags' { | ||
It "Tag '<_>' should not include whitespace" -ForEach @($manifest.PrivateData.PSData.Tags) { | ||
$_ | Should -Not -Match '\s' | ||
} | ||
} | ||
|
||
Context 'Individual file validation' { | ||
It 'The root module file exists' { | ||
Join-Path -Path $moduleRoot -ChildPath $manifest.RootModule | Should -Exist | ||
} | ||
|
||
Context 'Testing format files' -Skip:$(-not $manifest.ContainsKey('FormatsToProcess')) { | ||
It 'The file <_> should exist' -ForEach $manifest.FormatsToProcess { | ||
Join-Path -Path $moduleRoot -ChildPath $_ | Should -Exist | ||
} | ||
} | ||
|
||
Context 'Testing types files' -Skip:$(-not $manifest.ContainsKey('TypesToProcess')) { | ||
It 'The file <_> should exist' -ForEach $manifest.TypesToProcess { | ||
Join-Path -Path $moduleRoot -ChildPath $_ | Should -Exist | ||
} | ||
} | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,51 @@ | ||
[CmdletBinding()] | ||
Param ( | ||
[switch] | ||
$SkipTest, | ||
Param ( | ||
[switch] | ||
$SkipTest, | ||
|
||
[string[]] | ||
$CommandPath = @("$global:testroot\..\public", "$global:testroot\..\internal") | ||
[string[]] | ||
$CommandPath = @("$PSScriptRoot/../../public/", "$PSScriptRoot/../../internal/") | ||
) | ||
|
||
if ($SkipTest) { return } | ||
|
||
$global:__pester_data.ScriptAnalyzer = New-Object System.Collections.ArrayList | ||
BeforeDiscovery { | ||
$commandFiles = Get-ChildItem -Path $CommandPath -Recurse -File -Filter '*.ps1' | ||
$scriptAnalyzerRules = Get-ScriptAnalyzerRule | ||
} | ||
|
||
Describe 'Invoking PSScriptAnalyzer against commandbase' { | ||
$commandFiles = foreach ($path in $CommandPath) { Get-ChildItem -Path $path -Recurse | Where-Object Name -like "*.ps1" } | ||
$scriptAnalyzerRules = Get-ScriptAnalyzerRule | ||
Describe 'Invoking PSScriptAnalyzer against commandbase' -ForEach @{ commandFiles = $commandFiles } { | ||
BeforeAll { | ||
$analysis = $commandFiles | Invoke-ScriptAnalyzer -ExcludeRule PSAvoidTrailingWhitespace, PSShouldProcess | ||
} | ||
|
||
foreach ($file in $commandFiles) | ||
{ | ||
Context "Analyzing $($file.BaseName)" { | ||
$analysis = Invoke-ScriptAnalyzer -Path $file.FullName -ExcludeRule PSAvoidTrailingWhitespace, PSShouldProcess | ||
# The next Context blocks are kinda duplicate, but helps us document both | ||
# which files and which rules where evaluated without running every rule for every file | ||
Context 'Analyzing rule <_.RuleName>' -ForEach $scriptAnalyzerRules { | ||
BeforeAll { | ||
$rule = $_ | ||
} | ||
It 'All files should be compliant' { | ||
$failedFiles = foreach ($failure in $analysis) { | ||
if ($failure.RuleName -eq $rule.RuleName) { | ||
$failure.ScriptPath | ||
} | ||
} | ||
$failedFiles | Should -BeNullOrEmpty | ||
} | ||
} | ||
|
||
forEach ($rule in $scriptAnalyzerRules) | ||
{ | ||
It "Should pass $rule" -TestCases @{ analysis = $analysis; rule = $rule } { | ||
If ($analysis.RuleName -contains $rule) | ||
{ | ||
$analysis | Where-Object RuleName -EQ $rule -outvariable failures | ForEach-Object { $null = $global:__pester_data.ScriptAnalyzer.Add($_) } | ||
|
||
1 | Should -Be 0 | ||
} | ||
else | ||
{ | ||
0 | Should -Be 0 | ||
} | ||
} | ||
} | ||
} | ||
} | ||
Context 'Analyzing file <_.BaseName>' -ForEach $commandFiles { | ||
BeforeAll { | ||
$file = $_ | ||
} | ||
It "Should pass all rules" -Tag 'ScriptAnalyzerRule' { | ||
$failedRules = foreach ($failure in $analysis) { | ||
if ($failure.ScriptPath -eq $file.FullName) { | ||
$failure | ||
} | ||
} | ||
$failedRules # Intentional output so we can get it from StandardOutput-property in pester.ps1 | ||
@($failedRules).RuleName | Should -BeNullOrEmpty | ||
} | ||
} | ||
} |
Oops, something went wrong.