Skip to content
This repository was archived by the owner on Jun 13, 2024. It is now read-only.

Commit 9e3b40f

Browse files
authored
Merge pull request #407 from PowerShell/edyoung/issue_349_catastrophic
Edyoung/issue 349 Resolve "catastrophic failure with update module"
2 parents 842f6ad + 54b6b0a commit 9e3b40f

File tree

3 files changed

+100
-17
lines changed

3 files changed

+100
-17
lines changed

Tests/PSGetUpdateModule.Tests.ps1

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,74 @@ function SuiteCleanup {
7777
}
7878
}
7979

80+
Describe UpdateModuleFromAlternateRepo -Tags 'BVT' {
81+
BeforeAll {
82+
SuiteSetup
83+
}
84+
85+
AfterAll {
86+
SuiteCleanup
87+
}
88+
89+
AfterEach {
90+
PSGetTestUtils\Uninstall-Module ContosoServer
91+
PSGetTestUtils\Uninstall-Module ContosoClient
92+
}
93+
94+
It "Check that removing a slash from a repo doesn't break update" {
95+
$withSlash = "https://www.poshtestgallery.com/api/v2/"
96+
$noSlash = "https://www.poshtestgallery.com/api/v2"
97+
#Write-Host (Get-PSRepository | Out-String)
98+
(Get-PSRepository PSGallery).SourceLocation | Should Be $withSlash
99+
100+
Install-Module ContosoServer -RequiredVersion 1.0
101+
(Get-InstalledModule ContosoServer).RepositorySourceLocation | Should Be $withSlash
102+
#Write-Host (Get-InstalledModule ContosoServer -AllVersions | Format-List | Out-String)
103+
104+
# now update where PSGallery Source Location is
105+
Set-PSGallerySourceLocation -Location $noSlash
106+
#Write-Host (Get-PSRepository | Out-String)
107+
(Get-PSRepository PSGallery).SourceLocation | Should Be $noSlash
108+
109+
# reload powershellget to force-update cached repository info
110+
Import-Module PowerShellGet -Force
111+
112+
# now try and update module isntalled using other SourceLocation
113+
Update-Module ContosoServer -RequiredVersion 2.0 -ErrorAction Stop
114+
#Write-Host (Get-InstalledModule ContosoServer -AllVersions | Format-List | Out-String)
115+
(Get-InstalledModule ContosoServer).RepositorySourceLocation | Should Be $noSlash
116+
(Get-InstalledModule ContosoServer).Version | Should Be 2.0
117+
}
118+
119+
It "Check that adding a slash to a repo doesn't break update" {
120+
$withSlash = "https://www.poshtestgallery.com/api/v2/"
121+
$noSlash = "https://www.poshtestgallery.com/api/v2"
122+
#Write-Host (Get-PSRepository | Out-String)
123+
124+
Set-PSGallerySourceLocation -Location $noSlash
125+
126+
(Get-PSRepository PSGallery).SourceLocation | Should Be $noSlash
127+
128+
Install-Module ContosoServer -RequiredVersion 1.0
129+
(Get-InstalledModule ContosoServer).RepositorySourceLocation | Should Be $noSlash
130+
#Write-Host (Get-InstalledModule ContosoServer -AllVersions | Format-List | Out-String)
131+
132+
# now update where PSGallery Source Location is
133+
Set-PSGallerySourceLocation -Location $withSlash
134+
#Write-Host (Get-PSRepository | Out-String)
135+
(Get-PSRepository PSGallery).SourceLocation | Should Be $withSlash
136+
137+
# reload powershellget to force-update cached repository info
138+
Import-Module PowerShellGet -Force
139+
140+
# now try and update module isntalled using other SourceLocation
141+
Update-Module ContosoServer -RequiredVersion 2.0 -ErrorAction Stop
142+
#Write-Host (Get-InstalledModule ContosoServer -AllVersions | Format-List | Out-String)
143+
(Get-InstalledModule ContosoServer).RepositorySourceLocation | Should Be $withSlash
144+
(Get-InstalledModule ContosoServer).Version | Should Be 2.0
145+
}
146+
}
147+
80148
Describe PowerShell.PSGet.UpdateModuleTests -Tags 'BVT','InnerLoop' {
81149

82150
BeforeAll {
@@ -350,7 +418,7 @@ Describe PowerShell.PSGet.UpdateModuleTests -Tags 'BVT','InnerLoop' {
350418
}
351419

352420
Describe PowerShell.PSGet.UpdateModuleTests.P1 -Tags 'P1','OuterLoop' {
353-
# Not executing these tests on MacOS as
421+
# Not executing these tests on MacOS as
354422
# the total execution time is exceeding allowed 50 min in TravisCI daily builds.
355423
if($IsMacOS) {
356424
return
@@ -376,13 +444,13 @@ Describe PowerShell.PSGet.UpdateModuleTests.P1 -Tags 'P1','OuterLoop' {
376444
# Expected Result: both modules should be refreshed
377445
#
378446
It "UpdateMultipleModulesWithWildcard" {
379-
447+
380448
Install-Module ContosoClient -RequiredVersion 1.0
381-
449+
382450
$contosoClientDetails = Get-InstalledModule -Name ContosoClient
383451

384452
Install-Module ContosoServer -RequiredVersion 1.0
385-
453+
386454
$MyError = $null
387455
$DateTimeBeforeUpdate = Get-Date
388456

@@ -391,7 +459,7 @@ Describe PowerShell.PSGet.UpdateModuleTests.P1 -Tags 'P1','OuterLoop' {
391459
Assert ($MyError.Count -eq 0) "There should not be any error when updating multiple modules with wildcard in name, $MyError"
392460
$res = Get-InstalledModule -Name ContosoServer -MinimumVersion "1.1"
393461
Assert ($res -and ($res.Name -eq "ContosoServer") -and ($res.Version -gt [Version]"1.0")) "Update-Module should update when wildcard specified in name"
394-
462+
395463
$res = Get-InstalledModule -Name ContosoClient -MinimumVersion "1.1"
396464
Assert ($res -and ($res.Name -eq "ContosoClient") -and ($res.Version -gt [Version]"1.0")) "Update-Module should update when wildcard specified in name"
397465

@@ -457,7 +525,7 @@ Describe PowerShell.PSGet.UpdateModuleTests.P1 -Tags 'P1','OuterLoop' {
457525

458526
Describe PowerShell.PSGet.UpdateModuleTests.P2 -Tags 'P2','OuterLoop' {
459527

460-
# Not executing these tests on MacOS as
528+
# Not executing these tests on MacOS as
461529
# the total execution time is exceeding allowed 50 min in TravisCI daily builds.
462530
if($IsMacOS) {
463531
return
Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
1-
function Get-SourceName
2-
{
1+
function Get-SourceName {
32
[CmdletBinding()]
43
[OutputType("string")]
54
Param
65
(
7-
[Parameter(Mandatory=$true)]
6+
[Parameter(Mandatory = $true)]
87
[ValidateNotNullOrEmpty()]
98
[string]
109
$Location
1110
)
1211

1312
Set-ModuleSourcesVariable
1413

15-
foreach($psModuleSource in $script:PSGetModuleSources.Values)
16-
{
17-
if(($psModuleSource.Name -eq $Location) -or
18-
($psModuleSource.SourceLocation -eq $Location) -or
19-
((Get-Member -InputObject $psModuleSource -Name $script:ScriptSourceLocation) -and
20-
($psModuleSource.ScriptSourceLocation -eq $Location)))
21-
{
14+
foreach ($psModuleSource in $script:PSGetModuleSources.Values) {
15+
if (($psModuleSource.Name -eq $Location) -or
16+
(Test-EquivalentLocation -LocationA $psModuleSource.SourceLocation -LocationB $Location) -or
17+
((Get-Member -InputObject $psModuleSource -Name $script:ScriptSourceLocation) -and
18+
(Test-EquivalentLocation -LocationA $psModuleSource.ScriptSourceLocation -LocationB $Location))) {
2219
return $psModuleSource.Name
2320
}
2421
}
25-
}
22+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
# Compare 2 strings, ignoring any trailing slashes or backslashes.
3+
# This is not exactly the same as URL or path equivalence but it should work in practice
4+
function Test-EquivalentLocation {
5+
[CmdletBinding()]
6+
[OutputType("bool")]
7+
param(
8+
[Parameter(Mandatory = $false)]
9+
[string]$LocationA,
10+
11+
[Parameter(Mandatory = $false)]
12+
[string]$LocationB
13+
)
14+
15+
$LocationA = $LocationA.TrimEnd("\/")
16+
$LocationB = $LocationB.TrimEnd("\/")
17+
return $LocationA -eq $LocationB
18+
}

0 commit comments

Comments
 (0)