Skip to content

Commit

Permalink
Merge pull request #3200 from TheCakeIsNaOH/fix-remove-pin
Browse files Browse the repository at this point in the history
(#3196) Pin add/remove use exact package ID
  • Loading branch information
gep13 authored Jun 7, 2023
2 parents 5961b94 + b537989 commit a500fc5
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public abstract class ChocolateyPinCommandSpecsBase : TinySpec
protected ChocolateyConfiguration configuration = new ChocolateyConfiguration();
protected Mock<IPackageMetadata> package = new Mock<IPackageMetadata>();
protected Mock<IPackageMetadata> pinnedPackage = new Mock<IPackageMetadata>();
protected Mock<IPackageMetadata> mingwPackage = new Mock<IPackageMetadata>();
protected Mock<IPackageMetadata> gstreamerMingwPackage = new Mock<IPackageMetadata>();

public override void Context()
{
Expand All @@ -72,6 +74,23 @@ public override void Context()
{
IsPinned = true
});

mingwPackage = new Mock<IPackageMetadata>();
mingwPackage.Setup(p => p.Id).Returns("mingw");
mingwPackage.Setup(p => p.Version).Returns(new NuGetVersion("1.0.0"));
packageInfoService.Setup(s => s.Get(mingwPackage.Object)).Returns(
new ChocolateyPackageInformation(mingwPackage.Object)
{
IsPinned = true
});
gstreamerMingwPackage = new Mock<IPackageMetadata>();
gstreamerMingwPackage.Setup(p => p.Id).Returns("gstreamer-mingw");
gstreamerMingwPackage.Setup(p => p.Version).Returns(new NuGetVersion("1.0.0"));
packageInfoService.Setup(s => s.Get(gstreamerMingwPackage.Object)).Returns(
new ChocolateyPackageInformation(gstreamerMingwPackage.Object)
{
IsPinned = true
});
}

public void Reset()
Expand Down Expand Up @@ -461,5 +480,62 @@ public void Should_remove_pin_when_command_is_remove()
}
}

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

var packageResults = new[]
{
new PackageResult(mingwPackage.Object, null),
new PackageResult(gstreamerMingwPackage.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_call_nuget_service_list_run_when_command_is_list()
{
Reset();
configuration.PinCommand.Command = PinCommandType.List;
command.Run(configuration);

nugetService.Verify(n => n.List(It.IsAny<ChocolateyConfiguration>()), Times.Once);
}

[Fact]
public void Should_remove_pin_from_correct_package()
{
Reset();
configuration.PinCommand.Name = "mingw";
configuration.PinCommand.Command = PinCommandType.Remove;

command.SetPin(configuration);

packageInfoService.Verify(s =>
s.Save(It.Is<ChocolateyPackageInformation>(n =>
n.Package.Id.Equals("mingw"))), Times.Once);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,12 @@ public virtual void SetPin(ChocolateyConfiguration config)
config.Input = config.PinCommand.Name;
config.Version = semanticVersion.ToFullStringChecked();
config.ListCommand.ByIdOnly = true;
var exact = config.ListCommand.Exact;
config.ListCommand.Exact = true;
var quiet = config.QuietOutput;
config.QuietOutput = true;
var installedPackage = _nugetService.List(config).FirstOrDefault();
config.ListCommand.Exact = exact;
config.QuietOutput = quiet;
config.Input = input;

Expand Down

0 comments on commit a500fc5

Please sign in to comment.