Skip to content

Commit

Permalink
Merge pull request #3248 from TheCakeIsNaOH/upgrade-list-cache
Browse files Browse the repository at this point in the history
(#3231) Don't refresh local package info during upgrade no-ops
  • Loading branch information
corbob authored Jul 11, 2023
2 parents 2f79fb6 + cd92db1 commit 03c5aaa
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
48 changes: 43 additions & 5 deletions src/chocolatey.tests.integration/scenarios/UpgradeScenarios.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2017 - 2021 Chocolatey Software, Inc
// Copyright © 2017 - 2021 Chocolatey Software, Inc
// Copyright © 2011 - 2017 RealDimensions Software, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -19,12 +19,9 @@ namespace chocolatey.tests.integration.scenarios
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Threading;
using System.Xml.XPath;
using chocolatey.infrastructure.app.commands;
using chocolatey.infrastructure.app.configuration;
using chocolatey.infrastructure.app.services;
using chocolatey.infrastructure.filesystem;
Expand All @@ -33,7 +30,6 @@ namespace chocolatey.tests.integration.scenarios
using NuGet.Packaging;
using NUnit.Framework;
using FluentAssertions;
using static chocolatey.tests.integration.scenarios.InstallScenarios;

public class UpgradeScenarios
{
Expand Down Expand Up @@ -3829,6 +3825,48 @@ public void Should_skip_packages_in_except_list()
}
}

public class When_upgrading_all_packages_multiple_upgraded_packages : ScenariosBase
{
public override void Context()
{
base.Context();
Scenario.AddPackagesToSourceLocation(Configuration, "isdependency.1.*");
Scenario.InstallPackage(Configuration, "isdependency", "1.0.0");
Configuration.PackageNames = Configuration.Input = "all";
}

public override void Because()
{
Results = Service.Upgrade(Configuration);
}

[Fact]
public void Should_report_start_of_list_for_upgraded_packages_only()
{
var startOfLists = MockLogger.Messages["Debug"].Where(m => m == "--- Start of List ---");
startOfLists.Should().HaveCount(3);
}

[Fact]
public void Should_upgrade_packages_with_upgrades()
{
var upgradePackageResult = Results.Where(x => (x.Key == "upgradepackage" || x.Key == "isdependency")).ToList();
upgradePackageResult.Should().HaveCount(2);
upgradePackageResult.ForEach(r =>
{
r.Value.Version.Should().Be("1.1.0");
});
}

[Fact]
public void Should_skip_packages_without_upgrades()
{
var installPackageResult = Results.Where(x => x.Key == "installpackage").ToList();
installPackageResult.Should().ContainSingle("installpackage must be there once");
installPackageResult.First().Value.Version.Should().Be("1.0.0");
}
}

public class When_upgrading_an_existing_hook_package : ScenariosBase
{
private PackageResult _packageResult;
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 @@ -970,8 +970,9 @@ public virtual ConcurrentDictionary<string, PackageResult> Upgrade(ChocolateyCon
var projectContext = new ChocolateyNuGetProjectContext(config, _nugetLogger);

var configIgnoreDependencies = config.IgnoreDependencies;
SetPackageNamesIfAllSpecified(config, () => { config.IgnoreDependencies = true; });
var allLocalPackages = SetPackageNamesIfAllSpecified(config, () => { config.IgnoreDependencies = true; }).ToList();
config.IgnoreDependencies = configIgnoreDependencies;
var localPackageListValid = true;

config.CreateBackup();

Expand All @@ -981,7 +982,12 @@ public virtual ConcurrentDictionary<string, PackageResult> Upgrade(ChocolateyCon
// before we start reading it.
config.RevertChanges();

var allLocalPackages = GetInstalledPackages(config).ToList();
if (!localPackageListValid)
{
allLocalPackages = GetInstalledPackages(config).ToList();
localPackageListValid = true;
}

var installedPackage = allLocalPackages.FirstOrDefault(p => p.Name.IsEqualTo(packageName));
var packagesToInstall = new List<IPackageSearchMetadata>();
var packagesToUninstall = new HashSet<PackageResult>();
Expand Down Expand Up @@ -1013,6 +1019,7 @@ public virtual ConcurrentDictionary<string, PackageResult> Upgrade(ChocolateyCon
}

string logMessage = @"{0} is not installed. Installing...".FormatWith(packageName);
localPackageListValid = false;

if (config.RegularOutput) this.Log().Warn(ChocolateyLoggers.Important, logMessage);

Expand Down Expand Up @@ -1177,6 +1184,7 @@ public virtual ConcurrentDictionary<string, PackageResult> Upgrade(ChocolateyCon

if (performAction)
{
localPackageListValid = false;

NugetCommon.GetPackageDependencies(availablePackage.Identity, NuGetFramework.AnyFramework, sourceCacheContext, _nugetLogger, remoteEndpoints, sourcePackageDependencyInfos, sourceDependencyCache, config).GetAwaiter().GetResult();

Expand Down

0 comments on commit 03c5aaa

Please sign in to comment.