-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCliHelperCoverage.Tests.ps1
More file actions
149 lines (128 loc) · 6.79 KB
/
Copy pathCliHelperCoverage.Tests.ps1
File metadata and controls
149 lines (128 loc) · 6.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
$script:coverageGapsCliTestSupportPath = (Resolve-Path -LiteralPath (Join-Path $PSScriptRoot 'CoverageGaps.Cli.TestSupport.ps1')).Path
$global:cliHelperCoverageSupportFunctionNameList = @(
'Assert-TestStructuredCliError'
)
. $script:coverageGapsCliTestSupportPath
foreach ($functionName in $global:cliHelperCoverageSupportFunctionNameList) {
$scriptBlock = (Get-Command -Name $functionName -CommandType Function -ErrorAction Stop).ScriptBlock
Set-Item -Path "function:global:$functionName" -Value $scriptBlock
}
BeforeAll {
$here = Split-Path -Parent $PSCommandPath
$script:repoRoot = Split-Path -Parent $here
$script:moduleName = (Get-Content -LiteralPath (Join-Path $script:repoRoot 'project.json') -Raw | ConvertFrom-Json).ProjectName
$script:distModuleDir = Join-Path $script:repoRoot "dist/$script:moduleName"
$coverageGapsCliTestSupportPath = (Resolve-Path -LiteralPath (Join-Path $PSScriptRoot 'CoverageGaps.Cli.TestSupport.ps1')).Path
if (-not (Test-Path -LiteralPath $script:distModuleDir)) {
throw "Expected built $script:moduleName module at: $script:distModuleDir. Run Invoke-NovaBuild in the repo root first."
}
. $coverageGapsCliTestSupportPath
foreach ($functionName in $global:cliHelperCoverageSupportFunctionNameList) {
$scriptBlock = (Get-Command -Name $functionName -CommandType Function -ErrorAction Stop).ScriptBlock
Set-Item -Path "function:global:$functionName" -Value $scriptBlock
}
Remove-Module $script:moduleName -ErrorAction SilentlyContinue
Import-Module $script:distModuleDir -Force
}
Describe 'Targeted coverage for smaller CLI helper internals' {
It 'CLI help helpers expose the usage text, unsupported usage errors, empty examples, and direct option accumulation' {
InModuleScope $script:moduleName {
Get-NovaCliHelpUsageText | Should -Be "Use 'nova --help', 'nova -h', 'nova --help <command>', 'nova -h <command>', or 'nova <command> --help'/'nova <command> -h'."
Get-NovaCliExampleText -Examples @() | Should -Be ' (none)'
$options = @{}
Add-NovaCliOptionValue -Options $options -Name 'path' -Value '/tmp/one'
Add-NovaCliOptionValue -Options $options -Name 'path' -Value '/tmp/two'
$options.path | Should -Be @('/tmp/one', '/tmp/two')
$directHelpUsageError = $null
try {
Assert-NovaCliHelpUsageSupported -Tokens @('--help', 'build')
}
catch {
$directHelpUsageError = $_
}
Assert-TestStructuredCliError -ThrownError $directHelpUsageError -ExpectedError ([pscustomobject]@{
Message = "Unsupported help usage. Use 'nova --help', 'nova -h', 'nova --help <command>', 'nova -h <command>', or 'nova <command> --help'/'nova <command> -h'."
ErrorId = 'Nova.Validation.UnsupportedCliHelpUsage'
Category = [System.Management.Automation.ErrorCategory]::InvalidArgument
TargetObject = '--help build'
})
$rootHelpUsageError = $null
try {
Get-NovaCliHelpRequest -Command '--help' -Arguments @('--help')
}
catch {
$rootHelpUsageError = $_
}
Assert-TestStructuredCliError -ThrownError $rootHelpUsageError -ExpectedError ([pscustomobject]@{
Message = "Unsupported help usage. Use 'nova --help', 'nova -h', 'nova --help <command>', 'nova -h <command>', or 'nova <command> --help'/'nova <command> -h'."
ErrorId = 'Nova.Validation.UnsupportedCliHelpUsage'
Category = [System.Management.Automation.ErrorCategory]::InvalidArgument
TargetObject = '--help --help'
})
$subcommandHelpUsageError = $null
try {
Get-NovaCliHelpRequest -Command 'build' -Arguments @('--help', 'extra')
}
catch {
$subcommandHelpUsageError = $_
}
Assert-TestStructuredCliError -ThrownError $subcommandHelpUsageError -ExpectedError ([pscustomobject]@{
Message = "Unsupported help usage. Use 'nova --help', 'nova -h', 'nova --help <command>', 'nova -h <command>', or 'nova <command> --help'/'nova <command> -h'."
ErrorId = 'Nova.Validation.UnsupportedCliHelpUsage'
Category = [System.Management.Automation.ErrorCategory]::InvalidArgument
TargetObject = 'build --help extra'
})
}
}
It 'Get-NovaCliForwardingParameterSet includes verbose and should-process options directly when requested' {
InModuleScope $script:moduleName {
$previousWhatIfPreference = $WhatIfPreference
try {
$WhatIfPreference = $true
$result = Get-NovaCliForwardingParameterSet -BoundParameters @{Verbose = $true; Confirm = $false} -IncludeShouldProcess
$result.Verbose | Should -BeTrue
$result.WhatIf | Should -BeTrue
$result.Confirm | Should -BeFalse
}
finally {
$WhatIfPreference = $previousWhatIfPreference
}
}
}
It 'Get-NovaCliInstalledVersion returns the stable version when prerelease metadata is blank or missing' {
InModuleScope $script:moduleName {
$blankPrereleaseModule = [pscustomobject]@{
Version = [version]'2.0.0'
PrivateData = [pscustomobject]@{
PSData = [pscustomobject]@{
Prerelease = ' '
}
}
}
$missingPrereleaseModule = [pscustomobject]@{
Version = [version]'2.0.1'
PrivateData = [pscustomobject]@{
PSData = [pscustomobject]@{}
}
}
Get-NovaCliInstalledVersion -Module $blankPrereleaseModule | Should -Be '2.0.0'
Get-NovaCliInstalledVersion -Module $missingPrereleaseModule | Should -Be '2.0.1'
}
}
It 'native console read helpers remain injectable for coverage-safe testing' {
InModuleScope $script:moduleName {
$result = Invoke-NovaCliNativeConsoleReadKey -Reader {
[pscustomobject]@{KeyChar = [char]'y'}
}
Mock Invoke-NovaCliNativeConsoleReadKey {
[pscustomobject]@{KeyChar = [char]'y'}
}
$reader = Get-NovaCliConsoleReadKeyReader
$result.KeyChar | Should -Be ([char]'y')
$reader | Should -BeOfType 'scriptblock'
($reader.ToString()).Trim() | Should -Be 'Invoke-NovaCliNativeConsoleReadKey'
(& $reader).KeyChar | Should -Be ([char]'y')
Assert-MockCalled Invoke-NovaCliNativeConsoleReadKey -Times 1
}
}
}