forked from chocolatey/choco
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(chocolatey#2591) Add headers to limit output commands
When a user asks for limited output from Chocolatey, it is not uncommon to pipe that output to `ConvertFrom-String` or `ConvertFrom-Csv` and manually add headers to get back an object. This allows for getting a header row back so that the end user doesn't need to add their own headers and discern what they are. This also adds a StringResources static class that allows us to store constant strings in and use them across the code to reduce duplication.
- Loading branch information
Showing
17 changed files
with
227 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
Import-Module helpers/common-helpers | ||
|
||
Describe "choco headers tests" -Tag Chocolatey, HeadersFeature { | ||
BeforeDiscovery { | ||
$Commands = @( | ||
@{ | ||
Command = 'list' | ||
CommandLine = '--local-only' | ||
ExpectedHeaders = 'PackageID|Version' | ||
} | ||
@{ | ||
# | ||
Command = 'info' | ||
CommandLine = 'chocolatey --local-only' | ||
ExpectedHeaders = 'PackageID|Version' | ||
} | ||
@{ | ||
# Needs to pin something... | ||
Command = 'pin' | ||
ExpectedHeaders = 'PackageID|Version' | ||
} | ||
@{ | ||
Command = 'outdated' | ||
ExpectedHeaders = 'PackageName|CurrentVersion|AvailableVersion|Pinned' | ||
} | ||
@{ | ||
Command = 'source' | ||
ExpectedHeaders = 'SourceId|Location|Disabled|UserName|Certificate|Priority|BypassProxy|AllowSelfService|AdminOnly' | ||
} | ||
@{ | ||
Command = 'config' | ||
ExpectedHeaders = 'Name|Value|Description' | ||
} | ||
@{ | ||
Command = 'feature' | ||
ExpectedHeaders = 'FeatureName|Enabled|Description' | ||
} | ||
@{ | ||
Command = 'apikey' | ||
ExpectedHeaders = 'Source|Key' | ||
} | ||
@{ | ||
Command = 'template' | ||
ExpectedHeaders = 'TemplateName|Version' | ||
} | ||
) | ||
} | ||
BeforeAll { | ||
Initialize-ChocolateyTestInstall | ||
|
||
New-ChocolateyInstallSnapshot | ||
} | ||
|
||
AfterAll { | ||
Remove-ChocolateyTestInstall | ||
} | ||
|
||
Context "Outputs headers for <Command> when '--display-headers' provided configured" -ForEach $Commands { | ||
BeforeAll { | ||
$Output = Invoke-Choco $Command $CommandLine --limit-output --display-headers | ||
} | ||
|
||
It 'Exits success (0)' { | ||
$Output.ExitCode | Should -Be 0 -Because $Output.String | ||
} | ||
|
||
It "Displays appropriate header" { | ||
# Some commands won't output anything but the header. In that case we want just the lines instead of indexing into it. | ||
$ActualOutput = if ($Output.Lines.Count -gt 1) { | ||
$Output.Lines[0] | ||
} | ||
else { | ||
$Output.Lines | ||
} | ||
|
||
$ActualOutput | Should -Be $ExpectedHeaders | ||
} | ||
|
||
} | ||
|
||
Context "Does not output headers for <Command> by default" -ForEach $Commands { | ||
BeforeAll { | ||
$Output = Invoke-Choco $Command $CommandLine --limit-output | ||
} | ||
|
||
It 'Exits success (0)' { | ||
$Output.ExitCode | Should -Be 0 -Because $Output.String | ||
} | ||
|
||
It "Does not display header" { | ||
# Some commands won't output anything but the header. In that case we want just the lines instead of indexing into it. | ||
$ActualOutput = if ($Output.Lines.Count -gt 1) { | ||
$Output.Lines[0] | ||
} | ||
else { | ||
$Output.Lines | ||
} | ||
|
||
$ActualOutput | Should -Not -Be $ExpectedHeaders | ||
} | ||
|
||
} | ||
|
||
Context "Outputs headers when feature enabled" { | ||
BeforeAll { | ||
Restore-ChocolateyInstallSnapshot | ||
|
||
Enable-ChocolateyFeature -Name AlwaysDisplayHeaders | ||
} | ||
|
||
Context "Outputs headers for <Command>" -ForEach $Commands { | ||
BeforeAll { | ||
$Output = Invoke-Choco $Command $CommandLine --limit-output | ||
} | ||
|
||
It 'Exits success (0)' { | ||
$Output.ExitCode | Should -Be 0 -Because $Output.String | ||
} | ||
|
||
It "Displays appropriate header" { | ||
# Some commands won't output anything but the header. In that case we want just the lines instead of indexing into it. | ||
$ActualOutput = if ($Output.Lines.Count -gt 1) { | ||
$Output.Lines[0] | ||
} | ||
else { | ||
$Output.Lines | ||
} | ||
|
||
$ActualOutput | Should -Be $ExpectedHeaders | ||
} | ||
} | ||
} | ||
} |