|
| 1 | +$script:coverageGapsCliTestSupportPath = (Resolve-Path -LiteralPath (Join-Path $PSScriptRoot 'CoverageGaps.Cli.TestSupport.ps1')).Path |
| 2 | +. $script:coverageGapsCliTestSupportPath |
| 3 | +$global:cliSharedParserTestSupportFunctionNameList = @( |
| 4 | + 'Assert-TestStructuredCliError' |
| 5 | +) |
| 6 | + |
| 7 | +foreach ($functionName in $global:cliSharedParserTestSupportFunctionNameList) { |
| 8 | + $scriptBlock = (Get-Command -Name $functionName -CommandType Function -ErrorAction Stop).ScriptBlock |
| 9 | + Set-Item -Path "function:global:$functionName" -Value $scriptBlock |
| 10 | +} |
| 11 | + |
| 12 | +BeforeAll { |
| 13 | + $here = Split-Path -Parent $PSCommandPath |
| 14 | + $script:repoRoot = Split-Path -Parent $here |
| 15 | + $script:moduleName = (Get-Content -LiteralPath (Join-Path $script:repoRoot 'project.json') -Raw | ConvertFrom-Json).ProjectName |
| 16 | + $script:distModuleDir = Join-Path $script:repoRoot "dist/$script:moduleName" |
| 17 | + $coverageGapsCliTestSupportPath = (Resolve-Path -LiteralPath (Join-Path $PSScriptRoot 'CoverageGaps.Cli.TestSupport.ps1')).Path |
| 18 | + |
| 19 | + if (-not (Test-Path -LiteralPath $script:distModuleDir)) { |
| 20 | + throw "Expected built $script:moduleName module at: $script:distModuleDir. Run Invoke-NovaBuild in the repo root first." |
| 21 | + } |
| 22 | + |
| 23 | + . $coverageGapsCliTestSupportPath |
| 24 | + foreach ($functionName in $global:cliSharedParserTestSupportFunctionNameList) { |
| 25 | + $scriptBlock = (Get-Command -Name $functionName -CommandType Function -ErrorAction Stop).ScriptBlock |
| 26 | + Set-Item -Path "function:global:$functionName" -Value $scriptBlock |
| 27 | + } |
| 28 | + Remove-Module $script:moduleName -ErrorAction SilentlyContinue |
| 29 | + Import-Module $script:distModuleDir -Force |
| 30 | +} |
| 31 | + |
| 32 | +Describe 'CLI shared parser helpers' { |
| 33 | + It 'ConvertFrom-NovaCliSwitchArgument maps declarative switch schemas into boolean option sets' { |
| 34 | + InModuleScope $script:moduleName { |
| 35 | + $options = ConvertFrom-NovaCliSwitchArgument -Arguments @('--preview', '-i') -TokenMap @{ |
| 36 | + '--preview' = 'Preview' |
| 37 | + '-p' = 'Preview' |
| 38 | + '--continuous-integration' = 'ContinuousIntegration' |
| 39 | + '-i' = 'ContinuousIntegration' |
| 40 | + } |
| 41 | + |
| 42 | + $options.Preview | Should -BeTrue |
| 43 | + $options.ContinuousIntegration | Should -BeTrue |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + It 'Get-NovaCliModeArgumentValue supports empty results, mapped values, usage errors, and unknown arguments' { |
| 48 | + InModuleScope $script:moduleName { |
| 49 | + $notificationDefinition = [pscustomobject]@{ |
| 50 | + EmptyResult = 'status' |
| 51 | + TokenMap = @{ |
| 52 | + '--enable' = 'enable' |
| 53 | + } |
| 54 | + Usage = [pscustomobject]@{ |
| 55 | + Message = "Unsupported 'nova notification' usage." |
| 56 | + ErrorId = 'Nova.Validation.UnsupportedNotificationCliUsage' |
| 57 | + } |
| 58 | + UnknownArgumentUsesUsageError = $false |
| 59 | + } |
| 60 | + $updateDefinition = [pscustomobject]@{ |
| 61 | + EmptyResult = @{} |
| 62 | + TokenMap = @{} |
| 63 | + Usage = [pscustomobject]@{ |
| 64 | + Message = "Unsupported 'nova update' usage. Use 'nova update'." |
| 65 | + ErrorId = 'Nova.Validation.UnsupportedUpdateCliUsage' |
| 66 | + } |
| 67 | + UnknownArgumentUsesUsageError = $true |
| 68 | + } |
| 69 | + |
| 70 | + $emptyResult = Get-NovaCliModeArgumentValue -Arguments @() -Definition $notificationDefinition |
| 71 | + $mappedResult = Get-NovaCliModeArgumentValue -Arguments @('--enable') -Definition $notificationDefinition |
| 72 | + |
| 73 | + $usageError = $null |
| 74 | + try { |
| 75 | + Get-NovaCliModeArgumentValue -Arguments @('--enable', '--disable') -Definition $notificationDefinition |
| 76 | + } |
| 77 | + catch { |
| 78 | + $usageError = $_ |
| 79 | + } |
| 80 | + |
| 81 | + $unknownArgumentError = $null |
| 82 | + try { |
| 83 | + Get-NovaCliModeArgumentValue -Arguments @('--bogus') -Definition $notificationDefinition |
| 84 | + } |
| 85 | + catch { |
| 86 | + $unknownArgumentError = $_ |
| 87 | + } |
| 88 | + |
| 89 | + $updateUsageError = $null |
| 90 | + try { |
| 91 | + Get-NovaCliModeArgumentValue -Arguments @('--bogus') -Definition $updateDefinition |
| 92 | + } |
| 93 | + catch { |
| 94 | + $updateUsageError = $_ |
| 95 | + } |
| 96 | + |
| 97 | + $emptyResult | Should -Be 'status' |
| 98 | + $mappedResult | Should -Be 'enable' |
| 99 | + Assert-TestStructuredCliError -ThrownError $usageError -ExpectedError ([pscustomobject]@{ |
| 100 | + Message = "Unsupported 'nova notification' usage." |
| 101 | + ErrorId = 'Nova.Validation.UnsupportedNotificationCliUsage' |
| 102 | + Category = [System.Management.Automation.ErrorCategory]::InvalidArgument |
| 103 | + }) |
| 104 | + Assert-TestStructuredCliError -ThrownError $unknownArgumentError -ExpectedError ([pscustomobject]@{ |
| 105 | + Message = 'Unknown argument: --bogus' |
| 106 | + ErrorId = 'Nova.Validation.UnknownCliArgument' |
| 107 | + Category = [System.Management.Automation.ErrorCategory]::InvalidArgument |
| 108 | + TargetObject = '--bogus' |
| 109 | + }) |
| 110 | + Assert-TestStructuredCliError -ThrownError $updateUsageError -ExpectedError ([pscustomobject]@{ |
| 111 | + Message = "Unsupported 'nova update' usage. Use 'nova update'." |
| 112 | + ErrorId = 'Nova.Validation.UnsupportedUpdateCliUsage' |
| 113 | + Category = [System.Management.Automation.ErrorCategory]::InvalidArgument |
| 114 | + }) |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + It 'the shared parsers preserve the existing routed CLI contracts for bump, version, update, and notification' { |
| 119 | + InModuleScope $script:moduleName { |
| 120 | + $bumpOptions = ConvertFrom-NovaBumpCliArgument -Arguments @('--preview', '--continuous-integration') |
| 121 | + |
| 122 | + $bumpOptions.Preview | Should -BeTrue |
| 123 | + $bumpOptions.ContinuousIntegration | Should -BeTrue |
| 124 | + (ConvertFrom-NovaVersionCliArgument).Installed | Should -BeFalse |
| 125 | + (ConvertFrom-NovaVersionCliArgument -Arguments @('--installed')).Installed | Should -BeTrue |
| 126 | + ConvertFrom-NovaNotificationCliArgument | Should -Be 'status' |
| 127 | + ConvertFrom-NovaNotificationCliArgument -Arguments @('--enable') | Should -Be 'enable' |
| 128 | + ConvertFrom-NovaNotificationCliArgument -Arguments @('-d') | Should -Be 'disable' |
| 129 | + (ConvertFrom-NovaUpdateCliArgument).Count | Should -Be 0 |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments