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

(#508) Fix force dependencies to not remove existing non-dependency packages #3013

Merged
merged 3 commits into from
Feb 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/chocolatey.tests.integration/Scenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public static string get_package_install_path()
return _fileSystem.combine_paths(get_top_level(), "lib");
}

public static IEnumerable<string> get_installed_package_paths()
{
return _fileSystem.get_files(get_package_install_path(), "*" + NuGetConstants.PackageExtension, SearchOption.AllDirectories);
}

public static void reset(ChocolateyConfiguration config)
{
string packagesInstallPath = get_package_install_path();
Expand Down
15 changes: 15 additions & 0 deletions src/chocolatey.tests.integration/scenarios/InstallScenarios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2153,16 +2153,22 @@ public void should_have_a_version_of_one_dot_zero_dot_zero()

public class when_force_installing_an_already_installed_package_forcing_dependencies : ScenariosBase
{
private IEnumerable<string> _installedPackagePaths;
public override void Context()
{
base.Context();

Scenario.add_packages_to_source_location(Configuration, "installpackage*" + NuGetConstants.PackageExtension);
Scenario.install_package(Configuration, "installpackage", "1.0.0");

Configuration.PackageNames = Configuration.Input = "hasdependency";
Scenario.add_packages_to_source_location(Configuration, "hasdependency.1*" + NuGetConstants.PackageExtension);
Scenario.add_packages_to_source_location(Configuration, "isdependency.1.0.0*" + NuGetConstants.PackageExtension);
Scenario.add_packages_to_source_location(Configuration, "isexactversiondependency*" + NuGetConstants.PackageExtension);
Scenario.install_package(Configuration, "hasdependency", "1.0.0");
Scenario.add_packages_to_source_location(Configuration, "isdependency*" + NuGetConstants.PackageExtension);
_installedPackagePaths = Scenario.get_installed_package_paths().ToList();

Configuration.Force = true;
Configuration.ForceDependencies = true;
}
Expand Down Expand Up @@ -2199,6 +2205,15 @@ public void should_reinstall_the_exact_same_version_of_the_package()
}
}

[Fact]
public void should_not_remove_any_existing_packages_in_the_lib_directory()
{
foreach (var packagePath in _installedPackagePaths)
{
FileAssert.Exists(packagePath);
}
}

[Fact]
public void should_install_the_dependency_in_the_lib_directory()
{
Expand Down
12 changes: 10 additions & 2 deletions src/chocolatey/infrastructure.app/services/NugetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,10 @@ Version was specified as '{0}'. It is possible that version
var targetIdsToInstall = packagesToInstall.Select(p => p.Identity.Id);

var localPackagesDependencyInfos = allLocalPackages
.Where(p => !targetIdsToInstall.Contains(p.Name, StringComparer.OrdinalIgnoreCase))
// If we're forcing dependencies, we only need to know which dependencies are installed locally
.Where(p => config.ForceDependencies
? targetIdsToInstall.Contains(p.Name, StringComparer.OrdinalIgnoreCase)
: !targetIdsToInstall.Contains(p.Name, StringComparer.OrdinalIgnoreCase))
.Select(
p => new SourcePackageDependencyInfo(
p.SearchMetadata.Identity,
Expand All @@ -619,7 +622,12 @@ Version was specified as '{0}'. It is possible that version

var dependencyResolver = new PackageResolver();

var allPackagesIdentities = allLocalPackages.Select(p => p.SearchMetadata.Identity).Where(p => !targetIdsToInstall.Contains(p.Id, StringComparer.OrdinalIgnoreCase)).ToList();
var allPackagesIdentities = allLocalPackages
.Select(p => p.SearchMetadata.Identity)
// If we're forcing dependencies, we only need to know which dependencies are installed locally, not the entire list of packages
.Where(p => config.ForceDependencies
? sourcePackageDependencyInfos.Any(s => s.Id == p.Id)
: !targetIdsToInstall.Contains(p.Id, StringComparer.OrdinalIgnoreCase)).ToList();
var allPackagesReferences = allPackagesIdentities.Select(p => new PackageReference(p, NuGetFramework.AnyFramework));

var resolverContext = new PackageResolverContext(
Expand Down
6 changes: 3 additions & 3 deletions tests/chocolatey-tests/commands/choco-install.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Import-Module helpers/common-helpers
Import-Module helpers/common-helpers

# https://github.com/chocolatey/choco/blob/master/src/chocolatey.tests.integration/scenarios/InstallScenarios.cs

Expand Down Expand Up @@ -769,7 +769,7 @@ Describe "choco install" -Tag Chocolatey, InstallCommand {
$Output = Invoke-Choco install $PackageUnderTest --force --forcedependencies --confirm
}

It "Exits with Success (0)" -Tag Broken {
It "Exits with Success (0)" {
$Output.ExitCode | Should -Be 0
}

Expand Down Expand Up @@ -803,7 +803,7 @@ Describe "choco install" -Tag Chocolatey, InstallCommand {
$XML.package.metadata.version | Should -Be "1.0.0"
}

It "Outputs a message indicating that it installed the package(s) successfully" -Tag Broken {
It "Outputs a message indicating that it installed the package(s) successfully" {
$Output.Lines | Should -Contain "Chocolatey installed 3/3 packages."
}
}
Expand Down