|
| 1 | +# Integration tests for Test-PSBuildPester (psake/PowerShellBuild#102). |
| 2 | +# |
| 3 | +# Test-PSBuildPester wraps Invoke-Pester, so these tests are Pester-testing-Pester. Every |
| 4 | +# invocation runs in a Start-Job subprocess: two Pester versions cannot coexist in one session, |
| 5 | +# and the subprocess lets each test pin the inner Pester version independently of the outer |
| 6 | +# framework. The scenarios run against every installed Pester major (5.x and 6.x) to verify the |
| 7 | +# shipped function keeps supporting Pester 5 consumers. |
| 8 | +# |
| 9 | +# The crash fixtures are generated into $TestDrive at runtime, never checked in, so the |
| 10 | +# repository's own Pester run can never discover them (see #97 for the convention). |
| 11 | + |
| 12 | +BeforeDiscovery { |
| 13 | + # Newest installed Pester of each supported major version. CI installs 6.x (Pester) and |
| 14 | + # 5.x (PesterLegacy) via requirements.psd1; locally, absent majors simply produce fewer |
| 15 | + # matrix legs. |
| 16 | + $script:innerPesterVersions = @( |
| 17 | + foreach ($majorVersion in 5, 6) { |
| 18 | + $newestOfMajor = Get-Module -Name 'Pester' -ListAvailable | |
| 19 | + Where-Object { $_.Version.Major -eq $majorVersion } | |
| 20 | + Sort-Object -Property 'Version' -Descending | |
| 21 | + Select-Object -First 1 |
| 22 | + if ($newestOfMajor) { |
| 23 | + $newestOfMajor.Version.ToString() |
| 24 | + } |
| 25 | + } |
| 26 | + ) |
| 27 | +} |
| 28 | + |
| 29 | +Describe 'Test-PSBuildPester' { |
| 30 | + |
| 31 | + BeforeAll { |
| 32 | + $script:moduleRoot = Split-Path -Path $PSScriptRoot -Parent |
| 33 | + $script:builtModulePath = [IO.Path]::Combine($script:moduleRoot, 'Output', 'PowerShellBuild') |
| 34 | + |
| 35 | + Import-Module -Name ([IO.Path]::Combine($PSScriptRoot, 'fixtures', 'FixtureHelpers.psm1')) -Force |
| 36 | + |
| 37 | + # Runs Test-PSBuildPester in a subprocess with a pinned inner Pester version and reports |
| 38 | + # what happened. Returns an object with Threw, ErrorMessage, and the Pester version that |
| 39 | + # was loaded in the subprocess after the call. |
| 40 | + function script:Invoke-TestPSBuildPesterJob { |
| 41 | + param( |
| 42 | + [string]$InnerPesterVersion, |
| 43 | + [string]$Path, |
| 44 | + [hashtable]$AdditionalParameters = @{} |
| 45 | + ) |
| 46 | + |
| 47 | + $job = Start-Job -ScriptBlock { |
| 48 | + param($innerPesterVersion, $builtModulePath, $path, $additionalParameters) |
| 49 | + |
| 50 | + Import-Module -Name 'Pester' -RequiredVersion $innerPesterVersion -ErrorAction Stop |
| 51 | + Import-Module -Name $builtModulePath -Force -ErrorAction Stop |
| 52 | + |
| 53 | + $testPSBuildPesterParameters = @{ |
| 54 | + Path = $path |
| 55 | + OutputVerbosity = 'None' |
| 56 | + ErrorAction = 'Stop' |
| 57 | + } |
| 58 | + foreach ($key in $additionalParameters.Keys) { |
| 59 | + $testPSBuildPesterParameters[$key] = $additionalParameters[$key] |
| 60 | + } |
| 61 | + |
| 62 | + $threw = $false |
| 63 | + $errorMessage = $null |
| 64 | + try { |
| 65 | + Test-PSBuildPester @testPSBuildPesterParameters |
| 66 | + } catch { |
| 67 | + $threw = $true |
| 68 | + $errorMessage = $_.Exception.Message |
| 69 | + } |
| 70 | + |
| 71 | + [PSCustomObject]@{ |
| 72 | + Threw = $threw |
| 73 | + ErrorMessage = $errorMessage |
| 74 | + LoadedPesterVersions = @((Get-Module -Name 'Pester').Version.ToString()) |
| 75 | + } |
| 76 | + } -ArgumentList $InnerPesterVersion, $script:builtModulePath, $Path, $AdditionalParameters |
| 77 | + |
| 78 | + $jobResult = $job | Wait-Job | Receive-Job |
| 79 | + Remove-Job -Job $job -Force |
| 80 | + $jobResult |
| 81 | + } |
| 82 | + |
| 83 | + # Scenario directories, generated at runtime. |
| 84 | + $script:healthyPath = Join-Path -Path $TestDrive -ChildPath 'healthy' |
| 85 | + $script:failingTestPath = Join-Path -Path $TestDrive -ChildPath 'failingtest' |
| 86 | + $script:beforeAllCrashPath = Join-Path -Path $TestDrive -ChildPath 'beforeallcrash' |
| 87 | + $script:discoveryCrashPath = Join-Path -Path $TestDrive -ChildPath 'discoverycrash' |
| 88 | + $script:coveragePath = Join-Path -Path $TestDrive -ChildPath 'coverage' |
| 89 | + $script:outputPath = Join-Path -Path $TestDrive -ChildPath 'out' |
| 90 | + foreach ($directory in @( |
| 91 | + $script:healthyPath |
| 92 | + $script:failingTestPath |
| 93 | + $script:beforeAllCrashPath |
| 94 | + $script:discoveryCrashPath |
| 95 | + $script:coveragePath |
| 96 | + $script:outputPath |
| 97 | + )) { |
| 98 | + New-Item -Path $directory -ItemType Directory -Force > $null |
| 99 | + } |
| 100 | + |
| 101 | + Set-Content -Path (Join-Path -Path $script:healthyPath -ChildPath 'Healthy.tests.ps1') -Value @' |
| 102 | +Describe 'Healthy suite' { |
| 103 | + It 'passes' { |
| 104 | + 1 | Should -Be 1 |
| 105 | + } |
| 106 | +} |
| 107 | +'@ |
| 108 | + |
| 109 | + Set-Content -Path (Join-Path -Path $script:failingTestPath -ChildPath 'FailingTest.tests.ps1') -Value @' |
| 110 | +Describe 'Suite with a failing test' { |
| 111 | + It 'fails' { |
| 112 | + 1 | Should -Be 2 |
| 113 | + } |
| 114 | +} |
| 115 | +'@ |
| 116 | + |
| 117 | + Set-Content -Path (Join-Path -Path $script:beforeAllCrashPath -ChildPath 'BeforeAllCrash.tests.ps1') -Value @' |
| 118 | +Describe 'Suite with a broken setup' { |
| 119 | + BeforeAll { |
| 120 | + throw 'BeforeAll exploded' |
| 121 | + } |
| 122 | +
|
| 123 | + It 'never executes' { |
| 124 | + 1 | Should -Be 1 |
| 125 | + } |
| 126 | +} |
| 127 | +'@ |
| 128 | + |
| 129 | + Set-Content -Path (Join-Path -Path $script:discoveryCrashPath -ChildPath 'DiscoveryCrash.tests.ps1') -Value @' |
| 130 | +throw 'file exploded during discovery' |
| 131 | +
|
| 132 | +Describe 'Unreachable suite' { |
| 133 | + It 'is never discovered' { |
| 134 | + 1 | Should -Be 1 |
| 135 | + } |
| 136 | +} |
| 137 | +'@ |
| 138 | + |
| 139 | + # Coverage scenario: tests exercising the fixture module, with coverage measured on the |
| 140 | + # fixture's public functions. |
| 141 | + $script:fixturePath = Copy-PSBuildTestFixture -Destination $TestDrive |
| 142 | + $fixtureManifestPath = Join-Path -Path $script:fixturePath -ChildPath 'PSBuildTestFixture.psd1' |
| 143 | + Set-Content -Path (Join-Path -Path $script:coveragePath -ChildPath 'Coverage.tests.ps1') -Value @" |
| 144 | +BeforeAll { |
| 145 | + Import-Module -Name '$fixtureManifestPath' -Force |
| 146 | +} |
| 147 | +
|
| 148 | +Describe 'Coverage target' { |
| 149 | + It 'calls Get-Widget' { |
| 150 | + (Get-Widget -Name 'Sprocket').Name | Should -Be 'Sprocket' |
| 151 | + } |
| 152 | +} |
| 153 | +"@ |
| 154 | + } |
| 155 | + |
| 156 | + AfterAll { |
| 157 | + Remove-Module -Name 'FixtureHelpers' -Force -ErrorAction SilentlyContinue |
| 158 | + } |
| 159 | + |
| 160 | + Context 'with inner Pester <_>' -ForEach $script:innerPesterVersions { |
| 161 | + |
| 162 | + BeforeAll { |
| 163 | + $script:innerVersion = $_ |
| 164 | + } |
| 165 | + |
| 166 | + It 'succeeds for a healthy suite' { |
| 167 | + $result = Invoke-TestPSBuildPesterJob -InnerPesterVersion $script:innerVersion -Path $script:healthyPath |
| 168 | + |
| 169 | + $result.Threw | Should -BeFalse |
| 170 | + } |
| 171 | + |
| 172 | + It 'fails the build when a test fails' { |
| 173 | + # Regression: #52 |
| 174 | + $result = Invoke-TestPSBuildPesterJob -InnerPesterVersion $script:innerVersion -Path $script:failingTestPath |
| 175 | + |
| 176 | + $result.Threw | Should -BeTrue |
| 177 | + $result.ErrorMessage | Should -Match 'Pester tests failed' |
| 178 | + } |
| 179 | + |
| 180 | + It 'fails the build when a setup block throws' { |
| 181 | + # Regression: #128 / #133 (FailedCount alone misses failed blocks) |
| 182 | + $result = Invoke-TestPSBuildPesterJob -InnerPesterVersion $script:innerVersion -Path $script:beforeAllCrashPath |
| 183 | + |
| 184 | + $result.Threw | Should -BeTrue |
| 185 | + $result.ErrorMessage | Should -Match 'Pester tests failed' |
| 186 | + } |
| 187 | + |
| 188 | + It 'fails the build when a test file errors during discovery' { |
| 189 | + # Regression: #128 / #133 (FailedCount alone misses failed containers) |
| 190 | + $result = Invoke-TestPSBuildPesterJob -InnerPesterVersion $script:innerVersion -Path $script:discoveryCrashPath |
| 191 | + |
| 192 | + $result.Threw | Should -BeTrue |
| 193 | + $result.ErrorMessage | Should -Match 'Pester tests failed' |
| 194 | + } |
| 195 | + |
| 196 | + It 'writes test results to the requested output path' { |
| 197 | + $testResultsPath = Join-Path -Path $script:outputPath -ChildPath "testResults-$script:innerVersion.xml" |
| 198 | + $additionalParameters = @{ |
| 199 | + OutputPath = $testResultsPath |
| 200 | + } |
| 201 | + $result = Invoke-TestPSBuildPesterJob -InnerPesterVersion $script:innerVersion -Path $script:healthyPath -AdditionalParameters $additionalParameters |
| 202 | + |
| 203 | + $result.Threw | Should -BeFalse |
| 204 | + $testResultsPath | Should -Exist |
| 205 | + } |
| 206 | + |
| 207 | + It 'writes code coverage in the requested format to the requested path' { |
| 208 | + # Regression: #62 |
| 209 | + $coverageOutputPath = Join-Path -Path $script:outputPath -ChildPath "coverage-$script:innerVersion.xml" |
| 210 | + $additionalParameters = @{ |
| 211 | + CodeCoverage = $true |
| 212 | + CodeCoverageFiles = @(Join-Path -Path $script:fixturePath -ChildPath 'Public/*.ps1') |
| 213 | + CodeCoverageOutputFile = $coverageOutputPath |
| 214 | + CodeCoverageOutputFileFormat = 'JaCoCo' |
| 215 | + } |
| 216 | + $result = Invoke-TestPSBuildPesterJob -InnerPesterVersion $script:innerVersion -Path $script:coveragePath -AdditionalParameters $additionalParameters |
| 217 | + |
| 218 | + $result.Threw | Should -BeFalse |
| 219 | + $coverageOutputPath | Should -Exist |
| 220 | + [xml]$coverageReport = Get-Content -Path $coverageOutputPath -Raw |
| 221 | + $coverageReport.report | Should -Not -BeNullOrEmpty |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + # BeforeDiscovery variables are not visible during the run phase, so the discovered version |
| 226 | + # list is handed to the run phase through -ForEach. |
| 227 | + Context 'Regressions independent of the inner Pester version' -ForEach @( |
| 228 | + @{ AvailableVersions = $script:innerPesterVersions } |
| 229 | + ) { |
| 230 | + |
| 231 | + BeforeAll { |
| 232 | + $script:newestInnerVersion = $AvailableVersions | Select-Object -Last 1 |
| 233 | + $script:oldestInnerVersion = $AvailableVersions | Select-Object -First 1 |
| 234 | + } |
| 235 | + |
| 236 | + It 'does not error when ModuleName is not provided' { |
| 237 | + # Regression: the finally block called Remove-Module with an empty -Name, which |
| 238 | + # raised a parameter-binding error that -ErrorAction SilentlyContinue cannot |
| 239 | + # suppress. |
| 240 | + $result = Invoke-TestPSBuildPesterJob -InnerPesterVersion $script:newestInnerVersion -Path $script:healthyPath |
| 241 | + |
| 242 | + $result.Threw | Should -BeFalse |
| 243 | + $result.ErrorMessage | Should -BeNullOrEmpty |
| 244 | + } |
| 245 | + |
| 246 | + It 'honors the Pester version that is already loaded' -Skip:($script:innerPesterVersions.Count -lt 2) { |
| 247 | + # Regression: an unconditional Import-Module Pester -MinimumVersion 5.0.0 loaded the |
| 248 | + # newest installed Pester on top of an already-loaded older one, which crashes with a |
| 249 | + # Pester.dll version conflict when 5.x and 6.x are installed side by side. |
| 250 | + $result = Invoke-TestPSBuildPesterJob -InnerPesterVersion $script:oldestInnerVersion -Path $script:healthyPath |
| 251 | + |
| 252 | + $result.Threw | Should -BeFalse |
| 253 | + $result.LoadedPesterVersions | Should -Be @($script:oldestInnerVersion) |
| 254 | + } |
| 255 | + } |
| 256 | +} |
0 commit comments