Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#1759) Add enhanced exit codes for multiple commands #2533

Merged
merged 8 commits into from
Apr 23, 2024
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
Loading