From cd912828d1e9869dde0dc15748f399c480e26ccf Mon Sep 17 00:00:00 2001 From: Rain Sallow Date: Fri, 26 May 2023 12:16:58 -0400 Subject: [PATCH] (#158) Restore -lo warning when not using -r And also add an exit code, as it will be likely completely breaking in v3 and possibly causing problems. --- .../commands/ChocolateyListCommandSpecs.cs | 4 +-- .../commands/ChocolateyListCommand.cs | 32 +++++++++++++------ .../commands/choco-list.Tests.ps1 | 18 ++--------- 3 files changed, 28 insertions(+), 26 deletions(-) diff --git a/src/chocolatey.tests/infrastructure.app/commands/ChocolateyListCommandSpecs.cs b/src/chocolatey.tests/infrastructure.app/commands/ChocolateyListCommandSpecs.cs index 08128c1244..478ddff8c3 100644 --- a/src/chocolatey.tests/infrastructure.app/commands/ChocolateyListCommandSpecs.cs +++ b/src/chocolatey.tests/infrastructure.app/commands/ChocolateyListCommandSpecs.cs @@ -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")] @@ -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(); } } diff --git a/src/chocolatey/infrastructure.app/commands/ChocolateyListCommand.cs b/src/chocolatey/infrastructure.app/commands/ChocolateyListCommand.cs index 6a6d079d08..acd6d317b2 100644 --- a/src/chocolatey/infrastructure.app/commands/ChocolateyListCommand.cs +++ b/src/chocolatey/infrastructure.app/commands/ChocolateyListCommand.cs @@ -148,19 +148,33 @@ public virtual int Count(ChocolateyConfiguration config) public virtual void ParseAdditionalArguments(IList unparsedArguments, ChocolateyConfiguration configuration) { var argumentsWithoutLocalOnly = new List(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 { diff --git a/tests/chocolatey-tests/commands/choco-list.Tests.ps1 b/tests/chocolatey-tests/commands/choco-list.Tests.ps1 index b4a273a9e1..632ad329d4 100644 --- a/tests/chocolatey-tests/commands/choco-list.Tests.ps1 +++ b/tests/chocolatey-tests/commands/choco-list.Tests.ps1 @@ -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 } } }