From 1f961e13df51256518fcb0b179d9b97f99a49455 Mon Sep 17 00:00:00 2001 From: Cory Knox Date: Sat, 4 Feb 2023 07:48:10 -0800 Subject: [PATCH 1/3] (tests) Update force dependencies tests Update the integration test scenarios for install scenario to ensure that forcing reinstallation does not erroneously remove any existing packages. --- src/chocolatey.tests.integration/Scenario.cs | 5 +++++ .../scenarios/InstallScenarios.cs | 15 +++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/chocolatey.tests.integration/Scenario.cs b/src/chocolatey.tests.integration/Scenario.cs index 2602fe8359..d26d3cc9fc 100644 --- a/src/chocolatey.tests.integration/Scenario.cs +++ b/src/chocolatey.tests.integration/Scenario.cs @@ -49,6 +49,11 @@ public static string get_package_install_path() return _fileSystem.combine_paths(get_top_level(), "lib"); } + public static IEnumerable 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(); diff --git a/src/chocolatey.tests.integration/scenarios/InstallScenarios.cs b/src/chocolatey.tests.integration/scenarios/InstallScenarios.cs index 91ad33dca2..c77feb3bbf 100644 --- a/src/chocolatey.tests.integration/scenarios/InstallScenarios.cs +++ b/src/chocolatey.tests.integration/scenarios/InstallScenarios.cs @@ -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 _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; } @@ -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() { From dbf50ffde66c95e8bc6f6c3ea6ce7f9a485362d0 Mon Sep 17 00:00:00 2001 From: Cory Knox Date: Sat, 4 Feb 2023 07:44:58 -0800 Subject: [PATCH 2/3] (#508) Only dependencies if Forcing Dependencies When forcing dependencies, we need to ensure that we're only giving the dependencies to the service to force reinstallation. This updates the logic to only get the packages that are dependencies. --- .../infrastructure.app/services/NugetService.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/chocolatey/infrastructure.app/services/NugetService.cs b/src/chocolatey/infrastructure.app/services/NugetService.cs index f8dd7b799e..39777beb31 100644 --- a/src/chocolatey/infrastructure.app/services/NugetService.cs +++ b/src/chocolatey/infrastructure.app/services/NugetService.cs @@ -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, @@ -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( From 938d790fe14726abd3491d04c35cbbd34dec163a Mon Sep 17 00:00:00 2001 From: Cory Knox Date: Sat, 4 Feb 2023 07:46:18 -0800 Subject: [PATCH 3/3] (tests) Remove broken tag from force dependencies Remove the broken tag from the force dependencies pester tests as this scenario has been fixed. --- tests/chocolatey-tests/commands/choco-install.Tests.ps1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/chocolatey-tests/commands/choco-install.Tests.ps1 b/tests/chocolatey-tests/commands/choco-install.Tests.ps1 index 3e8aa1be7c..16a256abba 100644 --- a/tests/chocolatey-tests/commands/choco-install.Tests.ps1 +++ b/tests/chocolatey-tests/commands/choco-install.Tests.ps1 @@ -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 @@ -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 } @@ -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." } }