Skip to content

Commit

Permalink
Merge pull request #3167 from vexx32/list-lo-warnings
Browse files Browse the repository at this point in the history
(#158) Relegate the -lo/-li warning to log-file-only
  • Loading branch information
corbob authored May 24, 2023
2 parents 5204265 + 74aadea commit 0ad2978
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 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(@"
UNSUPPORTED ARGUMENT: Ignoring the argument {0}. This argument is unsupported for locally installed packages, and will be treated as a package name in Chocolatey CLI v3!".FormatWith(argument));
Ignoring the argument {0}. This argument is unsupported for locally installed packages.".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(@"
UNSUPPORTED ARGUMENT: Ignoring the argument {0}. This argument is unsupported for locally installed packages, and will be treated as a package name in Chocolatey CLI v3!".FormatWith(argument));
Ignoring the argument {0}. This argument is unsupported for locally installed packages.".FormatWith(argument));
Configuration.ListCommand.IncludeRegistryPrograms.ShouldBeTrue();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,18 @@ 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.Important, @"
UNSUPPORTED ARGUMENT: Ignoring the argument {0}. This argument is unsupported for locally installed packages, and will be treated as a package name in Chocolatey CLI v3!", argument);
this.Log().Warn(ChocolateyLoggers.LogFileOnly, unsupportedArgumentMessage, argument);
}
else if (_unsupportedIncludeRegistryProgramsArguments.Contains(argument, StringComparer.OrdinalIgnoreCase))
{
this.Log().Warn(ChocolateyLoggers.Important, @"
UNSUPPORTED ARGUMENT: Ignoring the argument {0}. This argument is unsupported for locally installed packages, and will be treated as a package name in Chocolatey CLI v3!", argument);
this.Log().Warn(ChocolateyLoggers.LogFileOnly, unsupportedArgumentMessage, argument);
configuration.ListCommand.IncludeRegistryPrograms = true;
}
else
Expand Down
18 changes: 15 additions & 3 deletions tests/chocolatey-tests/commands/choco-list.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,29 @@ Describe "choco list" -Tag Chocolatey, ListCommand {
}
}

Context "Listing local packages with unsupported argument outputs warning" -ForEach @('-l', '-lo', '--lo', '--local', '--localonly', '--local-only', '--order-by-popularity', '-a', '--all', '--allversions', '--all-versions', '-li', '-il', '-lai', '-lia', '-ali', '-ail', '-ial', '-ila') {
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" {
$Output.Lines | Should -Contain "UNSUPPORTED ARGUMENT: Ignoring the argument $_. This argument is unsupported for locally installed packages, and will be treated as a package name in Chocolatey CLI v3!"
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
}
}
}

0 comments on commit 0ad2978

Please sign in to comment.