Skip to content

Bug fix for Update-PSResource not updating from correct repository #1549

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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