Skip to content

Commit 486cd95

Browse files
authored
Merge pull request chocolatey#2726 from AdmiringWorm/1956-Cant-update-pkg-due-to-chocolatey-coreextension-version-mismatch
(chocolatey#1956) Fix packages not being upgraded if SxS installations are available
2 parents 6f3fc15 + 3d5c4a5 commit 486cd95

File tree

3 files changed

+215
-5
lines changed

3 files changed

+215
-5
lines changed

src/chocolatey/infrastructure.app/nuget/ChocolateyLocalPackageRepository.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,42 @@ public ChocolateyLocalPackageRepository(IPackagePathResolver pathResolver, IFile
4848
{
4949
}
5050

51+
public bool IgnoreVersionedDirectories { get; set; }
52+
53+
public override IQueryable<IPackage> GetPackages()
54+
{
55+
var packages = base.GetPackages();
56+
57+
if (IgnoreVersionedDirectories)
58+
{
59+
packages = packages.Where(ExcludeVersionedDirectories).ToList().AsQueryable();
60+
}
61+
62+
return packages;
63+
}
64+
65+
public override IPackage FindPackage(string packageId, SemanticVersion version)
66+
{
67+
if (IgnoreVersionedDirectories)
68+
{
69+
return FindPackagesById(packageId).FirstOrDefault(package => package.Version >= version);
70+
}
71+
72+
return base.FindPackage(packageId, version);
73+
}
74+
75+
public override IEnumerable<IPackage> FindPackagesById(string packageId)
76+
{
77+
var packages = base.FindPackagesById(packageId);
78+
79+
if (IgnoreVersionedDirectories)
80+
{
81+
packages = packages.Where(ExcludeVersionedDirectories);
82+
}
83+
84+
return packages;
85+
}
86+
5187
public override void AddPackage(IPackage package)
5288
{
5389
string packageFilePath = GetPackageFilePath(package);
@@ -72,6 +108,19 @@ public override void AddPackage(IPackage package)
72108
}
73109
}
74110

111+
private bool ExcludeVersionedDirectories(IPackage package)
112+
{
113+
var directoryPath = PathResolver.GetInstallPath(package);
114+
if (string.IsNullOrWhiteSpace(directoryPath))
115+
{
116+
return true;
117+
}
118+
119+
var directoryName = Path.GetFileName(directoryPath);
120+
121+
return string.Compare(directoryName, package.Id + "." + package.Version, StringComparison.OrdinalIgnoreCase) != 0;
122+
}
123+
75124
private string GetManifestFilePath(string packageId, SemanticVersion version)
76125
{
77126
string packageDirectory = PathResolver.GetPackageDirectory(packageId, version);

src/chocolatey/infrastructure.app/services/NugetService.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,24 @@ namespace chocolatey.infrastructure.app.services
1919
using System;
2020
using System.Collections.Concurrent;
2121
using System.Collections.Generic;
22-
using System.Globalization;
2322
using System.IO;
2423
using System.Linq;
2524
using System.Net;
26-
using NuGet;
2725
using adapters;
26+
using chocolatey.infrastructure.app.utility;
2827
using commandline;
2928
using configuration;
3029
using domain;
3130
using guards;
3231
using logging;
3332
using nuget;
33+
using NuGet;
3434
using platforms;
3535
using results;
3636
using tolerance;
3737
using DateTime = adapters.DateTime;
3838
using Environment = System.Environment;
3939
using IFileSystem = filesystem.IFileSystem;
40-
using chocolatey.infrastructure.app.utility;
4140

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

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

620+
var localRepository = packageManager.LocalRepository as ChocolateyLocalPackageRepository;
621+
622+
if (localRepository != null)
623+
{
624+
localRepository.IgnoreVersionedDirectories = !config.AllowMultipleVersions;
625+
}
626+
621627
var configIgnoreDependencies = config.IgnoreDependencies;
622628
set_package_names_if_all_is_specified(config, () => { config.IgnoreDependencies = true; });
623629
config.IgnoreDependencies = configIgnoreDependencies;
@@ -927,7 +933,7 @@ public virtual ConcurrentDictionary<string, PackageResult> get_outdated(Chocolat
927933
config.Prerelease = true;
928934
}
929935

930-
SemanticVersion version = null;
936+
SemanticVersion version = null;
931937
var latestPackage = NugetList.find_package(packageName, null, config, packageManager.SourceRepository);
932938

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

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
Import-Module helpers/common-helpers
2+
3+
Describe "choco upgrade" -Tag Chocolatey, UpgradeCommand {
4+
BeforeAll {
5+
Initialize-ChocolateyTestInstall
6+
7+
New-ChocolateyInstallSnapshot
8+
}
9+
10+
AfterAll {
11+
Remove-ChocolateyTestInstall
12+
}
13+
14+
Context "Can upgrade packages with dependencies containing side by side installations and outdated dependency" {
15+
BeforeAll {
16+
Restore-ChocolateyInstallSnapshot
17+
18+
$null = Invoke-Choco install chocolatey-core.extension --version 1.3.0 --confirm
19+
$null = Invoke-Choco install 7zip --version 16.04 --confirm
20+
$null = Invoke-Choco install chocolatey-core.extension --version 1.3.5.1 --sxs --confirm
21+
22+
$Output = Invoke-Choco upgrade 7zip --version 21.7 --confirm
23+
}
24+
25+
AfterAll {
26+
$null = Invoke-Choco uninstall 7zip 7zip.install --confirm
27+
}
28+
29+
It "Exits with Success (0)" {
30+
$Output.ExitCode | Should -Be 0 -Because $Output.String
31+
}
32+
33+
It "Upgrades version <OldVersion> of the package <Name>" -ForEach @(
34+
@{ Name = "7zip"; OldVersion = "16.04" }
35+
@{ Name = "7zip.install"; OldVersion = "16.04" }
36+
@{ Name = "chocolatey-core.extension"; OldVersion = "1.3.0" }
37+
) {
38+
"$env:ChocolateyInstall\lib\$Name\$Name.nuspec" | Should -Exist
39+
[xml]$XML = Get-Content "$env:ChocolateyInstall\lib\$Name\$Name.nuspec"
40+
$XML.package.metadata.version | Should -Not -Be $OldVersion
41+
}
42+
43+
It "Have not upgraded side by side installation of <Name> v<Version>" -ForEach @(
44+
@{ Name = "chocolatey-core.extension"; Version = "1.3.5.1" }
45+
) {
46+
"$env:ChocolateyInstall\lib\$Name.$Version\$Name.$Version.nupkg" | Should -Exist
47+
[xml]$XML = Get-Content "$env:ChocolateyInstall\lib\$Name.$Version\$Name.$Version.nuspec"
48+
$XML.package.metadata.id | Should -Be $Name
49+
$XML.package.metadata.version | Should -Be $Version
50+
}
51+
52+
It "Outputs a message showing that upgrading was successful" {
53+
$Output.String | SHould -Match "Chocolatey upgraded 3/3 packages\."
54+
}
55+
}
56+
57+
Context "Can upgrade packages with dependencies containing side by side installations and up to date dependency" {
58+
BeforeAll {
59+
Restore-ChocolateyInstallSnapshot
60+
61+
$null = Invoke-Choco install chocolatey-core.extension --version 1.3.3 --confirm
62+
$null = Invoke-Choco install 7zip --version 16.04 --confirm
63+
$null = Invoke-Choco install chocolatey-core.extension --version 1.3.5.1 --sxs --confirm
64+
65+
$Output = Invoke-Choco upgrade 7zip --version 21.7 --confirm
66+
}
67+
68+
AfterAll {
69+
$null = Invoke-Choco uninstall 7zip 7zip.install --confirm
70+
}
71+
72+
It "Exits with Success (0)" {
73+
$Output.ExitCode | Should -Be 0 -Because $Output.String
74+
}
75+
76+
It "Upgrades version <OldVersion> of the package <Name>" -ForEach @(
77+
@{ Name = "7zip"; OldVersion = "16.04" }
78+
@{ Name = "7zip.install"; OldVersion = "16.04" }
79+
) {
80+
"$env:ChocolateyInstall\lib\$Name\$Name.nuspec" | Should -Exist
81+
[xml]$XML = Get-Content "$env:ChocolateyInstall\lib\$Name\$Name.nuspec"
82+
$XML.package.metadata.version | Should -Not -Be $OldVersion
83+
}
84+
85+
It "Have not upgraded dependency <Name>" {
86+
"$env:ChocolateyInstall\lib\chocolatey-core.extension\chocolatey-core.extension.nupkg" | Should -Exist
87+
[xml]$XML = Get-Content "$env:ChocolateyInstall\lib\chocolatey-core.extension\chocolatey-core.extension.nuspec"
88+
$XML.package.metadata.id | Should -Be 'chocolatey-core.extension'
89+
$XML.package.metadata.version | Should -Be '1.3.3'
90+
}
91+
92+
It "Have not upgraded side by side installation of <Name> v<Version>" -ForEach @(
93+
@{ Name = "chocolatey-core.extension"; Version = "1.3.5.1" }
94+
) {
95+
"$env:ChocolateyInstall\lib\$Name.$Version\$Name.$Version.nupkg" | Should -Exist
96+
[xml]$XML = Get-Content "$env:ChocolateyInstall\lib\$Name.$Version\$Name.$Version.nuspec"
97+
$XML.package.metadata.id | Should -Be $Name
98+
$XML.package.metadata.version | Should -Be $Version
99+
}
100+
101+
It "Outputs a message showing that upgrading was successful" {
102+
$Output.String | SHould -Match "Chocolatey upgraded 2/2 packages\."
103+
}
104+
}
105+
106+
107+
Context "Can upgrade packages with dependencies containing outdated side by side installations and up to date dependency" {
108+
BeforeAll {
109+
Restore-ChocolateyInstallSnapshot
110+
111+
$null = Invoke-Choco install chocolatey-core.extension --version 1.3.3 --confirm
112+
$null = Invoke-Choco install 7zip --version 16.04 --confirm
113+
$null = Invoke-Choco install chocolatey-core.extension --version 1.3.0 --sxs --confirm
114+
115+
$Output = Invoke-Choco upgrade 7zip --version 21.7 --confirm
116+
}
117+
118+
AfterAll {
119+
$null = Invoke-Choco uninstall 7zip 7zip.install --confirm
120+
}
121+
122+
It "Exits with Success (0)" {
123+
$Output.ExitCode | Should -Be 0 -Because $Output.String
124+
}
125+
126+
It "Upgrades version <OldVersion> of the package <Name>" -ForEach @(
127+
@{ Name = "7zip"; OldVersion = "16.04" }
128+
@{ Name = "7zip.install"; OldVersion = "16.04" }
129+
) {
130+
"$env:ChocolateyInstall\lib\$Name\$Name.nuspec" | Should -Exist
131+
[xml]$XML = Get-Content "$env:ChocolateyInstall\lib\$Name\$Name.nuspec"
132+
$XML.package.metadata.version | Should -Not -Be $OldVersion
133+
}
134+
135+
It "Have not upgraded dependency chocolatey-core.extension" {
136+
"$env:ChocolateyInstall\lib\chocolatey-core.extension\chocolatey-core.extension.nupkg" | Should -Exist
137+
[xml]$XML = Get-Content "$env:ChocolateyInstall\lib\chocolatey-core.extension\chocolatey-core.extension.nuspec"
138+
$XML.package.metadata.id | Should -Be 'chocolatey-core.extension'
139+
$XML.package.metadata.version | Should -Be '1.3.3'
140+
}
141+
142+
It "Have not upgraded side by side installation of <Name> v<Version>" -ForEach @(
143+
@{ Name = "chocolatey-core.extension"; Version = "1.3.0" }
144+
) {
145+
"$env:ChocolateyInstall\lib\$Name.$Version\$Name.$Version.nupkg" | Should -Exist
146+
[xml]$XML = Get-Content "$env:ChocolateyInstall\lib\$Name.$Version\$Name.$Version.nuspec"
147+
$XML.package.metadata.id | Should -Be $Name
148+
$XML.package.metadata.version | Should -Be $Version
149+
}
150+
151+
It "Outputs a message showing that upgrading was successful" {
152+
$Output.String | SHould -Match "Chocolatey upgraded 2/2 packages\."
153+
}
154+
}
155+
}

0 commit comments

Comments
 (0)