Skip to content

Commit

Permalink
Merge pull request #3172 from vexx32/hard-error-on-list-lo
Browse files Browse the repository at this point in the history
(#158) Error on list -lo without -r
  • Loading branch information
gep13 committed May 30, 2023
2 parents a4f2319 + 202f7d6 commit ebef819
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public override void Context()
_unparsedArgs.Add("pkg1");
_unparsedArgs.Add("pkg2");
_unparsedArgs.Add("-l");
_unparsedArgs.Add("-lo");
_unparsedArgs.Add("--local-only");
_unparsedArgs.Add("--localonly");
_unparsedArgs.Add("-li");
Expand All @@ -132,32 +133,60 @@ public override void Because()
_because = () => Command.ParseAdditionalArguments(_unparsedArgs, Configuration);
}

[NUnit.Framework.TestCase("-l")]
[NUnit.Framework.TestCase("-lo")]
[NUnit.Framework.TestCase("--local-only")]
[NUnit.Framework.TestCase("--localonly")]
[NUnit.Framework.TestCase("-li")]
[NUnit.Framework.TestCase("-lai")]
public void Should_throw_on_unsupported_argument(string argument)
{
Configuration.RegularOutput = true;
var errored = false;
Exception error = null;
try
{
_because();
}
catch (Exception ex)
{
errored = true;
error = ex;
}

errored.ShouldBeTrue();
error.ShouldNotBeNull();
error.ShouldBeType<ApplicationException>();
}

[Fact]
public void Should_set_unparsed_arguments_to_configuration_input()
public void Should_set_unparsed_arguments_to_configuration_input_with_limit_output()
{
Configuration.RegularOutput = false;
_because();
Configuration.Input.ShouldEqual("pkg1 pkg2");
}

[NUnit.Framework.TestCase("-l")]
[NUnit.Framework.TestCase("-lo")]
[NUnit.Framework.TestCase("--local-only")]
[NUnit.Framework.TestCase("--localonly")]
public void Should_output_warning_message_about_unsupported_argument(string argument)
public void Should_output_warning_message_about_unsupported_argument_with_limit_output(string argument)
{
Configuration.RegularOutput = false;
_because();
MockLogger.Messages.Keys.ShouldContain("Warn");
MockLogger.Messages["Warn"].ShouldContain(@"
Invalid argument {0}. This argument has been removed from the list command and cannot be used.".FormatWith(argument));
MockLogger.Messages["Warn"].ShouldContain("Ignoring the argument {0}. This argument is unsupported for locally installed packages.".FormatWith(argument));
}

[NUnit.Framework.TestCase("-li")]
[NUnit.Framework.TestCase("-lai")]
public void Should_output_warning_message_about_unsupported_argument_and_set_include_programs(string argument)
public void Should_output_warning_message_about_unsupported_argument_and_set_include_programs_with_limit_output(string argument)
{
Configuration.RegularOutput = false;
_because();
MockLogger.Messages.Keys.ShouldContain("Warn");
MockLogger.Messages["Warn"].ShouldContain(@"
Invalid argument {0}. This argument has been removed from the list command and cannot be used.".FormatWith(argument));
MockLogger.Messages["Warn"].ShouldContain("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 @@ -156,25 +156,17 @@ public virtual void ParseAdditionalArguments(IList<string> unparsedArguments, Ch

if (isUnsupportedArgument || isUnsupportedRegistryProgramsArgument)
{
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;
throw new ApplicationException("Invalid argument {0}. This argument has been removed from the list command and cannot be used.".FormatWith(argument));
}
else

if (isUnsupportedRegistryProgramsArgument)
{
this.Log().Warn(ChocolateyLoggers.LogFileOnly, @"
Ignoring the argument {0}. This argument is unsupported for locally installed packages.", argument);
configuration.ListCommand.IncludeRegistryPrograms = true;
}

this.Log().Warn(ChocolateyLoggers.LogFileOnly, "Ignoring the argument {0}. This argument is unsupported for locally installed packages.", argument);
}
else
{
Expand Down
8 changes: 4 additions & 4 deletions tests/chocolatey-tests/commands/choco-list.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ Describe "choco list" -Tag Chocolatey, ListCommand {
}
}

Context "Listing local packages with unsupported argument outputs a 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 errors out" -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 {
$Output = Invoke-Choco list $_
}
Expand All @@ -108,12 +108,12 @@ Describe "choco list" -Tag Chocolatey, ListCommand {
$Output.ExitCode | Should -Be 1
}

It "Should output expected warning message" {
It "Should output expected error message" {
$Output.Lines | Should -Contain "Invalid argument $_. This argument has been removed from the list command and cannot be used." -Because $Output.String
}
}

Context "Listing local packages with unsupported argument and --limit-output does not output a 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 and --limit-output allows listing packages" -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 {
$Output = Invoke-Choco list $_ --limit-output
}
Expand All @@ -122,7 +122,7 @@ Describe "choco list" -Tag Chocolatey, ListCommand {
$Output.ExitCode | Should -Be 0
}

It "Should not output the warning message" {
It "Should not output the error message" {
$Output.Lines | Should -Not -Contain "Invalid argument $_. This argument has been removed from the list command and cannot be used." -Because $Output.String
}
}
Expand Down

0 comments on commit ebef819

Please sign in to comment.