|
| 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 | + |
| 75 | + It 'self-update execution stays behind the update command adapter' { |
| 76 | + $matches = & $script:findMatches -RootPath $script:srcRoot -Pattern '^\s*(?:return\s+)?Update-Module\b' |
| 77 | + $actual = & $script:getMatchedPaths -MatchList $matches |
| 78 | + $expected = @('src/private/update/InvokeNovaModuleUpdateCommand.ps1') |
| 79 | + |
| 80 | + (Compare-Object -ReferenceObject $expected -DifferenceObject $actual) | Should -BeNullOrEmpty -Because (($actual -join ', '), (& $script:formatMatches -MatchList $matches) -join [Environment]::NewLine) |
| 81 | + } |
| 82 | + |
| 83 | + It 'public command files use only their approved Nova helper surface' { |
| 84 | + $testCases = @( |
| 85 | + [pscustomobject]@{Path = 'src/public/DeployNovaPackage.ps1'; ExpectedHelpers = @('Get-NovaPackageUploadWorkflowContext', 'Get-NovaProjectInfo', 'Invoke-NovaPackageUploadWorkflow', 'New-NovaPackageUploadDynamicParameterDictionary', 'New-NovaPackageUploadOption')} |
| 86 | + [pscustomobject]@{Path = 'src/public/GetNovaProjectInfo.ps1'; ExpectedHelpers = @('Get-NovaProjectInfoContext', 'Get-NovaProjectInfoResult')} |
| 87 | + [pscustomobject]@{Path = 'src/public/GetNovaUpdateNotificationPreference.ps1'; ExpectedHelpers = @('Get-NovaUpdateNotificationPreferenceStatus')} |
| 88 | + [pscustomobject]@{Path = 'src/public/InitializeNovaModule.ps1'; ExpectedHelpers = @('Get-NovaModuleInitializationWorkflowContext', 'Invoke-NovaModuleInitializationWorkflow')} |
| 89 | + [pscustomobject]@{Path = 'src/public/InstallNovaCli.ps1'; ExpectedHelpers = @('Get-NovaCliInstallWorkflowContext', 'Invoke-NovaCliInstallWorkflow', 'Write-NovaModuleReleaseNotesLink')} |
| 90 | + [pscustomobject]@{Path = 'src/public/InvokeNovaBuild.ps1'; ExpectedHelpers = @('Get-NovaBuildWorkflowContext', 'Invoke-NovaBuildWorkflow')} |
| 91 | + [pscustomobject]@{Path = 'src/public/InvokeNovaCli.ps1'; ExpectedHelpers = @('Get-NovaCliInvocationContext', 'Invoke-NovaCliCommandRoute')} |
| 92 | + [pscustomobject]@{Path = 'src/public/InvokeNovaRelease.ps1'; ExpectedHelpers = @('Get-NovaProjectInfo', 'Get-NovaPublishWorkflowContext', 'Get-NovaShouldProcessForwardingParameter', 'Invoke-NovaReleaseWorkflow', 'Write-NovaPublishWorkflowContext')} |
| 93 | + [pscustomobject]@{Path = 'src/public/NewNovaModulePackage.ps1'; ExpectedHelpers = @('Get-NovaPackageWorkflowContext', 'Get-NovaShouldProcessForwardingParameter', 'Invoke-NovaPackageWorkflow')} |
| 94 | + [pscustomobject]@{Path = 'src/public/PublishNovaModule.ps1'; ExpectedHelpers = @('Get-NovaDynamicDeliveryParameterDictionary', 'Get-NovaProjectInfo', 'Get-NovaPublishWorkflowContext', 'Get-NovaShouldProcessForwardingParameter', 'Invoke-NovaPublishWorkflow', 'Write-NovaPublishWorkflowContext')} |
| 95 | + [pscustomobject]@{Path = 'src/public/SetNovaUpdateNotificationPreference.ps1'; ExpectedHelpers = @('Get-NovaUpdateNotificationPreferenceChangeContext', 'Invoke-NovaUpdateNotificationPreferenceChange')} |
| 96 | + [pscustomobject]@{Path = 'src/public/TestNovaBuild.ps1'; ExpectedHelpers = @('Get-NovaTestWorkflowContext', 'Invoke-NovaTestWorkflow', 'New-NovaTestDynamicParameterDictionary')} |
| 97 | + [pscustomobject]@{Path = 'src/public/UpdateNovaModuleTools.ps1'; ExpectedHelpers = @('Complete-NovaModuleSelfUpdateResult', 'Confirm-NovaPrereleaseModuleUpdate', 'Get-NovaModuleSelfUpdateWorkflowContext', 'Invoke-NovaModuleSelfUpdateWorkflow', 'Write-NovaModuleReleaseNotesLink')} |
| 98 | + [pscustomobject]@{Path = 'src/public/UpdateNovaModuleVersion.ps1'; ExpectedHelpers = @('Get-NovaVersionUpdateCiActivatedCommand', 'Get-NovaVersionUpdateWorkflowContext', 'Invoke-NovaVersionUpdateWorkflow')} |
| 99 | + ) |
| 100 | + $expectedPaths = @($testCases | ForEach-Object Path | Sort-Object) |
| 101 | + $actualPaths = @( |
| 102 | + Get-ChildItem -LiteralPath (Join-Path $script:srcRoot 'public') -Filter '*.ps1' -File | |
| 103 | + ForEach-Object {([System.IO.Path]::GetRelativePath($script:repoRoot, $_.FullName)).Replace('\', '/')} | |
| 104 | + Sort-Object |
| 105 | + ) |
| 106 | + |
| 107 | + (Compare-Object -ReferenceObject $expectedPaths -DifferenceObject $actualPaths) | Should -BeNullOrEmpty -Because "Public command allowlist should stay in sync with src/public. Expected: $( $expectedPaths -join ', ' ) | Actual: $( $actualPaths -join ', ' )" |
| 108 | + |
| 109 | + foreach ($testCase in $testCases) { |
| 110 | + $filePath = Join-Path $script:repoRoot $testCase.Path |
| 111 | + $null = $tokens = $parseErrors = $null |
| 112 | + $ast = [System.Management.Automation.Language.Parser]::ParseFile($filePath, [ref]$tokens, [ref]$parseErrors) |
| 113 | + $actualHelpers = @( |
| 114 | + $ast.FindAll({param($node) $node -is [System.Management.Automation.Language.CommandAst]}, $true) | |
| 115 | + ForEach-Object {$_.GetCommandName()} | |
| 116 | + Where-Object {$_ -like '*Nova*'} | |
| 117 | + Sort-Object -Unique |
| 118 | + ) |
| 119 | + $expectedHelpers = @($testCase.ExpectedHelpers | Sort-Object -Unique) |
| 120 | + |
| 121 | + (Compare-Object -ReferenceObject $expectedHelpers -DifferenceObject $actualHelpers) | Should -BeNullOrEmpty -Because "$( $testCase.Path ) should only use its approved Nova helper surface. Expected: $( $expectedHelpers -join ', ' ) | Actual: $( $actualHelpers -join ', ' )" |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + It 'project.json persistence stays limited to the shared writer and its expected callers' { |
| 126 | + $matches = & $script:findMatches -RootPath $script:srcRoot -Pattern '\bWrite-ProjectJsonData\b' |
| 127 | + $actual = & $script:getMatchedPaths -MatchList $matches |
| 128 | + $expected = @( |
| 129 | + 'src/private/release/SetNovaModuleVersion.ps1' |
| 130 | + 'src/private/scaffold/WriteNovaModuleProjectJson.ps1' |
| 131 | + 'src/private/shared/Write-ProjectJsonData.ps1' |
| 132 | + ) |
| 133 | + |
| 134 | + (Compare-Object -ReferenceObject $expected -DifferenceObject $actual) | Should -BeNullOrEmpty -Because (($actual -join ', '), (& $script:formatMatches -MatchList $matches) -join [Environment]::NewLine) |
| 135 | + } |
| 136 | +} |
0 commit comments