From 93a8e7cd371f824d0e1cbe1784a2bf99901a962c 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/ChocolateyListCommand.cs | 28 ++++++++++++++----- .../commands/choco-list.Tests.ps1 | 14 +--------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/chocolatey/infrastructure.app/commands/ChocolateyListCommand.cs b/src/chocolatey/infrastructure.app/commands/ChocolateyListCommand.cs index 6a6d079d08..fe49b994ba 100644 --- a/src/chocolatey/infrastructure.app/commands/ChocolateyListCommand.cs +++ b/src/chocolatey/infrastructure.app/commands/ChocolateyListCommand.cs @@ -153,14 +153,28 @@ public virtual void ParseAdditionalArguments(IList 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 { diff --git a/tests/chocolatey-tests/commands/choco-list.Tests.ps1 b/tests/chocolatey-tests/commands/choco-list.Tests.ps1 index b4a273a9e1..59e1205a10 100644 --- a/tests/chocolatey-tests/commands/choco-list.Tests.ps1 +++ b/tests/chocolatey-tests/commands/choco-list.Tests.ps1 @@ -101,18 +101,7 @@ 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)" { @@ -120,8 +109,7 @@ Describe "choco list" -Tag Chocolatey, ListCommand { } 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 } } }