Skip to content

Commit

Permalink
Merge pull request #2726 from AdmiringWorm/1956-Cant-update-pkg-due-t…
Browse files Browse the repository at this point in the history
…o-chocolatey-coreextension-version-mismatch

(#1956) Fix packages not being upgraded if SxS installations are available
  • Loading branch information
gep13 authored Jun 30, 2022
2 parents 6f3fc15 + 3d5c4a5 commit 486cd95
Show file tree
Hide file tree
Showing 3 changed files with 215 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,42 @@ public ChocolateyLocalPackageRepository(IPackagePathResolver pathResolver, IFile
{
}

public bool IgnoreVersionedDirectories { get; set; }

public override IQueryable<IPackage> GetPackages()
{
var packages = base.GetPackages();

if (IgnoreVersionedDirectories)
{
packages = packages.Where(ExcludeVersionedDirectories).ToList().AsQueryable();
}

return packages;
}

public override IPackage FindPackage(string packageId, SemanticVersion version)
{
if (IgnoreVersionedDirectories)
{
return FindPackagesById(packageId).FirstOrDefault(package => package.Version >= version);
}

return base.FindPackage(packageId, version);
}

public override IEnumerable<IPackage> FindPackagesById(string packageId)
{
var packages = base.FindPackagesById(packageId);

if (IgnoreVersionedDirectories)
{
packages = packages.Where(ExcludeVersionedDirectories);
}

return packages;
}

public override void AddPackage(IPackage package)
{
string packageFilePath = GetPackageFilePath(package);
Expand All @@ -72,6 +108,19 @@ public override void AddPackage(IPackage package)
}
}

private bool ExcludeVersionedDirectories(IPackage package)
{
var directoryPath = PathResolver.GetInstallPath(package);
if (string.IsNullOrWhiteSpace(directoryPath))
{
return true;
}

var directoryName = Path.GetFileName(directoryPath);

return string.Compare(directoryName, package.Id + "." + package.Version, StringComparison.OrdinalIgnoreCase) != 0;
}

private string GetManifestFilePath(string packageId, SemanticVersion version)
{
string packageDirectory = PathResolver.GetPackageDirectory(packageId, version);
Expand Down
16 changes: 11 additions & 5 deletions src/chocolatey/infrastructure.app/services/NugetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,24 @@ namespace chocolatey.infrastructure.app.services
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using NuGet;
using adapters;
using chocolatey.infrastructure.app.utility;
using commandline;
using configuration;
using domain;
using guards;
using logging;
using nuget;
using NuGet;
using platforms;
using results;
using tolerance;
using DateTime = adapters.DateTime;
using Environment = System.Environment;
using IFileSystem = filesystem.IFileSystem;
using chocolatey.infrastructure.app.utility;

//todo: #2575 - this monolith is too large. Refactor once test coverage is up.

Expand Down Expand Up @@ -618,6 +617,13 @@ public virtual ConcurrentDictionary<string, PackageResult> upgrade_run(Chocolate
uninstallSuccessAction: null,
addUninstallHandler: false);

var localRepository = packageManager.LocalRepository as ChocolateyLocalPackageRepository;

if (localRepository != null)
{
localRepository.IgnoreVersionedDirectories = !config.AllowMultipleVersions;
}

var configIgnoreDependencies = config.IgnoreDependencies;
set_package_names_if_all_is_specified(config, () => { config.IgnoreDependencies = true; });
config.IgnoreDependencies = configIgnoreDependencies;
Expand Down Expand Up @@ -927,7 +933,7 @@ public virtual ConcurrentDictionary<string, PackageResult> get_outdated(Chocolat
config.Prerelease = true;
}

SemanticVersion version = null;
SemanticVersion version = null;
var latestPackage = NugetList.find_package(packageName, null, config, packageManager.SourceRepository);

if (latestPackage == null)
Expand Down Expand Up @@ -1112,7 +1118,7 @@ public virtual void remove_packaging_files_prior_to_upgrade(string directoryPath
// script could be incorrectly left in place during an upgrade operation. To guard against this,
// remove any Chocolatey Packaging scripts, which will then be restored by the new package, if
// they are still required
var filesToDelete = new List<string> {"chocolateyinstall", "chocolateyuninstall", "chocolateybeforemodify"};
var filesToDelete = new List<string> { "chocolateyinstall", "chocolateyuninstall", "chocolateybeforemodify" };
var packagingScripts = _fileSystem.get_files(directoryPath, "*.ps1", SearchOption.AllDirectories)
.Where(p => filesToDelete.Contains(_fileSystem.get_file_name_without_extension(p).to_lower()));

Expand Down
155 changes: 155 additions & 0 deletions tests/chocolatey-tests/choco-upgrade.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
Import-Module helpers/common-helpers

Describe "choco upgrade" -Tag Chocolatey, UpgradeCommand {
BeforeAll {
Initialize-ChocolateyTestInstall

New-ChocolateyInstallSnapshot
}

AfterAll {
Remove-ChocolateyTestInstall
}

Context "Can upgrade packages with dependencies containing side by side installations and outdated dependency" {
BeforeAll {
Restore-ChocolateyInstallSnapshot

$null = Invoke-Choco install chocolatey-core.extension --version 1.3.0 --confirm
$null = Invoke-Choco install 7zip --version 16.04 --confirm
$null = Invoke-Choco install chocolatey-core.extension --version 1.3.5.1 --sxs --confirm

$Output = Invoke-Choco upgrade 7zip --version 21.7 --confirm
}

AfterAll {
$null = Invoke-Choco uninstall 7zip 7zip.install --confirm
}

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

It "Upgrades version <OldVersion> of the package <Name>" -ForEach @(
@{ Name = "7zip"; OldVersion = "16.04" }
@{ Name = "7zip.install"; OldVersion = "16.04" }
@{ Name = "chocolatey-core.extension"; OldVersion = "1.3.0" }
) {
"$env:ChocolateyInstall\lib\$Name\$Name.nuspec" | Should -Exist
[xml]$XML = Get-Content "$env:ChocolateyInstall\lib\$Name\$Name.nuspec"
$XML.package.metadata.version | Should -Not -Be $OldVersion
}

It "Have not upgraded side by side installation of <Name> v<Version>" -ForEach @(
@{ Name = "chocolatey-core.extension"; Version = "1.3.5.1" }
) {
"$env:ChocolateyInstall\lib\$Name.$Version\$Name.$Version.nupkg" | Should -Exist
[xml]$XML = Get-Content "$env:ChocolateyInstall\lib\$Name.$Version\$Name.$Version.nuspec"
$XML.package.metadata.id | Should -Be $Name
$XML.package.metadata.version | Should -Be $Version
}

It "Outputs a message showing that upgrading was successful" {
$Output.String | SHould -Match "Chocolatey upgraded 3/3 packages\."
}
}

Context "Can upgrade packages with dependencies containing side by side installations and up to date dependency" {
BeforeAll {
Restore-ChocolateyInstallSnapshot

$null = Invoke-Choco install chocolatey-core.extension --version 1.3.3 --confirm
$null = Invoke-Choco install 7zip --version 16.04 --confirm
$null = Invoke-Choco install chocolatey-core.extension --version 1.3.5.1 --sxs --confirm

$Output = Invoke-Choco upgrade 7zip --version 21.7 --confirm
}

AfterAll {
$null = Invoke-Choco uninstall 7zip 7zip.install --confirm
}

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

It "Upgrades version <OldVersion> of the package <Name>" -ForEach @(
@{ Name = "7zip"; OldVersion = "16.04" }
@{ Name = "7zip.install"; OldVersion = "16.04" }
) {
"$env:ChocolateyInstall\lib\$Name\$Name.nuspec" | Should -Exist
[xml]$XML = Get-Content "$env:ChocolateyInstall\lib\$Name\$Name.nuspec"
$XML.package.metadata.version | Should -Not -Be $OldVersion
}

It "Have not upgraded dependency <Name>" {
"$env:ChocolateyInstall\lib\chocolatey-core.extension\chocolatey-core.extension.nupkg" | Should -Exist
[xml]$XML = Get-Content "$env:ChocolateyInstall\lib\chocolatey-core.extension\chocolatey-core.extension.nuspec"
$XML.package.metadata.id | Should -Be 'chocolatey-core.extension'
$XML.package.metadata.version | Should -Be '1.3.3'
}

It "Have not upgraded side by side installation of <Name> v<Version>" -ForEach @(
@{ Name = "chocolatey-core.extension"; Version = "1.3.5.1" }
) {
"$env:ChocolateyInstall\lib\$Name.$Version\$Name.$Version.nupkg" | Should -Exist
[xml]$XML = Get-Content "$env:ChocolateyInstall\lib\$Name.$Version\$Name.$Version.nuspec"
$XML.package.metadata.id | Should -Be $Name
$XML.package.metadata.version | Should -Be $Version
}

It "Outputs a message showing that upgrading was successful" {
$Output.String | SHould -Match "Chocolatey upgraded 2/2 packages\."
}
}


Context "Can upgrade packages with dependencies containing outdated side by side installations and up to date dependency" {
BeforeAll {
Restore-ChocolateyInstallSnapshot

$null = Invoke-Choco install chocolatey-core.extension --version 1.3.3 --confirm
$null = Invoke-Choco install 7zip --version 16.04 --confirm
$null = Invoke-Choco install chocolatey-core.extension --version 1.3.0 --sxs --confirm

$Output = Invoke-Choco upgrade 7zip --version 21.7 --confirm
}

AfterAll {
$null = Invoke-Choco uninstall 7zip 7zip.install --confirm
}

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

It "Upgrades version <OldVersion> of the package <Name>" -ForEach @(
@{ Name = "7zip"; OldVersion = "16.04" }
@{ Name = "7zip.install"; OldVersion = "16.04" }
) {
"$env:ChocolateyInstall\lib\$Name\$Name.nuspec" | Should -Exist
[xml]$XML = Get-Content "$env:ChocolateyInstall\lib\$Name\$Name.nuspec"
$XML.package.metadata.version | Should -Not -Be $OldVersion
}

It "Have not upgraded dependency chocolatey-core.extension" {
"$env:ChocolateyInstall\lib\chocolatey-core.extension\chocolatey-core.extension.nupkg" | Should -Exist
[xml]$XML = Get-Content "$env:ChocolateyInstall\lib\chocolatey-core.extension\chocolatey-core.extension.nuspec"
$XML.package.metadata.id | Should -Be 'chocolatey-core.extension'
$XML.package.metadata.version | Should -Be '1.3.3'
}

It "Have not upgraded side by side installation of <Name> v<Version>" -ForEach @(
@{ Name = "chocolatey-core.extension"; Version = "1.3.0" }
) {
"$env:ChocolateyInstall\lib\$Name.$Version\$Name.$Version.nupkg" | Should -Exist
[xml]$XML = Get-Content "$env:ChocolateyInstall\lib\$Name.$Version\$Name.$Version.nuspec"
$XML.package.metadata.id | Should -Be $Name
$XML.package.metadata.version | Should -Be $Version
}

It "Outputs a message showing that upgrading was successful" {
$Output.String | SHould -Match "Chocolatey upgraded 2/2 packages\."
}
}
}

0 comments on commit 486cd95

Please sign in to comment.