|
| 1 | +[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Justification = 'Variables used in Pester test contexts')] |
| 2 | +param() |
| 3 | + |
| 4 | +BeforeAll { |
| 5 | + # Determine the built module path dynamically |
| 6 | + $moduleName = 'PSScriptModule' |
| 7 | + $modulePath = Resolve-Path -Path (Join-Path -Path $PSScriptRoot -ChildPath "../../build/out/$moduleName") |
| 8 | + $manifestPath = Join-Path -Path $modulePath -ChildPath "$moduleName.psd1" |
| 9 | + |
| 10 | + # Import the built module |
| 11 | + if ($manifestPath -and (Test-Path $manifestPath)) { |
| 12 | + Import-Module $manifestPath -Force -ErrorAction Stop |
| 13 | + } else { |
| 14 | + throw "Built module not found at: $manifestPath. Run 'Invoke-Build' first." |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +AfterAll { |
| 19 | + # Clean up |
| 20 | + Remove-Module 'PSScriptModule' -ErrorAction SilentlyContinue |
| 21 | +} |
| 22 | + |
| 23 | +Describe 'PSScriptModule Integration Tests' -Tag 'Integration' { |
| 24 | + |
| 25 | + Context 'Module Loading' { |
| 26 | + It 'Should load the module successfully' { |
| 27 | + $module = Get-Module -Name 'PSScriptModule' |
| 28 | + $module | Should -Not -BeNullOrEmpty |
| 29 | + } |
| 30 | + |
| 31 | + It 'Should have module manifest or psm1' { |
| 32 | + $module = Get-Module -Name 'PSScriptModule' |
| 33 | + $module.Path | Should -Match '\.(psd1|psm1)$' |
| 34 | + } |
| 35 | + |
| 36 | + It 'Should have correct module name' { |
| 37 | + $module = Get-Module -Name 'PSScriptModule' |
| 38 | + $module.Name | Should -Be 'PSScriptModule' |
| 39 | + } |
| 40 | + |
| 41 | + It 'Should have a valid version' { |
| 42 | + $module = Get-Module -Name 'PSScriptModule' |
| 43 | + $module.Version | Should -Not -BeNullOrEmpty |
| 44 | + $module.Version | Should -BeOfType [version] |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + Context 'Exported Functions' { |
| 49 | + BeforeAll { |
| 50 | + $module = Get-Module -Name 'PSScriptModule' |
| 51 | + $exportedCommands = $module.ExportedCommands.Keys |
| 52 | + |
| 53 | + # Discover public functions from source |
| 54 | + $publicFunctionsPath = Resolve-Path -Path (Join-Path -Path $PSScriptRoot -ChildPath '../../src/Public/') |
| 55 | + $publicFunctionFiles = @(Get-ChildItem -Path $publicFunctionsPath -Include '*.ps1' -Exclude '*.Tests.ps1' -Recurse | |
| 56 | + Select-Object -ExpandProperty BaseName) |
| 57 | + } |
| 58 | + |
| 59 | + It 'Should export at least one function' { |
| 60 | + $exportedCommands.Count | Should -BeGreaterThan 0 |
| 61 | + } |
| 62 | + |
| 63 | + It 'Public function files' { |
| 64 | + $publicFunctionFiles | Should -HaveCount 1 -Because 'The template ships with a single public function' |
| 65 | + } |
| 66 | + |
| 67 | + It 'Should discover public functions from src/Public' { |
| 68 | + $publicFunctionFiles.Count | Should -BeGreaterThan 0 -Because 'src/Public should contain at least one public function' |
| 69 | + } |
| 70 | + |
| 71 | + It 'Should export all public functions from src/Public' { |
| 72 | + foreach ($functionName in $publicFunctionFiles) { |
| 73 | + $exportedCommands | Should -Contain $functionName -Because "Public function $functionName should be exported" |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + It 'Should not export private functions' { |
| 78 | + $privateFunctionsPath = Join-Path $PSScriptRoot '../../src/Private' |
| 79 | + if (Test-Path $privateFunctionsPath) { |
| 80 | + $privateFiles = Get-ChildItem -Path $privateFunctionsPath -Filter '*.ps1' -Exclude '*.Tests.ps1' | |
| 81 | + Select-Object -ExpandProperty BaseName |
| 82 | + |
| 83 | + foreach ($functionName in $privateFiles) { |
| 84 | + $exportedCommands | Should -Not -Contain $functionName -Because "Private function $functionName should not be exported" |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + It 'Should have help for all exported functions' { |
| 90 | + foreach ($command in $exportedCommands) { |
| 91 | + $help = Get-Help $command |
| 92 | + $help.Synopsis | Should -Not -BeNullOrEmpty -Because "Function $command should have synopsis" |
| 93 | + $help.Description | Should -Not -BeNullOrEmpty -Because "Function $command should have description" |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + It 'Should have help for each public function dynamically' { |
| 98 | + foreach ($functionName in $publicFunctionFiles) { |
| 99 | + $help = Get-Help $functionName |
| 100 | + $help.Synopsis | Should -Not -BeNullOrEmpty -Because "Public function $functionName should have synopsis" |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + Context 'Module Metadata' { |
| 106 | + BeforeAll { |
| 107 | + $module = Get-Module -Name 'PSScriptModule' |
| 108 | + } |
| 109 | + |
| 110 | + It 'Should have an author' { |
| 111 | + $module.Author | Should -Not -BeNullOrEmpty |
| 112 | + } |
| 113 | + |
| 114 | + It 'Should have a description' { |
| 115 | + $module.Description | Should -Not -BeNullOrEmpty |
| 116 | + } |
| 117 | + |
| 118 | + It 'Should have a company name' { |
| 119 | + $module.CompanyName | Should -Not -BeNullOrEmpty |
| 120 | + } |
| 121 | + |
| 122 | + It 'Should have a copyright' { |
| 123 | + $module.Copyright | Should -Not -BeNullOrEmpty |
| 124 | + } |
| 125 | + |
| 126 | + It 'Should specify PowerShell version' { |
| 127 | + $module = Get-Module -Name 'PSScriptModule' |
| 128 | + # PowerShellVersion may not be set for all modules |
| 129 | + if ($module.PowerShellVersion) { |
| 130 | + $module.PowerShellVersion | Should -BeOfType [version] |
| 131 | + } else { |
| 132 | + Set-ItResult -Skipped -Because 'PowerShellVersion not specified in manifest' |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + It 'Should have project URI' { |
| 137 | + $module.ProjectUri | Should -Not -BeNullOrEmpty |
| 138 | + $module.ProjectUri.AbsoluteUri | Should -Match '^https?://' |
| 139 | + } |
| 140 | + |
| 141 | + It 'Should have license URI' { |
| 142 | + $module.LicenseUri | Should -Not -BeNullOrEmpty |
| 143 | + $module.LicenseUri.AbsoluteUri | Should -Match '^https?://' |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + Context 'Performance' { |
| 148 | + It 'Should load module in reasonable time' { |
| 149 | + $loadTime = Measure-Command { |
| 150 | + Remove-Module 'PSScriptModule' -ErrorAction SilentlyContinue |
| 151 | + # Use the same module path resolution as BeforeAll |
| 152 | + Import-Module $manifestPath -Force |
| 153 | + } |
| 154 | + |
| 155 | + $loadTime.TotalSeconds | Should -BeLessThan 5 -Because 'Module should load within 5 seconds' |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments