Skip to content

Commit

Permalink
Bug fix for Update-PSResource not updating from correct repository (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
alerickson authored Jan 30, 2024
1 parent 109b902 commit f82d340
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/code/UpdatePSResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,15 @@ private string[] ProcessPackageNames(
latestInstalledIsPrerelease = true;
}

// Update from the repository where the package was previously installed from.
// If user explicitly specifies a repository to update from, use that instead.
if (Repository == null)
{
Repository = new String[] { installedPackages.First().Value.Repository };

WriteDebug($"Updating from repository '{string.Join(", ", Repository)}");
}

// Find all packages selected for updating in provided repositories.
var repositoryPackages = new Dictionary<string, PSResourceInfo>(StringComparer.InvariantCultureIgnoreCase);
foreach (var foundResource in _findHelper.FindByResourceName(
Expand Down
29 changes: 29 additions & 0 deletions test/UpdatePSResourceTests/UpdatePSResourceLocalTests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Describe 'Test Update-PSResource for local repositories' -tags 'CI' {

BeforeAll {
$localRepo = "psgettestlocal"
$localRepo2 = "psgettestlocal2"
$moduleName = "test_local_mod"
$moduleName2 = "test_local_mod2"
Get-NewPSResourceRepositoryFile
Expand All @@ -20,6 +21,9 @@ Describe 'Test Update-PSResource for local repositories' -tags 'CI' {
Get-ModuleResourcePublishedToLocalRepoTestDrive $moduleName $localRepo "5.0.0"
Get-ModuleResourcePublishedToLocalRepoTestDrive $moduleName2 $localRepo "1.0.0"
Get-ModuleResourcePublishedToLocalRepoTestDrive $moduleName2 $localRepo "5.0.0"

Get-ModuleResourcePublishedToLocalRepoTestDrive $moduleName $localRepo2 "1.0.0"
Get-ModuleResourcePublishedToLocalRepoTestDrive $moduleName $localRepo2 "5.0.0"
}

AfterEach {
Expand Down Expand Up @@ -48,6 +52,31 @@ Describe 'Test Update-PSResource for local repositories' -tags 'CI' {
$isPkgUpdated | Should -Be $true
}

It "Update resource from the repository which package was previously from" {
Install-PSResource -Name $moduleName -Version "1.0.0" -Repository $localRepo2 -TrustRepository

Update-PSResource -Name $moduleName -TrustRepository
$res = Get-InstalledPSResource -Name $moduleName

$isPkgUpdated = $false
$isCorrectRepo = $false
foreach ($pkg in $res)
{
if ([System.Version]$pkg.Version -gt [System.Version]"1.0.0")
{
$isPkgUpdated = $true

if ($pkg.Repository -eq $localRepo2)
{
$isCorrectRepo = $true
}
}
}

$isPkgUpdated | Should -Be $true
$isCorrectRepo | Should -Be $true
}

It "Update resources installed given Name (with wildcard) parameter" {
Install-PSResource -Name $moduleName -Version "1.0.0" -Repository $localRepo -TrustRepository
Install-PSResource -Name $moduleName2 -Version "1.0.0" -Repository $localRepo -TrustRepository
Expand Down

0 comments on commit f82d340

Please sign in to comment.