Skip to content

Commit 6915968

Browse files
authored
Merge pull request #1796 from PowerShell/bugfix-containerregistry-2digitinstall
Bugfix for ContainerRegistry repository Install-PSResource with varying digit version
2 parents 3ab7f01 + fa0880b commit 6915968

File tree

4 files changed

+77
-4
lines changed

4 files changed

+77
-4
lines changed

src/code/InstallHelper.cs

+8
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,14 @@ private Hashtable BeginPackageInstall(
824824
pkgVersion += $"-{pkgToInstall.Prerelease}";
825825
}
826826
}
827+
828+
// For most repositories/providers the server will use the normalized version, which pkgVersion originally reflects
829+
// However, for container registries the version must exactly match what was in the artifact manifest and then reflected in PSResourceInfo.Version.ToString()
830+
if (currentServer.Repository.ApiVersion == PSRepositoryInfo.APIVersion.ContainerRegistry)
831+
{
832+
pkgVersion = String.IsNullOrEmpty(pkgToInstall.Prerelease) ? pkgToInstall.Version.ToString() : $"{pkgToInstall.Version.ToString()}-{pkgToInstall.Prerelease}";
833+
}
834+
827835
// Check to see if the pkg is already installed (ie the pkg is installed and the version satisfies the version range provided via param)
828836
if (!_reinstall)
829837
{

src/code/Utils.cs

+6-3
Original file line numberDiff line numberDiff line change
@@ -316,9 +316,11 @@ public static string GetNormalizedVersionString(
316316
string versionString,
317317
string prerelease)
318318
{
319-
// versionString may be like 1.2.0.0 or 1.2.0
319+
// versionString may be like 1.2.0.0 or 1.2.0 or 1.2
320320
// prerelease may be null or "alpha1"
321321
// possible passed in examples:
322+
// versionString: "1.2" <- container registry 2 digit version
323+
// versionString: "1.2" prerelease: "alpha1" <- container registry 2 digit version
322324
// versionString: "1.2.0" prerelease: "alpha1"
323325
// versionString: "1.2.0" prerelease: "" <- doubtful though
324326
// versionString: "1.2.0.0" prerelease: "alpha1"
@@ -331,9 +333,10 @@ public static string GetNormalizedVersionString(
331333

332334
int numVersionDigits = versionString.Split('.').Count();
333335

334-
if (numVersionDigits == 3)
336+
if (numVersionDigits == 2 || numVersionDigits == 3)
335337
{
336-
// versionString: "1.2.0" prerelease: "alpha1"
338+
// versionString: "1.2.0" prerelease: "alpha1" -> 1.2.0-alpha1
339+
// versionString: "1.2" prerelease: "alpha1" -> 1.2-alpha1
337340
return versionString + "-" + prerelease;
338341
}
339342
else if (numVersionDigits == 4)

test/FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1

+20
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Describe 'Test HTTP Find-PSResource for ACR Server Protocol' -tags 'CI' {
88

99
BeforeAll{
1010
$testModuleName = "test-module"
11+
$testModuleWith2DigitVersion = "test-2DigitPkg"
1112
$testModuleParentName = "test_parent_mod"
1213
$testModuleDependencyName = "test_dependency_mod"
1314
$testScriptName = "test-script"
@@ -82,6 +83,25 @@ Describe 'Test HTTP Find-PSResource for ACR Server Protocol' -tags 'CI' {
8283
$res.Count | Should -BeGreaterOrEqual 1
8384
}
8485

86+
It "Find resource when version contains different number of digits than the normalized version" {
87+
# the resource has version "1.0", but querying with any equivalent version should work
88+
$res1DigitVersion = Find-PSResource -Name $testModuleWith2DigitVersion -Version "1" -Repository $ACRRepoName
89+
$res1DigitVersion | Should -Not -BeNullOrEmpty
90+
$res1DigitVersion.Version | Should -Be "1.0"
91+
92+
$res2DigitVersion = Find-PSResource -Name $testModuleWith2DigitVersion -Version "1.0" -Repository $ACRRepoName
93+
$res2DigitVersion | Should -Not -BeNullOrEmpty
94+
$res2DigitVersion.Version | Should -Be "1.0"
95+
96+
$res3DigitVersion = Find-PSResource -Name $testModuleWith2DigitVersion -Version "1.0.0" -Repository $ACRRepoName
97+
$res3DigitVersion | Should -Not -BeNullOrEmpty
98+
$res3DigitVersion.Version | Should -Be "1.0"
99+
100+
$res4DigitVersion = Find-PSResource -Name $testModuleWith2DigitVersion -Version "1.0.0.0" -Repository $ACRRepoName
101+
$res4DigitVersion | Should -Not -BeNullOrEmpty
102+
$res4DigitVersion.Version | Should -Be "1.0"
103+
}
104+
85105
It "Find module and dependencies when -IncludeDependencies is specified" {
86106
$res = Find-PSResource -Name $testModuleParentName -Repository $ACRRepoName -IncludeDependencies
87107
$res | Should -Not -BeNullOrEmpty

test/InstallPSResourceTests/InstallPSResourceContainerRegistryServer.Tests.ps1

+43-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Describe 'Test Install-PSResource for ACR scenarios' -tags 'CI' {
1010
BeforeAll {
1111
$testModuleName = "test-module"
1212
$testModuleName2 = "test-module2"
13+
$testModuleWith2DigitVersion = "test-2DigitPkg"
1314
$testCamelCaseModuleName = "test-camelCaseModule"
1415
$testCamelCaseScriptName = "test-camelCaseScript"
1516
$testModuleParentName = "test_parent_mod"
@@ -33,7 +34,7 @@ Describe 'Test Install-PSResource for ACR scenarios' -tags 'CI' {
3334
}
3435

3536
AfterEach {
36-
Uninstall-PSResource $testModuleName, $testModuleName2, $testCamelCaseModuleName, $testScriptName, $testCamelCaseScriptName -Version "*" -SkipDependencyCheck -ErrorAction SilentlyContinue
37+
Uninstall-PSResource $testModuleName, $testModuleName2, $testCamelCaseModuleName, $testScriptName, $testCamelCaseScriptName, $testModuleWith2DigitVersion -Version "*" -SkipDependencyCheck -ErrorAction SilentlyContinue
3738
}
3839

3940
AfterAll {
@@ -75,6 +76,47 @@ Describe 'Test Install-PSResource for ACR scenarios' -tags 'CI' {
7576
$pkg.Version | Should -BeExactly "1.0.0"
7677
}
7778

79+
It "Install resource when version contains different number of digits than the normalized version- 1 digit specified" {
80+
# the resource has version "1.0", but querying with any equivalent version should work
81+
Install-PSResource -Name $testModuleWith2DigitVersion -Version "1" -Repository $ACRRepoName -TrustRepository
82+
$res = Get-InstalledPSResource -Name $testModuleWith2DigitVersion
83+
$res | Should -Not -BeNullOrEmpty
84+
$res.Version | Should -Be "1.0"
85+
}
86+
87+
It "Install resource when version contains different number of digits than the normalized version- 2 digits specified" {
88+
# the resource has version "1.0", but querying with any equivalent version should work
89+
Install-PSResource -Name $testModuleWith2DigitVersion -Version "1.0" -Repository $ACRRepoName -TrustRepository
90+
$res = Get-InstalledPSResource -Name $testModuleWith2DigitVersion
91+
$res | Should -Not -BeNullOrEmpty
92+
$res.Version | Should -Be "1.0"
93+
}
94+
95+
It "Install resource when version contains different number of digits than the normalized version- 3 digits specified" {
96+
# the resource has version "1.0", but querying with any equivalent version should work
97+
Install-PSResource -Name $testModuleWith2DigitVersion -Version "1.0.0" -Repository $ACRRepoName -TrustRepository
98+
$res = Get-InstalledPSResource -Name $testModuleWith2DigitVersion
99+
$res | Should -Not -BeNullOrEmpty
100+
$res.Version | Should -Be "1.0"
101+
}
102+
103+
It "Install resource when version contains different number of digits than the normalized version- 4 digits specified" {
104+
# the resource has version "1.0", but querying with any equivalent version should work
105+
Install-PSResource -Name $testModuleWith2DigitVersion -Version "1.0.0.0" -Repository $ACRRepoName -TrustRepository
106+
$res = Get-InstalledPSResource -Name $testModuleWith2DigitVersion
107+
$res | Should -Not -BeNullOrEmpty
108+
$res.Version | Should -Be "1.0"
109+
}
110+
111+
It "Install resource where version specified is a prerelease version" {
112+
# the resource has version "1.0", but querying with any equivalent version should work
113+
Install-PSResource -Name $testModuleWith2DigitVersion -Version "1.5-alpha" -Prerelease -Repository $ACRRepoName -TrustRepository
114+
$res = Get-InstalledPSResource -Name $testModuleWith2DigitVersion
115+
$res | Should -Not -BeNullOrEmpty
116+
$res.Version | Should -Be "1.5"
117+
$res.Prerelease | Should -Be "alpha"
118+
}
119+
78120
It "Install multiple resources by name" {
79121
$pkgNames = @($testModuleName, $testModuleName2)
80122
Install-PSResource -Name $pkgNames -Repository $ACRRepoName -TrustRepository

0 commit comments

Comments
 (0)