Skip to content

Commit

Permalink
(test) Add unit tests to cover exit code changes
Browse files Browse the repository at this point in the history
Within the following issues, a number of changes were made to allow
Chocolatey CLI to exit with a ExitCode of 2, when no action was taken:

chocolatey#2200 - Upgrade
chocolatey#1764 - Source
chocolatey#1762 - Pin
chocolatey#1761 - Feature
chocolatey#1760 - Config
chocolatey#1759 - ApiKey

This commit adds the unit tests to exercise these changes to ensure
that they work as expected, and also that they continue to work as
additional changes are made.

No new test files have been created in this commit, but rather test
were added to existing test files.

The tests that have been added are a carbon copy of each other, to an
extent, i.e. setup and run the required action, then assert the logs
and ExitCode of 0.  Then enable the UseEnhancedExitCodes feature and
run the action again and assert the logs and ExitCode of 2.

One change was made to the TinySpec file to ensure that the
Environment.ExitCode is set to 0 (default) at the start of each test
execution.  This was required due to the fact that some test executions
result in the ExitCode being set to 2 explicitly, but the next test
didn't set it to 0, due to the fact that it relies on the default value
being returned.  Since this is a concern across all test executions,
the decision was made to put it into the TinySpec file.
  • Loading branch information
gep13 committed Apr 23, 2024
1 parent a26c92b commit 50c22ed
Show file tree
Hide file tree
Showing 4 changed files with 1,124 additions and 4 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 50c22ed

Please sign in to comment.