|
| 1 | +BeforeAll { |
| 2 | + $script:repoRoot = Split-Path -Parent $PSScriptRoot |
| 3 | + $script:srcRoot = Join-Path $script:repoRoot 'src' |
| 4 | + $script:findMatches = { |
| 5 | + param( |
| 6 | + [Parameter(Mandatory)][string]$Pattern, |
| 7 | + [Parameter(Mandatory)][string]$RootPath |
| 8 | + ) |
| 9 | + |
| 10 | + $matches = foreach ($file in (Get-ChildItem -LiteralPath $RootPath -Filter '*.ps1' -Recurse -File)) { |
| 11 | + foreach ($match in (Select-String -Path $file.FullName -Pattern $Pattern)) { |
| 12 | + [pscustomobject]@{ |
| 13 | + Path = ([System.IO.Path]::GetRelativePath($script:repoRoot, $file.FullName)).Replace('\', '/') |
| 14 | + Line = $match.LineNumber |
| 15 | + Text = $match.Line.Trim() |
| 16 | + } |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + return @($matches) |
| 21 | + } |
| 22 | + $script:getMatchedPaths = { |
| 23 | + param([AllowNull()][object[]]$MatchList) |
| 24 | + |
| 25 | + if ($null -eq $MatchList) { |
| 26 | + return @() |
| 27 | + } |
| 28 | + |
| 29 | + return @($MatchList | ForEach-Object Path | Sort-Object -Unique) |
| 30 | + } |
| 31 | + $script:formatMatches = { |
| 32 | + param([AllowNull()][object[]]$MatchList) |
| 33 | + |
| 34 | + if ($null -eq $MatchList) { |
| 35 | + return 'No matches.' |
| 36 | + } |
| 37 | + |
| 38 | + return ($MatchList | ForEach-Object { |
| 39 | + "$( $_.Path ):$( $_.Line ) -> $( $_.Text )" |
| 40 | + }) -join [Environment]::NewLine |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +Describe 'Architecture guardrails' { |
| 45 | + It 'public commands stay free of raw infrastructure primitives' { |
| 46 | + $matches = & $script:findMatches -RootPath (Join-Path $script:srcRoot 'public') -Pattern 'ConvertFrom-Json|ConvertTo-Json|Invoke-WebRequest|Invoke-RestMethod|Update-Module|&\s*git\b|\$env:|GetEnvironmentVariable\(' |
| 47 | + |
| 48 | + $matches | Should -BeNullOrEmpty -Because (& $script:formatMatches -MatchList $matches) |
| 49 | + } |
| 50 | + |
| 51 | + It 'direct environment-variable access stays centralized in the shared helper' { |
| 52 | + $matches = & $script:findMatches -RootPath $script:srcRoot -Pattern '\$env:|GetEnvironmentVariable\(' |
| 53 | + $actual = & $script:getMatchedPaths -MatchList $matches |
| 54 | + $expected = @('src/private/shared/GetNovaEnvironmentVariableValue.ps1') |
| 55 | + |
| 56 | + (Compare-Object -ReferenceObject $expected -DifferenceObject $actual) | Should -BeNullOrEmpty -Because (($actual -join ', '), (& $script:formatMatches -MatchList $matches) -join [Environment]::NewLine) |
| 57 | + } |
| 58 | + |
| 59 | + It 'direct git execution stays centralized in the shared git adapter' { |
| 60 | + $matches = & $script:findMatches -RootPath $script:srcRoot -Pattern '&\s*git\b' |
| 61 | + $actual = & $script:getMatchedPaths -MatchList $matches |
| 62 | + $expected = @('src/private/shared/InvokeNovaGitCommand.ps1') |
| 63 | + |
| 64 | + (Compare-Object -ReferenceObject $expected -DifferenceObject $actual) | Should -BeNullOrEmpty -Because (($actual -join ', '), (& $script:formatMatches -MatchList $matches) -join [Environment]::NewLine) |
| 65 | + } |
| 66 | + |
| 67 | + It 'raw upload requests stay behind the package request adapter' { |
| 68 | + $matches = & $script:findMatches -RootPath $script:srcRoot -Pattern '\bInvoke-WebRequest\b' |
| 69 | + $actual = & $script:getMatchedPaths -MatchList $matches |
| 70 | + $expected = @('src/private/package/InvokeNovaPackageUploadRequest.ps1') |
| 71 | + |
| 72 | + (Compare-Object -ReferenceObject $expected -DifferenceObject $actual) | Should -BeNullOrEmpty -Because (($actual -join ', '), (& $script:formatMatches -MatchList $matches) -join [Environment]::NewLine) |
| 73 | + } |
| 74 | +} |
0 commit comments