Skip to content

Commit

Permalink
(chocolatey#158) Restore -lo warning when not using -r
Browse files Browse the repository at this point in the history
And also add an exit code, as it will be likely completely breaking in
v3 and possibly causing problems.
  • Loading branch information
vexx32 committed May 26, 2023
1 parent 0ad2978 commit 93a8e7c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,28 @@ public virtual void ParseAdditionalArguments(IList<string> unparsedArguments, Ch

foreach (var argument in unparsedArguments)
{
if (_unsupportedArguments.Contains(argument, StringComparer.OrdinalIgnoreCase))
{
this.Log().Warn(ChocolateyLoggers.LogFileOnly, unsupportedArgumentMessage, argument);
}
else if (_unsupportedIncludeRegistryProgramsArguments.Contains(argument, StringComparer.OrdinalIgnoreCase))
bool isUnsupportedArgument = _unsupportedArguments.Contains(argument, StringComparer.OrdinalIgnoreCase);
bool isUnsupportedRegistryProgramsArgument = _unsupportedIncludeRegistryProgramsArguments.Contains(argument, StringComparer.OrdinalIgnoreCase);

if (isUnsupportedArgument || isUnsupportedRegistryProgramsArgument)
{
this.Log().Warn(ChocolateyLoggers.LogFileOnly, unsupportedArgumentMessage, argument);
configuration.ListCommand.IncludeRegistryPrograms = true;
if (isUnsupportedRegistryProgramsArgument)
{
configuration.ListCommand.IncludeRegistryPrograms = true;
}

if (configuration.RegularOutput)
{
this.Log().Warn(ChocolateyLoggers.Important, unsupportedArgumentMessage, argument);

// Give an error code to make the warning more notable; as this could potentially cause issues if these are used in v3
// we want folks to take note that they need to be careful about using these unsupported arguments.
Environment.ExitCode = 1;
}
else
{
this.Log().Warn(ChocolateyLoggers.LogFileOnly, unsupportedArgumentMessage, argument);
}
}
else
{
Expand Down
14 changes: 1 addition & 13 deletions tests/chocolatey-tests/commands/choco-list.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -101,27 +101,15 @@ Describe "choco list" -Tag Chocolatey, ListCommand {

Context "Listing local packages with unsupported argument outputs warning to log file only" -ForEach @('-l', '-lo', '--lo', '--local', '--localonly', '--local-only', '--order-by-popularity', '-a', '--all', '--allversions', '--all-versions', '-li', '-il', '-lai', '-lia', '-ali', '-ail', '-ial', '-ila') {
BeforeAll {
$LogPath = "$env:ChocolateyInstall\logs\chocolatey.log"
$OldLogContent = Get-Content $LogPath
Remove-Item -Path $LogPath

$Output = Invoke-Choco list $_
$LogFile = Get-Content -Path $LogPath

# Logs are picked up by CI for investigation if needed, so we'll keep both full sets of logs.
@(
$OldLogContent
$LogFile
) | Set-Content -Path $LogPath
}

It "Exits with Success (0)" {
$Output.ExitCode | Should -Be 0
}

It "Should contain expected warning message in the logs" {
@($LogFile) -match "Ignoring the argument $_. This argument is unsupported for locally installed packages." |
Should -Not -BeNullOrEmpty
$Output.Lines | Should -Contain "Ignoring the argument $_. This argument is unsupported for locally installed packages." -Because $Output.String
}
}
}

0 comments on commit 93a8e7c

Please sign in to comment.