Skip to content
8 changes: 8 additions & 0 deletions src/code/InstallHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,14 @@ private Hashtable BeginPackageInstall(
pkgVersion += $"-{pkgToInstall.Prerelease}";
}
}

// For most repositories/providers the server will use the normalized version, which pkgVersion originally reflects
// However, for container registries the version must exactly match what was in the artifact manifest and then reflected in PSResourceInfo.Version.ToString()
if (currentServer.Repository.ApiVersion == PSRepositoryInfo.APIVersion.ContainerRegistry)
{
pkgVersion = String.IsNullOrEmpty(pkgToInstall.Prerelease) ? pkgToInstall.Version.ToString() : $"{pkgToInstall.Version.ToString()}-{pkgToInstall.Prerelease}";
}

// Check to see if the pkg is already installed (ie the pkg is installed and the version satisfies the version range provided via param)
if (!_reinstall)
{
Expand Down
9 changes: 6 additions & 3 deletions src/code/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,11 @@ public static string GetNormalizedVersionString(
string versionString,
string prerelease)
{
// versionString may be like 1.2.0.0 or 1.2.0
// versionString may be like 1.2.0.0 or 1.2.0 or 1.2
// prerelease may be null or "alpha1"
// possible passed in examples:
// versionString: "1.2" <- container registry 2 digit version
// versionString: "1.2" prerelease: "alpha1" <- container registry 2 digit version
// versionString: "1.2.0" prerelease: "alpha1"
// versionString: "1.2.0" prerelease: "" <- doubtful though
// versionString: "1.2.0.0" prerelease: "alpha1"
Expand All @@ -331,9 +333,10 @@ public static string GetNormalizedVersionString(

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

if (numVersionDigits == 3)
if (numVersionDigits == 2 || numVersionDigits == 3)
{
// versionString: "1.2.0" prerelease: "alpha1"
// versionString: "1.2.0" prerelease: "alpha1" -> 1.2.0-alpha1
// versionString: "1.2" prerelease: "alpha1" -> 1.2-alpha1
return versionString + "-" + prerelease;
}
else if (numVersionDigits == 4)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Describe 'Test HTTP Find-PSResource for ACR Server Protocol' -tags 'CI' {

BeforeAll{
$testModuleName = "test-module"
$testModuleWith2DigitVersion = "test-2DigitPkg"
$testModuleParentName = "test_parent_mod"
$testModuleDependencyName = "test_dependency_mod"
$testScriptName = "test-script"
Expand Down Expand Up @@ -82,6 +83,25 @@ Describe 'Test HTTP Find-PSResource for ACR Server Protocol' -tags 'CI' {
$res.Count | Should -BeGreaterOrEqual 1
}

It "Find resource when version contains different number of digits than the normalized version" {
# the resource has version "1.0", but querying with any equivalent version should work
$res1DigitVersion = Find-PSResource -Name $testModuleWith2DigitVersion -Version "1" -Repository $ACRRepoName
$res1DigitVersion | Should -Not -BeNullOrEmpty
$res1DigitVersion.Version | Should -Be "1.0"

$res2DigitVersion = Find-PSResource -Name $testModuleWith2DigitVersion -Version "1.0" -Repository $ACRRepoName
$res2DigitVersion | Should -Not -BeNullOrEmpty
$res2DigitVersion.Version | Should -Be "1.0"

$res3DigitVersion = Find-PSResource -Name $testModuleWith2DigitVersion -Version "1.0.0" -Repository $ACRRepoName
$res3DigitVersion | Should -Not -BeNullOrEmpty
$res3DigitVersion.Version | Should -Be "1.0"

$res4DigitVersion = Find-PSResource -Name $testModuleWith2DigitVersion -Version "1.0.0.0" -Repository $ACRRepoName
$res4DigitVersion | Should -Not -BeNullOrEmpty
$res4DigitVersion.Version | Should -Be "1.0"
}

It "Find module and dependencies when -IncludeDependencies is specified" {
$res = Find-PSResource -Name $testModuleParentName -Repository $ACRRepoName -IncludeDependencies
$res | Should -Not -BeNullOrEmpty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Describe 'Test Install-PSResource for ACR scenarios' -tags 'CI' {
BeforeAll {
$testModuleName = "test-module"
$testModuleName2 = "test-module2"
$testModuleWith2DigitVersion = "test-2DigitPkg"
$testCamelCaseModuleName = "test-camelCaseModule"
$testCamelCaseScriptName = "test-camelCaseScript"
$testModuleParentName = "test_parent_mod"
Expand All @@ -33,7 +34,7 @@ Describe 'Test Install-PSResource for ACR scenarios' -tags 'CI' {
}

AfterEach {
Uninstall-PSResource $testModuleName, $testModuleName2, $testCamelCaseModuleName, $testScriptName, $testCamelCaseScriptName -Version "*" -SkipDependencyCheck -ErrorAction SilentlyContinue
Uninstall-PSResource $testModuleName, $testModuleName2, $testCamelCaseModuleName, $testScriptName, $testCamelCaseScriptName, $testModuleWith2DigitVersion -Version "*" -SkipDependencyCheck -ErrorAction SilentlyContinue
}

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

It "Install resource when version contains different number of digits than the normalized version- 1 digit specified" {
# the resource has version "1.0", but querying with any equivalent version should work
Install-PSResource -Name $testModuleWith2DigitVersion -Version "1" -Repository $ACRRepoName -TrustRepository
$res = Get-InstalledPSResource -Name $testModuleWith2DigitVersion
$res | Should -Not -BeNullOrEmpty
$res.Version | Should -Be "1.0"
}

It "Install resource when version contains different number of digits than the normalized version- 2 digits specified" {
# the resource has version "1.0", but querying with any equivalent version should work
Install-PSResource -Name $testModuleWith2DigitVersion -Version "1.0" -Repository $ACRRepoName -TrustRepository
$res = Get-InstalledPSResource -Name $testModuleWith2DigitVersion
$res | Should -Not -BeNullOrEmpty
$res.Version | Should -Be "1.0"
}

It "Install resource when version contains different number of digits than the normalized version- 3 digits specified" {
# the resource has version "1.0", but querying with any equivalent version should work
Install-PSResource -Name $testModuleWith2DigitVersion -Version "1.0.0" -Repository $ACRRepoName -TrustRepository
$res = Get-InstalledPSResource -Name $testModuleWith2DigitVersion
$res | Should -Not -BeNullOrEmpty
$res.Version | Should -Be "1.0"
}

It "Install resource when version contains different number of digits than the normalized version- 4 digits specified" {
# the resource has version "1.0", but querying with any equivalent version should work
Install-PSResource -Name $testModuleWith2DigitVersion -Version "1.0.0.0" -Repository $ACRRepoName -TrustRepository
$res = Get-InstalledPSResource -Name $testModuleWith2DigitVersion
$res | Should -Not -BeNullOrEmpty
$res.Version | Should -Be "1.0"
}

It "Install resource where version specified is a prerelease version" {
# the resource has version "1.0", but querying with any equivalent version should work
Install-PSResource -Name $testModuleWith2DigitVersion -Version "1.5-alpha" -Prerelease -Repository $ACRRepoName -TrustRepository
$res = Get-InstalledPSResource -Name $testModuleWith2DigitVersion
$res | Should -Not -BeNullOrEmpty
$res.Version | Should -Be "1.5"
$res.Prerelease | Should -Be "alpha"
}

It "Install multiple resources by name" {
$pkgNames = @($testModuleName, $testModuleName2)
Install-PSResource -Name $pkgNames -Repository $ACRRepoName -TrustRepository
Expand Down
Loading