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

Commit 60ffbc9

Browse files
authored
Merge pull request #352 from PowerShell/unslashify
Warn when a repo is unreachable, don't produce a confusing error
2 parents 3e08d6d + a9e5c66 commit 60ffbc9

16 files changed

+138
-69
lines changed

Tests/PSGetPublishScript.Tests.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,7 +1080,7 @@ Describe PowerShell.PSGet.PublishScriptTests -Tags 'BVT','InnerLoop' {
10801080
finally {
10811081
Install-NuGetBinaries
10821082
}
1083-
} -Skip:$($PSEdition -eq 'Core')
1083+
} -Skip:$($PSEdition -eq 'Core' -or $PSVersionTable.Version -lt '5.0.0')
10841084

10851085
# Purpose: Validate that Publish-Script prompts to install NuGet.exe if NuGet.exe file is not found
10861086
#
@@ -1138,7 +1138,7 @@ Describe PowerShell.PSGet.PublishScriptTests -Tags 'BVT','InnerLoop' {
11381138
finally {
11391139
Install-NuGetBinaries
11401140
}
1141-
} -Skip:$($PSEdition -eq 'Core')
1141+
} -Skip:$($PSEdition -eq 'Core' -or $PSVersionTable.Version -lt '5.0.0')
11421142

11431143
# Purpose: Validate that Publish-Module prompts to upgrade NuGet.exe if local NuGet.exe file is less than minimum required version
11441144
#
@@ -1204,7 +1204,7 @@ Describe PowerShell.PSGet.PublishScriptTests -Tags 'BVT','InnerLoop' {
12041204
finally {
12051205
Install-NuGetBinaries
12061206
}
1207-
} -Skip:$($PSEdition -eq 'Core')
1207+
} -Skip:$($PSEdition -eq 'Core' -or $PSVersionTable.Version -lt '5.0.0')
12081208

12091209
# Purpose: Validate that Publish-Script prompts to install NuGet.exe if file not found
12101210
#
@@ -1262,7 +1262,7 @@ Describe PowerShell.PSGet.PublishScriptTests -Tags 'BVT','InnerLoop' {
12621262
finally {
12631263
Install-NuGetBinaries
12641264
}
1265-
} -Skip:$($PSEdition -eq 'Core')
1265+
} -Skip:$($PSEdition -eq 'Core' -or $PSVersionTable.Version -lt '5.0.0')
12661266
}
12671267

