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

Commit 4cc6d80

Browse files
edyoungalerickson
authored andcommitted
Proposed fix for location issues
1 parent 5a51ee6 commit 4cc6d80

File tree

3 files changed

+59
-16
lines changed

3 files changed

+59
-16
lines changed

Tests/PSGetUpdateModule.Tests.ps1

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,31 +91,59 @@ Describe UpdateModuleFromAlternateRepo -Tags 'BVT' {
9191
PSGetTestUtils\Uninstall-Module ContosoClient
9292
}
9393

94-
It "Check situation" {
94+
It "Check that removing a slash from a repo doesn't break update" {
9595
$withSlash = "https://www.poshtestgallery.com/api/v2/"
9696
$noSlash = "https://www.poshtestgallery.com/api/v2"
97-
Write-Host (Get-PSRepository | Out-String)
97+
#Write-Host (Get-PSRepository | Out-String)
9898
(Get-PSRepository PSGallery).SourceLocation | Should Be $withSlash
9999

100100
Install-Module ContosoServer -RequiredVersion 1.0
101101
(Get-InstalledModule ContosoServer).RepositorySourceLocation | Should Be $withSlash
102-
Write-Host (Get-InstalledModule ContosoServer -AllVersions | Format-List | Out-String)
102+
#Write-Host (Get-InstalledModule ContosoServer -AllVersions | Format-List | Out-String)
103103

104104
# now update where PSGallery Source Location is
105105
Set-PSGallerySourceLocation -Location $noSlash
106-
Write-Host (Get-PSRepository | Out-String)
106+
#Write-Host (Get-PSRepository | Out-String)
107107
(Get-PSRepository PSGallery).SourceLocation | Should Be $noSlash
108108

109109
# reload powershellget to force-update cached repository info
110110
Import-Module PowerShellGet -Force
111111

112112
# now try and update module isntalled using other SourceLocation
113113
Update-Module ContosoServer -RequiredVersion 2.0 -ErrorAction Stop
114-
Write-Host (Get-InstalledModule ContosoServer -AllVersions | Format-List | Out-String)
114+
#Write-Host (Get-InstalledModule ContosoServer -AllVersions | Format-List | Out-String)
115115
(Get-InstalledModule ContosoServer).RepositorySourceLocation | Should Be $noSlash
116116

117117
(Get-InstalledModule ContosoServer).Version | Should Be 2.0
118+
}
119+
120+
It "Check that adding a slash to a repo doesn't break update" {
121+
$withSlash = "https://www.poshtestgallery.com/api/v2/"
122+
$noSlash = "https://www.poshtestgallery.com/api/v2"
123+
#Write-Host (Get-PSRepository | Out-String)
124+
125+
Set-PSGallerySourceLocation -Location $noSlash
126+
127+
(Get-PSRepository PSGallery).SourceLocation | Should Be $noSlash
128+
129+
Install-Module ContosoServer -RequiredVersion 1.0
130+
(Get-InstalledModule ContosoServer).RepositorySourceLocation | Should Be $noSlash
131+
#Write-Host (Get-InstalledModule ContosoServer -AllVersions | Format-List | Out-String)
118132

133+
# now update where PSGallery Source Location is
134+
Set-PSGallerySourceLocation -Location $withSlash
135+
#Write-Host (Get-PSRepository | Out-String)
136+
(Get-PSRepository PSGallery).SourceLocation | Should Be $withSlash
137+
138+
# reload powershellget to force-update cached repository info
139+
Import-Module PowerShellGet -Force
140+
141+
# now try and update module isntalled using other SourceLocation
142+
Update-Module ContosoServer -RequiredVersion 2.0 -ErrorAction Stop
143+
#Write-Host (Get-InstalledModule ContosoServer -AllVersions | Format-List | Out-String)
144+
(Get-InstalledModule ContosoServer).RepositorySourceLocation | Should Be $withSlash
145+
146+
(Get-InstalledModule ContosoServer).Version | Should Be 2.0
119147
}
120148
}
121149

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)