Tests/Orbit.Users.tests.ps1
# Module: Orbit.Users # Function: Test # Author: David Eberhardt # Updated: 03-JUN 2023 # Script Analyzer Exceptions #[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUserDeclaredVarsMoreThanAssignments', '', Justification = 'Context Boundaries')] BeforeDiscovery { # Finding relevant Paths from $PSScriptRoot $ModuleRoot = ($PsScriptRoot -split '\\Tests')[0] $ModuleName = Split-Path -Leaf $ModuleRoot $OrbitSrc = Split-Path -Parent $ModuleRoot $OrbitDirs = Get-ChildItem -Path $OrbitSrc -Directory | Sort-Object Name -Descending $OrbitModules = $OrbitDirs.Basename $OrbitModules | ForEach-Object { if ( -not (Get-Module $_) ) { Import-Module "$OrbitSrc\$_\$_.psd1" -Force } } # Generate command list for generating Context / TestCases $Module = Get-Module $ModuleName $CommandList = @( $Module.CommandList.Keys $Module.ExportedCmdlets.Keys ) Write-Output "Module '$ModuleName' - $($CommandList.count) exported Commands found" # Determining Functions $PublicFunctions = Get-ChildItem "$ModuleRoot\Private" -Include '*.ps1' -Exclude '*.Tests.ps1' -Recurse Write-Output "Module '$ModuleName' - $($PublicFunctions.Count) public functions discovered" $PrivateFunctions = Get-ChildItem "$ModuleRoot\Private" -Include '*.ps1' -Exclude '*.Tests.ps1' -Recurse Write-Output "Module '$ModuleName' - $($PrivateFunctions.Count) private functions discovered" } BeforeAll { # Need to do this here again, otherwise it will not disover ModuleRoot correctly $ModuleRoot = ($PsScriptRoot -split '\\Tests')[0] $ModuleName = Split-Path -Leaf $ModuleRoot } # Unit Tests Describe -Tags ('Unit', 'Acceptance') "Module '$ModuleName'" { Context "Validating Module Framework" { It "has the root module '$ModuleName`.psm1'" { "$ModuleRoot\$ModuleName.psm1" | Should -Exist } It "has the root module '$ModuleName`.psm1'" { "$ModuleRoot\$ModuleName.psm1" | Should -Exist } It "has the a manifest file of '$ModuleName`.psd1'" { "$ModuleRoot\$ModuleName.psd1" | Should -Exist } It "$ModuleName has folder for Functions" { "$ModuleRoot\Public" | Should -Exist "$ModuleRoot\Private" | Should -Exist } It "$ModuleName has folder for Tests" { "$ModuleRoot\Tests\Unit\Public" | Should -Exist "$ModuleRoot\Tests\Unit\Private" | Should -Exist } It "$ModuleName is valid PowerShell code" { $psFile = Get-Content -Path "$ModuleRoot\$ModuleName.psm1" -ErrorAction Stop $errors = $null $null = [System.Management.Automation.PSParser]::Tokenize($psFile, [ref]$errors) $errors.Count | Should -Be 0 } } } <#BODGE DOES NOT WORK Describe -Tags ('Unit') "Module '$ModuleName' - Command Help" -ForEach $PublicFunctions { BeforeAll { $Command = $_ } Context "$Command - Help Content" { BeforeEach { $Help = @{ Help = Get-Help -Name $Command -Full | Select-Object -Property * } $Parameters = Get-Help -Name $Command -Parameter * -ErrorAction Ignore | Where-Object { $_.Name -and $_.Name -notin $ShouldProcessParameters } | ForEach-Object { @{ Name = $_.name Description = $_.Description.Text } } $Ast = @{ # Ast will be $null if the command is a compiled cmdlet Ast = (Get-Content -Path "function:/$Command" -ErrorAction Ignore).Ast Parameters = $Parameters } $Examples = $Help.Help.Examples.Example | ForEach-Object { @{ Example = $_ } } } It "has help content for $Command" -TestCases $Help { $Help | Should -Not -BeNullOrEmpty } It "contains a synopsis for $Command" -TestCases $Help { $Help.Synopsis | Should -Not -BeNullOrEmpty } It "contains a description for $Command" -TestCases $Help { $Help.Description | Should -Not -BeNullOrEmpty } } } #> <#BODGE DOES NOT WORK Describe -Tags ('Unit') "Module '$ModuleName' - Command Validation" -ForEach $PublicFunctions { BeforeAll { $Command = $_ } Context "$Command - Location & Test File" { It "Command '$_' should be saved as a separate file in Public" { #$Item = Get-ChildItem -Path "$ModuleRoot\Public" -Include "$_`.ps1" -Exclude '*.Tests.ps1' -Recurse $Item = Get-ChildItem -Path "$moduleroot\Public" -Filter "$Command`.ps1" -Exclude '*.Tests.ps1' -Recurse $Item.Count | Should -Be 1 } It "Command '$Command' file should only be found once in the module directory" { $Item = Get-ChildItem -Path $ModuleRoot -Include "$Command`.ps1" -Exclude '*.Tests.ps1' -Recurse $Item.Count | Should -Be 1 } It "Command '$Command' should have an appropriate Tests.ps1 file in Tests" { $Item = Get-ChildItem -Path "$ModuleRoot\Tests\Unit\Public" -Include "$Command`.Tests.ps1" -Exclude '*.ps1' -Recurse $Item.Count | Should -Be 1 } } } #> <#BODGE DOES NOT WORK Describe -Tags ('Unit') "Module '$ModuleName' - Command Validation" -ForEach $PrivateFunctions { BeforeAll { $Command = $_.BaseName } Context "'$Command' - Validating Private Function" { It "Command '$Command' should be saved as a separate file in Private" { $Item = Get-ChildItem -Path "$ModuleRoot\Private" -Include "$Command`.ps1" -Exclude '*.Tests.ps1' -Recurse $Item | Should -Exist } It "Command '$Command' file should only be found once in the module directory" { $Item = Get-ChildItem -Path "$ModuleRoot" -Include "$Command`.ps1" -Exclude '*.Tests.ps1' -Recurse $Item.Count | Should -Be 1 } It "Command '$Command' should have an appropriate Tests.ps1 file in Tests" { $Item = Get-ChildItem -Path "$ModuleRoot\Tests\Unit\Private" -Include "$Command`.Tests.ps1" -Exclude '*.ps1' -Recurse $Item.Count | Should -Be 1 } } } #> |