Skip to content

Commit 0297f39

Browse files
committed
(#2591) Fix broken Pester tests
We found an issue when trying to pass multiple arguments through to Invoke-Choco. After a few different attempts to get things working, we settled on explicitly creating an array of arguments, which eant a minor change in Invoke-Choco itself to handle this correctly. The logic for skipping the `choco license` test was simplified to only include that command _when_ licensed is in play, rather than trying to skip that test when license is in play. Also added the required setup to allow execution of the required commands, i.e. install an older version of a package to allow `choco outdated` to work, and setting an apikey so the headers appear when doing `choco apikey`. Took the opportunity to fix a spelling mistake that was in nother file as well: encryped -> encrypted
1 parent bdd356f commit 0297f39

File tree

3 files changed

+41
-54
lines changed

3 files changed

+41
-54
lines changed

tests/helpers/common/Chocolatey/Invoke-Choco.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
)
1919
begin {
2020
$chocoPath = Get-ChocoPath
21-
$firstArgument, [string[]]$remainingArguments = $Arguments
21+
$firstArgument, [string[]]$remainingArguments = $Arguments.ForEach{$_}
2222
$arguments = @(
2323
$firstArgument
2424
'--allow-unofficial'

tests/pester-tests/commands/choco-apikey.Tests.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ Describe "choco <_>" -ForEach $Command -Tag Chocolatey, ApiKeyCommand {
119119
$key = $Key
120120
$config = $apiKeys.Where{ $_.source -eq "https://test.com/api/$key" }
121121
$config | Should -HaveCount 1
122-
$config.key | Should -Not -BeNullOrEmpty # The key is encryped, so we don't test the value
122+
$config.key | Should -Not -BeNullOrEmpty # The key is encrypted, so we don't test the value
123123
}
124124
}
125125

@@ -163,7 +163,7 @@ Describe "choco <_>" -ForEach $Command -Tag Chocolatey, ApiKeyCommand {
163163
$key = $Key
164164
$config = $apiKeys.Where{ $_.source -eq "https://test.com/api/add/$key" }
165165
$config | Should -HaveCount 1
166-
$config.key | Should -Not -BeNullOrEmpty # The key is encryped, so we don't test the value
166+
$config.key | Should -Not -BeNullOrEmpty # The key is encrypted, so we don't test the value
167167
}
168168
}
169169

@@ -211,7 +211,7 @@ Describe "choco <_>" -ForEach $Command -Tag Chocolatey, ApiKeyCommand {
211211
$key = $Key
212212
$config = $apiKeys.Where{ $_.source -eq "https://test.com/api/$key" }
213213
$config | Should -HaveCount 1
214-
$config.key | Should -Not -BeNullOrEmpty # The key is encryped, so we don't test the value
214+
$config.key | Should -Not -BeNullOrEmpty # The key is encrypted, so we don't test the value
215215
}
216216
}
217217

@@ -259,7 +259,7 @@ Describe "choco <_>" -ForEach $Command -Tag Chocolatey, ApiKeyCommand {
259259
$key = $Key
260260
$config = $apiKeys.Where{ $_.source -eq "https://test.com/api/add/$key" }
261261
$config | Should -HaveCount 1
262-
$config.key | Should -Not -BeNullOrEmpty # The key is encryped, so we don't test the value
262+
$config.key | Should -Not -BeNullOrEmpty # The key is encrypted, so we don't test the value
263263
}
264264
}
265265

tests/pester-tests/features/Headers.Tests.ps1

