Skip to content

Commit

Permalink
Merge pull request #2533 from TheCakeIsNaOH/apikey-exit-code
Browse files Browse the repository at this point in the history
(#1759) Add enhanced exit codes for multiple commands
  • Loading branch information
gep13 authored Apr 23, 2024
2 parents 3c77b7c + 65098d1 commit 2596f28
Show file tree
Hide file tree
Showing 13 changed files with 1,228 additions and 21 deletions.
8 changes: 8 additions & 0 deletions src/chocolatey.tests/TinySpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ public void Setup()
{
MockLogger.Reset();
}

// Chocolatey CLI by default will exit with Code 0, when everything work as expected, even if it doesn't
// set this explicitly.
// However, in some tests, we are testing for the setting of an explicit exit code, and when we do this,
// it can have an impact on other tests, since it may not have been reset. Let's explicitly set it to
// 0 before running each test, so that everything starts off at the right place.
Environment.ExitCode = default;

//Log.InitializeWith(MockLogger);
NugetCommon.ClearRepositoriesCache();
Context();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
using NUnit.Framework;

using FluentAssertions;
using FluentAssertions.Execution;

namespace chocolatey.tests.infrastructure.app.commands
{
Expand Down Expand Up @@ -537,5 +538,139 @@ public void Should_remove_pin_from_correct_package()
n.Package.Id.Equals("mingw"))), Times.Once);
}
}

public class When_adding_a_pin_on_an_already_pinned_package : ChocolateyPinCommandSpecsBase
{
public override void Context()
{
base.Context();
Configuration.Sources = ApplicationParameters.PackagesLocation;
Configuration.ListCommand.LocalOnly = true;
Configuration.AllVersions = true;

var packageResults = new[]
{
new PackageResult(PinnedPackage.Object, null)
};
NugetService.Setup(n => n.List(It.IsAny<ChocolateyConfiguration>())).Returns(packageResults);
}

public new void Reset()
{
Context();
base.Reset();
}

public override void AfterEachSpec()
{
base.AfterEachSpec();
MockLogger.Messages.Clear();
}

public override void Because()
{
}

[Fact]
public void Should_Return_0_ExitCode_When_Pinning_An_Already_Pinned_Package()
{
Reset();
Configuration.Features.UseEnhancedExitCodes = false;
Configuration.PinCommand.Name = "pinned";
Configuration.PinCommand.Command = PinCommandType.Add;

Command.SetPin(Configuration);

using (new AssertionScope())
{
MockLogger.Messages.Should().ContainKey("Warn").WhoseValue.Should().ContainSingle().And.Contain("Nothing to change. Pin already set or removed.");
Environment.ExitCode.Should().Be(0);
}
}

[Fact]
public void Should_Return_2_ExitCode_When_Pinning_An_Already_Pinned_Package_With_UseEnhancedExitCodes_Enabled()
{
Reset();
Configuration.Features.UseEnhancedExitCodes = true;
Configuration.PinCommand.Name = "pinned";
Configuration.PinCommand.Command = PinCommandType.Add;

Command.SetPin(Configuration);

using (new AssertionScope())
{
MockLogger.Messages.Should().ContainKey("Warn").WhoseValue.Should().ContainSingle().And.Contain("Nothing to change. Pin already set or removed.");
Environment.ExitCode.Should().Be(2);
}
}
}

public class When_removing_a_pin_on_a_package_with_no_pin : ChocolateyPinCommandSpecsBase
{
public override void Context()
{
base.Context();
Configuration.Sources = ApplicationParameters.PackagesLocation;
Configuration.ListCommand.LocalOnly = true;
Configuration.AllVersions = true;

var packageResults = new[]
{
new PackageResult(Package.Object, null)
};
NugetService.Setup(n => n.List(It.IsAny<ChocolateyConfiguration>())).Returns(packageResults);
}

public new void Reset()
{
Context();
base.Reset();
}

public override void AfterEachSpec()
{
base.AfterEachSpec();
MockLogger.Messages.Clear();
}

public override void Because()
{
}

[Fact]
public void Should_Return_0_ExitCode_When_Pinning_An_Already_Pinned_Package()
{
Reset();
Configuration.Features.UseEnhancedExitCodes = false;
Configuration.PinCommand.Name = "regular";
Configuration.PinCommand.Command = PinCommandType.Remove;

Command.SetPin(Configuration);

using (new AssertionScope())
{
MockLogger.Messages.Should().ContainKey("Warn").WhoseValue.Should().ContainSingle().And.Contain("Nothing to change. Pin already set or removed.");
Environment.ExitCode.Should().Be(0);
}
}

[Fact]
public void Should_Return_2_ExitCode_When_Pinning_An_Already_Pinned_Package_With_UseEnhancedExitCodes_Enabled()
{
Reset();
Configuration.Features.UseEnhancedExitCodes = true;
Configuration.PinCommand.Name = "regular";
Configuration.PinCommand.Command = PinCommandType.Remove;

Command.SetPin(Configuration);

using (new AssertionScope())
{
MockLogger.Messages.Should().ContainKey("Warn").WhoseValue.Should().ContainSingle().And.Contain("Nothing to change. Pin already set or removed.");
Environment.ExitCode.Should().Be(2);
}
}
}
}
}
Loading

0 comments on commit 2596f28

Please sign in to comment.