Skip to content

Commit d64b5fa

Browse files
committed
Bug fix for Update-PSResource not updating from correct repository (#1549)
1 parent 7c3f650 commit d64b5fa

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/code/UpdatePSResource.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,15 @@ private string[] ProcessPackageNames(
321321
latestInstalledIsPrerelease = true;
322322
}
323323

324+
// Update from the repository where the package was previously installed from.
325+
// If user explicitly specifies a repository to update from, use that instead.
326+
if (Repository == null)
327+
{
328+
Repository = new String[] { installedPackages.First().Value.Repository };
329+
330+
WriteDebug($"Updating from repository '{string.Join(", ", Repository)}");
331+
}
332+
324333
// Find all packages selected for updating in provided repositories.
325334
var repositoryPackages = new Dictionary<string, PSResourceInfo>(StringComparer.InvariantCultureIgnoreCase);
326335
foreach (var foundResource in _findHelper.FindByResourceName(

test/UpdatePSResourceTests/UpdatePSResourceLocalTests.ps1

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Describe 'Test Update-PSResource for local repositories' -tags 'CI' {
1010

1111
BeforeAll {
1212
$localRepo = "psgettestlocal"
13+
$localRepo2 = "psgettestlocal2"
1314
$moduleName = "test_local_mod"
1415
$moduleName2 = "test_local_mod2"
1516
Get-NewPSResourceRepositoryFile
@@ -20,6 +21,9 @@ Describe 'Test Update-PSResource for local repositories' -tags 'CI' {
2021
Get-ModuleResourcePublishedToLocalRepoTestDrive $moduleName $localRepo "5.0.0"
2122
Get-ModuleResourcePublishedToLocalRepoTestDrive $moduleName2 $localRepo "1.0.0"
2223
Get-ModuleResourcePublishedToLocalRepoTestDrive $moduleName2 $localRepo "5.0.0"
24+
25+
Get-ModuleResourcePublishedToLocalRepoTestDrive $moduleName $localRepo2 "1.0.0"
26+
Get-ModuleResourcePublishedToLocalRepoTestDrive $moduleName $localRepo2 "5.0.0"
2327
}
2428

2529
AfterEach {
@@ -48,6 +52,31 @@ Describe 'Test Update-PSResource for local repositories' -tags 'CI' {
4852
$isPkgUpdated | Should -Be $true
4953
}
5054

55+
It "Update resource from the repository which package was previously from" {
56+
Install-PSResource -Name $moduleName -Version "1.0.0" -Repository $localRepo2 -TrustRepository
57+
58+
Update-PSResource -Name $moduleName -TrustRepository
59+
$res = Get-InstalledPSResource -Name $moduleName
60+
61+
$isPkgUpdated = $false
62+
$isCorrectRepo = $false
63+
foreach ($pkg in $res)
64+
{
65+
if ([System.Version]$pkg.Version -gt [System.Version]"1.0.0")
66+
{
67+
$isPkgUpdated = $true
68+
69+
if ($pkg.Repository -eq $localRepo2)
70+
{
71+
$isCorrectRepo = $true
72+
}
73+
}
74+
}
75+
76+
$isPkgUpdated | Should -Be $true
77+
$isCorrectRepo | Should -Be $true
78+
}
79+
5180
It "Update resources installed given Name (with wildcard) parameter" {
5281
Install-PSResource -Name $moduleName -Version "1.0.0" -Repository $localRepo -TrustRepository
5382
Install-PSResource -Name $moduleName2 -Version "1.0.0" -Repository $localRepo -TrustRepository

0 commit comments

Comments
 (0)