Skip to content

Commit c534f15

Browse files
authored
Use version comparison function rather than string compare in PS (microsoft#5323)
## Change Swap the version finding code in the PowerShell module from using a direct string comparison to using the `CompareToVersion` function. This enables non-exact version matching, such as "1 == 1.0.0".
1 parent 7016c56 commit c534f15

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/PowerShell/Microsoft.WinGet.Client.Engine/Commands/Common/PackageCommand.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,9 @@ private CatalogPackage GetCatalogPackage(CompositeSearchBehavior behavior, Packa
128128
{
129129
for (var i = 0; i < package.AvailableVersions.Count; i++)
130130
{
131-
if (package.AvailableVersions[i].Version.CompareTo(this.Version) == 0)
131+
PackageVersionInfo versionInfo = package.GetPackageVersionInfo(package.AvailableVersions[i]);
132+
133+
if (versionInfo != null && versionInfo.CompareToVersion(this.Version) == CompareResult.Equal)
132134
{
133135
return package.AvailableVersions[i];
134136
}

src/PowerShell/tests/Microsoft.WinGet.Client.Tests.ps1

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,21 @@ Describe 'Export-WinGetPackage' {
600600
Test-Path -Path $testDirectory | Should -Be $false
601601
}
602602

603+
It 'Download with short Version' {
604+
$testDirectory = GetRandomTestDirectory
605+
$result = Export-WinGetPackage -Id AppInstallerTest.TestExeInstaller -Version '1' -DownloadDirectory $testDirectory
606+
607+
$result | Should -Not -BeNullOrEmpty
608+
$result.Id | Should -Be "AppInstallerTest.TestExeInstaller"
609+
$result.Name | Should -Be "TestExeInstaller"
610+
$result.Source | Should -Be "TestSource"
611+
$result.Status | Should -Be 'Ok'
612+
613+
# Download directory should be created and have exactly two files (installer and manifest file).
614+
Test-Path -Path $testDirectory | Should -Be $true
615+
(Get-ChildItem -Path $testDirectory -Force | Measure-Object).Count | Should -Be 2
616+
}
617+
603618
AfterEach {
604619
if (Test-Path $testDirectory) {
605620
Remove-Item $testDirectory -Force -Recurse

0 commit comments

Comments
 (0)