Lines changed: 36 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,100 +6,85 @@ Describe "choco headers tests" -Tag Chocolatey, HeadersFeature {
66

77
$Commands = @(
88
@{
9-
Command = 'apikey'
10-
CommandLine = 'list'
9+
CommandArguments = 'apikey', 'list'
1110
ExpectedHeaders = 'Source|ApiKey'
12-
SkipTest = $false
1311
}
1412
@{
15-
Command = 'config'
16-
CommandLine = 'list'
13+
CommandArguments = 'config', 'list'
1714
ExpectedHeaders = 'Name|Value|Description'
18-
SkipTest = $false
1915
}
2016
@{
21-
Command = 'feature'
22-
CommandLine = 'list'
17+
CommandArguments = 'feature', 'list'
2318
ExpectedHeaders = 'Name|Enabled|Description'
24-
SkipTest = $false
2519
}
2620
@{
27-
Command = 'info'
28-
CommandLine = "chocolatey", "--local-only"
21+
CommandArguments = 'info', 'chocolatey', '--local-only', '--pre'
2922
ExpectedHeaders = 'Id|Version'
30-
SkipTest = $false
3123
}
3224
@{
33-
Command = 'license'
34-
CommandLine = 'info'
35-
ExpectedHeaders = 'Name|Type|ExpirationDate|NodeCount'
36-
SkipTest = $isLicensed
37-
}
38-
@{
39-
Command = 'list'
25+
CommandArguments = 'list'
4026
ExpectedHeaders = 'Id|Version'
41-
SkipTest = $false
4227
}
4328
@{
44-
Command = 'outdated'
29+
CommandArguments = 'outdated', '--ignore-unfound', '--ignore-pinned'
4530
ExpectedHeaders = 'Id|Version|AvailableVersion|Pinned'
46-
SkipTest = $false
4731
}
4832
@{
49-
# Needs to pin something...
50-
Command = 'pin'
51-
CommandLine = 'list'
33+
CommandArguments = 'pin', 'list'
5234
ExpectedHeaders = 'Id|Version'
53-
SkipTest = $false
5435
}
5536
@{
56-
Command = 'rule'
57-
CommandLine = 'list'
37+
CommandArguments = 'rule', 'list'
5838
ExpectedHeaders = 'Severity|Id|Summary|HelpUrl'
59-
SkipTest = $false
6039
}
6140
@{
62-
Command = 'search'
63-
CommandLine = 'windirstat'
41+
CommandArguments = 'search', 'windirstat'
6442
ExpectedHeaders = 'Id|Version'
65-
SkipTest = $false
6643
}
6744
@{
68-
Command = 'source'
69-
CommandLine = 'list'
45+
CommandArguments = 'source', 'list'
7046
ExpectedHeaders = 'Name|Source|Disabled|UserName|Certificate|Priority|BypassProxy|AllowSelfService|AdminOnly'
71-
SkipTest = $false
7247
}
7348
@{
74-
Command = 'template'
75-
CommandLine = 'list'
49+
CommandArguments = 'template', 'list'
7650
ExpectedHeaders = 'Name|Version'
77-
SkipTest = $false
7851
}
7952
)
53+
54+
if ($isLicensed) {
55+
$Commands += @{
56+
CommandArguments = 'license', 'info'
57+
ExpectedHeaders = 'Name|Type|ExpirationDate|NodeCount'
58+
}
59+
}
8060
}
8161
BeforeAll {
8262
Initialize-ChocolateyTestInstall
8363

84-
New-ChocolateyInstallSnapshot
85-
8664
# These are needed in order to test the choco pin list execution
8765
# as well as the choco apikey list as the headers will only be shown
88-
# when there is a pinned package
66+
# when there is a pinned package. Finally, we need to install an
67+
# outdated package, so that the headers show in the choco outdated
68+
# command.
8969
$null = Invoke-Choco pin add --name="chocolatey"
9070

91-
$null = Invoke-Choco apikey add --source "https://community.chocolatey.org/api/v2" --api-key "bob"
71+
$null = Invoke-Choco apikey add --source "https://test.com/api/add/" --api-key "test-api-key"
72+
73+
$null = Invoke-Choco install upgradepackage --version 1.0.0 --confirm
74+
75+
New-ChocolateyInstallSnapshot
9276
}
9377

9478
AfterAll {
9579
Remove-ChocolateyTestInstall
9680
}
9781

98-
Context "Outputs headers for <Command> when '--include-headers' provided configured" -ForEach $Commands -Skip:($SkipTest) {
82+
Context "Outputs headers for <CommandArguments> when '--include-headers' provided configured" -ForEach $Commands {
9983
BeforeAll {
10084
Restore-ChocolateyInstallSnapshot -NoSnapshotCopy
10185

102-
$Output = Invoke-Choco $Command @CommandLine --limit-output --include-headers
86+
[string[]]$chocoArgs = @($CommandArguments) + @('--limit-output', '--include-headers')
87+
$Output = Invoke-Choco @chocoArgs
10388
}
10489

10590
It 'Exits success (0)' {
@@ -117,11 +102,12 @@ Describe "choco headers tests" -Tag Chocolatey, HeadersFeature {
117102
}
118103
}
119104

120-
Context "Does not output headers for <Command> by default" -ForEach $Commands -Skip:($SkipTest) {
105+
Context "Does not output headers for <CommandArguments> by default" -ForEach $Commands {
121106
BeforeAll {
122107
Restore-ChocolateyInstallSnapshot -NoSnapshotCopy
123108

124-
$Output = Invoke-Choco $Command @CommandLine --limit-output
109+
[string[]]$chocoArgs = @($CommandArguments) + @('--limit-output')
110+
$Output = Invoke-Choco @chocoArgs
125111
}
126112

127113
It 'Exits success (0)' {
@@ -139,16 +125,17 @@ Describe "choco headers tests" -Tag Chocolatey, HeadersFeature {
139125
}
140126
}
141127

142-
Context "Includes headers when feature enabled" -Skip:($SkipTest) {
128+
Context "Includes headers when feature enabled" {
143129
BeforeAll {
144130
Restore-ChocolateyInstallSnapshot
145131

146132
Enable-ChocolateyFeature -Name AlwaysIncludeHeaders
147133
}
148134

149-
Context "Includes headers for <Command>" -ForEach $Commands {
135+
Context "Includes headers for <CommandArguments>" -ForEach $Commands {
150136
BeforeAll {
151-
$Output = Invoke-Choco $Command @CommandLine --limit-output
137+
[string[]]$chocoArgs = @($CommandArguments) + @('--limit-output')
138+
$Output = Invoke-Choco @chocoArgs
152139
}
153140

154141
It 'Exits success (0)' {

0 commit comments

Comments
 (0)