diff --git a/src/chocolatey.tests/infrastructure.app/commands/ChocolateyInstallCommandSpecs.cs b/src/chocolatey.tests/infrastructure.app/commands/ChocolateyInstallCommandSpecs.cs index f32acfd5e6..0e34855e57 100644 --- a/src/chocolatey.tests/infrastructure.app/commands/ChocolateyInstallCommandSpecs.cs +++ b/src/chocolatey.tests/infrastructure.app/commands/ChocolateyInstallCommandSpecs.cs @@ -25,7 +25,9 @@ namespace chocolatey.tests.infrastructure.app.commands using chocolatey.infrastructure.app.domain; using chocolatey.infrastructure.app.services; using chocolatey.infrastructure.commandline; + using chocolatey.infrastructure.commands; using Moq; + using NuGet; using Should; public class ChocolateyInstallCommandSpecs @@ -34,12 +36,16 @@ public abstract class ChocolateyInstallCommandSpecsBase : TinySpec { protected ChocolateyInstallCommand command; protected Mock packageService = new Mock(); + protected Mock chocolateyPackageInformationService = new Mock(); + protected Mock nugetService = new Mock(); + protected Mock nugetLogger = new Mock(); protected ChocolateyConfiguration configuration = new ChocolateyConfiguration(); public override void Context() { configuration.Sources = "bob"; - command = new ChocolateyInstallCommand(packageService.Object); + var chocolateyPinCommand = new ChocolateyPinCommand(chocolateyPackageInformationService.Object, nugetLogger.Object, nugetService.Object); + command = new ChocolateyInstallCommand(packageService.Object, chocolateyPinCommand, nugetLogger.Object); } } @@ -235,6 +241,12 @@ public void should_add_short_version_of_password_to_the_option_set() { optionSet.Contains("p").ShouldBeTrue(); } + + [Fact] + public void should_add_pinVersion_to_the_option_set() + { + optionSet.Contains("pin-version").ShouldBeTrue(); + } } public class when_handling_additional_argument_parsing : ChocolateyInstallCommandSpecsBase diff --git a/src/chocolatey/infrastructure.app/commands/ChocolateyInstallCommand.cs b/src/chocolatey/infrastructure.app/commands/ChocolateyInstallCommand.cs index ba9cf33556..242d5d09a6 100644 --- a/src/chocolatey/infrastructure.app/commands/ChocolateyInstallCommand.cs +++ b/src/chocolatey/infrastructure.app/commands/ChocolateyInstallCommand.cs @@ -20,21 +20,27 @@ namespace chocolatey.infrastructure.app.commands using System.Collections.Generic; using System.Linq; using attributes; + using chocolatey.infrastructure.app.nuget; using commandline; using configuration; using infrastructure.commands; using infrastructure.configuration; using logging; + using NuGet; using services; [CommandFor("install", "installs packages from various sources")] public class ChocolateyInstallCommand : ICommand { private readonly IChocolateyPackageService _packageService; + private readonly ChocolateyPinCommand _chocolateyPinCommand; + private readonly ILogger _nugetLogger; - public ChocolateyInstallCommand(IChocolateyPackageService packageService) + public ChocolateyInstallCommand(IChocolateyPackageService packageService, ChocolateyPinCommand chocolateyPinCommand, ILogger nugetLogger) { _packageService = packageService; + _chocolateyPinCommand = chocolateyPinCommand; + _nugetLogger = nugetLogger; } public virtual void configure_argument_parser(OptionSet optionSet, ChocolateyConfiguration configuration) @@ -180,6 +186,15 @@ public virtual void configure_argument_parser(OptionSet optionSet, ChocolateyCon configuration.Features.UsePackageRepositoryOptimizations = false; } }) + .Add("pin-version", + "Pin version to prevent future upgrades.", + option => + { + if (option != null) + { + configuration.Features.PinVersion = true; + } + }) ; //todo: package name can be a url / installertype @@ -436,6 +451,17 @@ public virtual void run(ChocolateyConfiguration configuration) { _packageService.ensure_source_app_installed(configuration); _packageService.install_run(configuration); + + if (configuration.Features.PinVersion) + { + var packageManager = NugetCommon.GetPackageManager(configuration, _nugetLogger, + new PackageDownloader(), + installSuccessAction: null, + uninstallSuccessAction: null, + addUninstallHandler: false); + + _chocolateyPinCommand.set_pin(packageManager, configuration); + } } public virtual bool may_require_admin_access() diff --git a/src/chocolatey/infrastructure.app/configuration/ChocolateyConfiguration.cs b/src/chocolatey/infrastructure.app/configuration/ChocolateyConfiguration.cs index c5cc33f73f..9892ccb12e 100644 --- a/src/chocolatey/infrastructure.app/configuration/ChocolateyConfiguration.cs +++ b/src/chocolatey/infrastructure.app/configuration/ChocolateyConfiguration.cs @@ -396,6 +396,7 @@ public sealed class FeaturesConfiguration public bool ExitOnRebootDetected { get; set; } public bool LogValidationResultsOnWarnings { get; set; } public bool UsePackageRepositoryOptimizations { get; set; } + public bool PinVersion { get; set; } //todo remove in 0.11.0 public bool ScriptsCheckLastExitCode { get; set; } diff --git a/src/chocolatey/infrastructure.app/registration/ContainerBinding.cs b/src/chocolatey/infrastructure.app/registration/ContainerBinding.cs index 67ffc551b5..e0f76ab512 100644 --- a/src/chocolatey/infrastructure.app/registration/ContainerBinding.cs +++ b/src/chocolatey/infrastructure.app/registration/ContainerBinding.cs @@ -81,7 +81,7 @@ public void RegisterComponents(Container container) { new ChocolateyListCommand(container.GetInstance()), new ChocolateyInfoCommand(container.GetInstance()), - new ChocolateyInstallCommand(container.GetInstance()), + new ChocolateyInstallCommand(container.GetInstance(), container.GetInstance(), container.GetInstance()), new ChocolateyPinCommand(container.GetInstance(), container.GetInstance(), container.GetInstance()), new ChocolateyOutdatedCommand(container.GetInstance()), new ChocolateyUpgradeCommand(container.GetInstance()),