12681268
Describe PowerShell.PSGet.PublishScriptTests.P1 -Tags 'P1','OuterLoop' {

Tests/PSGetTestUtils.psm1

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,18 @@ function GetAndSet-PSGetTestGalleryDetails
149149
{
150150
$SourceUri = 'http://localhost:8765/api/v2/'
151151
$psgetModule = Import-Module -Name PowerShellGet -PassThru -Scope Local
152-
$ResolvedLocalSource = & $psgetModule Resolve-Location -Location $SourceUri -LocationParameterName 'SourceLocation'
153152

154-
if($ResolvedLocalSource -and
153+
# see if the localhost repo used in CI is present
154+
$pingSuccess = $false
155+
try
156+
{
157+
Invoke-WebRequest -Uri $SourceUri
158+
$pingSuccess = $true
159+
} catch {
160+
161+
}
162+
163+
if($pingSuccess -and
155164
$PSVersionTable.PSVersion -ge '5.0.0' -and
156165
[System.Environment]::OSVersion.Version -ge "6.2.9200.0" -and
157166
$PSCulture -eq 'en-US')

Tests/Pester.PSRepository.Tests.ps1

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,4 +225,89 @@ Describe 'Test Set-PSRepository and Set-PackageSource for PSGallery repository'
225225
$ev[0].FullyQualifiedErrorId | Should be 'ParameterIsNotAllowedWithPSGallery,Add-PackageSource,Microsoft.PowerShell.PackageManagement.Cmdlets.SetPackageSource'
226226
} `
227227
-Skip:$($PSVersionTable.PSVersion -lt '5.0.0')
228+
}
229+
230+
Describe 'Managing repositories' -Tag BVT {
231+
BeforeAll {
232+
Install-NuGetBinaries
233+
}
234+
235+
AfterAll {
236+
237+
}
238+
239+
BeforeEach {
240+
Unregister-PSRepository NewRepo -ErrorAction SilentlyContinue
241+
Unregister-PSRepository NewRepoSlash -ErrorAction SilentlyContinue
242+
}
243+
244+
AfterEach {
245+
Unregister-PSRepository NewRepo -ErrorAction SilentlyContinue
246+
Unregister-PSRepository NewRepoSlash -ErrorAction SilentlyContinue
247+
}
248+
249+
It "Should let you register an unreachable repository but produce a warning" {
250+
# microsoft.com exists but doesn't host a nuget repo
251+
Register-PSRepository -Name NewRepo -SourceLocation "https://microsoft.com/api/v2" -WarningVariable warning -WarningAction SilentlyContinue
252+
if($psversiontable.PSVersion -ge '5.0.0') { # -warningvariable doesn't seem to work in ps4?
253+
$warning -join "" | Should BeLike "*Unable to reach URL*"
254+
}
255+
}
256+
257+
It "Should let you change a repo to an unreachable location but produce a warning" {
258+
Register-PSRepository -Name NewRepo -SourceLocation "https://www.microsoft.com/api/v2"-WarningAction SilentlyContinue
259+
Set-PSRepository -Name NewRepo -SourceLocation "https://docs.microsoft.com/api/v2" -WarningVariable warning -WarningAction SilentlyContinue
260+
if($psversiontable.PSVersion -ge '5.0.0') { # -warningvariable doesn't seem to work in ps4?
261+
$warning -join "" | Should BeLike "*Unable to reach URL 'https://docs*"
262+
}
263+
}
264+
265+
It "Should let you add a package source but produce a warning" {
266+
Register-PackageSource NewRepo -Location https://microsoft.com/api/v2 -ProviderName powershellget -WarningVariable warning -WarningAction SilentlyContinue
267+
$warning -join "" | Should BeLike "*Unable to reach URL*"
268+
}
269+
270+
It "Should let you update a package source but produce a warning" {
271+
Register-PackageSource NewRepo -Location https://microsoft.com/api/v2 -ProviderName powershellget -WarningAction SilentlyContinue
272+
Set-PackageSource -Name NewRepo -Location https://microsoft.com/api/v2 -NewLocation https://docs.microsoft.com/api/v2 -ProviderName powershellget -WarningVariable warning -WarningAction SilentlyContinue
273+
$warning -join "" | Should BeLike "*Unable to reach URL 'https://docs*"
274+
}
275+
276+
It "Should not let you register 2 repositories which differ only by /" {
277+
Register-PSRepository -Name NewRepo -SourceLocation "https://nowhere.com/api/v2" -WarningAction SilentlyContinue
278+
{ Register-PSRepository -Name NewRepoSlash -SourceLocation "https://nowhere.com/api/v2/" -WarningAction SilentlyContinue } | Should -Throw
279+
} -Skip
280+
}
281+
282+
Describe "Managing galleries while offline" -Tag BVT {
283+
BeforeAll {
284+
if(-not (Get-PSRepository PSGallery -ErrorAction SilentlyContinue)) {
285+
Register-PSRepository -Default
286+
}
287+
}
288+
289+
AfterAll {
290+
if(-not (Get-PSRepository PSGallery -ErrorAction SilentlyContinue)) {
291+
Register-PSRepository -Default
292+
}
293+
}
294+
295+
Context "Mock network failures" {
296+
# Pinging any endpoint results in no response. However this only affects the front-end of powershellget,
297+
# not code running in the package provider, so it is not truly blocking all network access.
298+
Mock Ping-EndPoint -ModuleName powershellget {}
299+
300+
It "Should let you unregister and reregister PSGallery" {
301+
Unregister-PSRepository PSGallery -WarningVariable unregisterWarning -WarningAction SilentlyContinue
302+
$unregisterWarning | Should Be $null
303+
304+
Register-PSRepository -Default -WarningAction SilentlyContinue
305+
$defaultRepo = Get-PSRepository -Name PSGallery
306+
$DefaultRepo.SourceLocation | Should Be "https://www.powershellgallery.com/api/v2"
307+
}
308+
309+
It "Should let you trust the gallery even when it is unavailable" {
310+
Set-PSRepository PSGallery -InstallationPolicy Trusted
311+
}
312+
}
228313
}

src/PowerShellGet/PSGet.Resource.psd1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ ConvertFrom-StringData @'
9999
FailedToPublish=Failed to publish module '{0}': '{1}'.
100100
PublishedSuccessfully=Successfully published module '{0}' to the module publish location '{1}'. Please allow few minutes for '{2}' to show up in the search results.
101101
InvalidWebUri=The specified Uri '{0}' for parameter '{1}' is an invalid Web Uri. Please ensure that it meets the Web Uri requirements.
102+
WarnUnableToReachWebUri=Unable to reach URL '{0}'. Please check that the URL is correct and that Proxy and ProxyCredentials (if needed) are correct.
102103
RepositoryAlreadyRegistered=The repository could not be registered because there exists a registered repository with Name '{0}' and SourceLocation '{1}'. To register another repository with Name '{2}', please unregister the existing repository using the Unregister-PSRepository cmdlet.
103104
RepositoryToBeUnregisteredNotFound=The repository '{0}' was not removed because no repository was found with that name. Please run Get-PSRepository and ensure that a repository of that name is present.
104105
RepositoryCannotBeUnregistered=The specified repository '{0}' cannot be unregistered.

src/PowerShellGet/private/functions/Check-PSGalleryApiAvailability.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,23 +62,23 @@ function Check-PSGalleryApiAvailability
6262

6363
# ping V2
6464
$res_v2 = Ping-Endpoint -Endpoint $PSGalleryV2ApiUri
65-
if ($res_v2.ContainsKey($Script:ResponseUri))
65+
if ($res_v2 -and $res_v2.ContainsKey($Script:ResponseUri))
6666
{
6767
$resolvedUri_v2 = $res_v2[$Script:ResponseUri]
6868
}
69-
if ($res_v2.ContainsKey($Script:StatusCode))
69+
if ($res_v2 -and $res_v2.ContainsKey($Script:StatusCode))
7070
{
7171
$statusCode_v2 = $res_v2[$Script:StatusCode]
7272
}
7373

7474

7575
# ping V3
7676
$res_v3 = Ping-Endpoint -Endpoint $PSGalleryV3ApiUri
77-
if ($res_v3.ContainsKey($Script:ResponseUri))
77+
if ($res_v3 -and $res_v3.ContainsKey($Script:ResponseUri))
7878
{
7979
$resolvedUri_v3 = $res_v3[$Script:ResponseUri]
8080
}
81-
if ($res_v3.ContainsKey($Script:StatusCode))
81+
if ($res_v3 -and $res_v3.ContainsKey($Script:StatusCode))
8282
{
8383
$statusCode_v3 = $res_v3[$Script:StatusCode]
8484
}

src/PowerShellGet/private/functions/Get-DynamicParameters.ps1

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ function Get-DynamicParameters
2525

2626
# Ping and resolve the specified location
2727
$loc = Resolve-Location -Location $loc `
28-
-LocationParameterName 'Location' `
2928
-ErrorAction SilentlyContinue `
3029
-WarningAction SilentlyContinue
3130
if(-not $loc)

src/PowerShellGet/private/functions/Get-ScriptSourceLocation.ps1

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ function Get-ScriptSourceLocation
4343
{
4444
# Ping and resolve the specified location
4545
$scriptLocation = Resolve-Location -Location $tempScriptLocation `
46-
-LocationParameterName 'ScriptSourceLocation' `
4746
-Credential $Credential `
4847
-Proxy $Proxy `
4948
-ProxyCredential $ProxyCredential `

src/PowerShellGet/private/functions/Get-ValidModuleLocation.ps1

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,11 @@ function Get-ValidModuleLocation
5353
{
5454
# Ping and resolve the specified location
5555
$tempLocation = Resolve-Location -Location $tempLocation `
56-
-LocationParameterName $ParameterName `
5756
-Credential $Credential `
5857
-Proxy $Proxy `
5958
-ProxyCredential $ProxyCredential `
6059
-ErrorAction SilentlyContinue `
61-
-WarningAction SilentlyContinue
60+
-SkipLocationWarning
6261
if($tempLocation)
6362
{
6463
return $tempLocation
@@ -69,7 +68,6 @@ function Get-ValidModuleLocation
6968

7069
# Ping and resolve the specified location
7170
$LocationString = Resolve-Location -Location $LocationString `
72-
-LocationParameterName $ParameterName `
7371
-Credential $Credential `
7472
-Proxy $Proxy `
7573
-ProxyCredential $ProxyCredential `

src/PowerShellGet/private/functions/Resolve-Location.ps1

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ function Resolve-Location
88
[string]
99
$Location,
1010

11-
[Parameter(Mandatory=$true)]
12-
[string]
13-
$LocationParameterName,
14-
1511
[Parameter()]
1612
$Credential,
1713

@@ -23,7 +19,11 @@ function Resolve-Location
2319

2420
[Parameter()]
2521
[System.Management.Automation.PSCmdlet]
26-
$CallerPSCmdlet
22+
$CallerPSCmdlet,
23+
24+
[Parameter()]
25+
[switch]
26+
$SkipLocationWarning
2727
)
2828

2929
# Ping and resolve the specified location
@@ -66,15 +66,16 @@ function Resolve-Location
6666
{
6767
return $resolvedLocation
6868
}
69-
elseif($CallerPSCmdlet)
69+
else
7070
{
71-
$message = $LocalizedData.InvalidWebUri -f ($Location, $LocationParameterName)
72-
ThrowError -ExceptionName "System.ArgumentException" `
73-
-ExceptionMessage $message `
74-
-ErrorId "InvalidWebUri" `
75-
-CallerPSCmdlet $CallerPSCmdlet `
76-
-ErrorCategory InvalidArgument `
77-
-ExceptionObject $Location
71+
# We couldn't reach the repo. Register it anyway but warn the user if we're able
72+
# We'd like to include the parameter name here, but Register-PSRepository calls Add-PackageSource,
73+
# and both use different param names, so the parameter name becomes confusing
74+
if($CallerPSCmdlet -and -not $SkipLocationWarning) {
75+
$message = $LocalizedData.WarnUnableToReachWebUri -f ($Location)
76+
Write-Warning $message
77+
}
78+
return $Location
7879
}
7980
}
8081
}

src/PowerShellGet/private/functions/Set-PSGalleryRepository.ps1

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,26 @@ function Set-PSGalleryRepository
1010
$Proxy,
1111

1212
[Parameter()]
13-
$ProxyCredential
13+
$ProxyCredential,
14+
15+
[Parameter()]
16+
[System.Management.Automation.PSCmdlet]
17+
$CallerPSCmdlet
1418
)
1519

1620
$psgalleryLocation = Resolve-Location -Location $Script:PSGallerySourceUri `
17-
-LocationParameterName 'SourceLocation' `
1821
-Proxy $Proxy `
1922
-ProxyCredential $ProxyCredential `
20-
-ErrorAction SilentlyContinue `
21-
-WarningAction SilentlyContinue
23+
-CallerPSCmdlet $CallerPSCmdlet `
24+
-ErrorAction SilentlyContinue
25+
2226

2327
$scriptSourceLocation = Resolve-Location -Location $Script:PSGalleryScriptSourceUri `
24-
-LocationParameterName 'ScriptSourceLocation' `
2528
-Proxy $Proxy `
2629
-ProxyCredential $ProxyCredential `
27-
-ErrorAction SilentlyContinue `
28-
-WarningAction SilentlyContinue
30+
-CallerPSCmdlet $CallerPSCmdlet `
31+
-ErrorAction SilentlyContinue
32+
2933
if($psgalleryLocation)
3034
{
3135
$result = Ping-Endpoint -Endpoint $Script:PSGalleryPublishUri -AllowAutoRedirect:$false -Proxy $Proxy -ProxyCredential $ProxyCredential

0 commit comments

Comments
 (0)