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 cd91282
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public void Should_output_warning_message_about_unsupported_argument(string argu
_because();
MockLogger.Messages.Keys.ShouldContain("Warn");
MockLogger.Messages["Warn"].ShouldContain(@"
Ignoring the argument {0}. This argument is unsupported for locally installed packages.".FormatWith(argument));
Invalid argument {0}. This argument has been removed from the list command and cannot be used.".FormatWith(argument));
}

[NUnit.Framework.TestCase("-li")]
Expand All @@ -157,7 +157,7 @@ public void Should_output_warning_message_about_unsupported_argument_and_set_inc
_because();
MockLogger.Messages.Keys.ShouldContain("Warn");
MockLogger.Messages["Warn"].ShouldContain(@"
Ignoring the argument {0}. This argument is unsupported for locally installed packages.".FormatWith(argument));
Invalid argument {0}. This argument has been removed from the list command and cannot be used.".FormatWith(argument));
Configuration.ListCommand.IncludeRegistryPrograms.ShouldBeTrue();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,19 +148,33 @@ public virtual int Count(ChocolateyConfiguration config)
public virtual void ParseAdditionalArguments(IList<string> unparsedArguments, ChocolateyConfiguration configuration)
{
var argumentsWithoutLocalOnly = new List<string>(unparsedArguments.Count);
const string unsupportedArgumentMessage = @"
Ignoring the argument {0}. This argument is unsupported for locally installed packages.";

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, @"
Invalid argument {0}. This argument has been removed from the list command and cannot be used.", 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, @"
Ignoring the argument {0}. This argument is unsupported for locally installed packages.", argument);
}
}
else
{
Expand Down
18 changes: 3 additions & 15 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 "Exits with Failure (1)" {
$Output.ExitCode | Should -Be 1
}

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 "Invalid argument $_. This argument has been removed from the list command and cannot be used." -Because $Output.String
}
}
}

0 comments on commit cd91282

Please sign in to comment.