Skip to content

Commit b050c97

Browse files
authored
Merge pull request #154 from ShaunLawrie/fix-write-progress-on-psdrive
Add PSDrive support for Invoke-RestMethodWithProgress
2 parents af2eaf9 + 6a6a6e8 commit b050c97

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

Private/Invoke-RestMethodWithProgress.ps1

+13-3
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ function Invoke-RestMethodWithProgress {
4141
$ProgressActivity = "Thinking..."
4242
)
4343

44+
$currentLocation = Get-Location
45+
4446
# Some hosts can't support background jobs. It's best to opt-in to this feature by using a list of supported hosts
45-
if($script:SupportedHosts -notcontains (Get-Host).Name) {
47+
if($script:SupportedHosts -notcontains (Get-Host).Name -or $currentLocation.Provider.Name -ne "FileSystem") {
4648
return Invoke-RestMethod @Params
4749
}
4850

@@ -51,6 +53,11 @@ function Invoke-RestMethodWithProgress {
5153
try {
5254
try { [Console]::CursorVisible = $false }
5355
catch [System.IO.IOException] { <# unit tests don't have a console #> }
56+
57+
Push-Location -StackName "RestMethodWithProgress"
58+
if($currentLocation.Path -ne $currentLocation.ProviderPath) {
59+
Set-Location $currentLocation.ProviderPath
60+
}
5461

5562
$job = Start-Job {
5663
$restParameters = $using:Params
@@ -87,8 +94,11 @@ function Invoke-RestMethodWithProgress {
8794
} catch {
8895
throw $_
8996
} finally {
90-
Stop-Job $job -ErrorAction "SilentlyContinue"
91-
Remove-Job $job -Force -ErrorAction "SilentlyContinue"
97+
Pop-Location -StackName "RestMethodWithProgress" -ErrorAction "SilentlyContinue"
98+
if($null -ne $job) {
99+
Stop-Job $job -ErrorAction "SilentlyContinue"
100+
Remove-Job $job -Force -ErrorAction "SilentlyContinue"
101+
}
92102
try { [Console]::CursorVisible = $true }
93103
catch [System.IO.IOException] { <# unit tests don't have a console #> }
94104
}

__tests__/Invoke-RestMethodWithProgress.tests.ps1

+52
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ Describe "Invoke-RestMethodWithProgress" -Tag InvokeRestMethodWithProgress {
88

99
BeforeEach {
1010
Reset-APIEstimatedResponseTimes
11+
Push-Location -StackName "MOCK-IRMWP" -Path $PSScriptRoot
12+
New-PSDrive -Name "MOCK-IRMWP" -PSProvider "FileSystem" -Root $PSScriptRoot
13+
}
14+
15+
AfterEach {
16+
Pop-Location -StackName "MOCK-IRMWP" -ErrorAction "SilentlyContinue"
17+
Remove-PSDrive -Name "MOCK-IRMWP" -ErrorAction "SilentlyContinue"
1118
}
1219

1320
It "should return the known response if the API call is successful" {
@@ -31,6 +38,51 @@ Describe "Invoke-RestMethodWithProgress" -Tag InvokeRestMethodWithProgress {
3138
$response | Should -BeExactly "a happy web response from a web server"
3239
}
3340

41+
It "should work when run from a psdrive" {
42+
43+
Mock Start-Job {
44+
return & $script:StartJobCommand { }
45+
}
46+
47+
Mock Receive-Job {
48+
return @{
49+
Response = "a happy web response from a web server"
50+
}
51+
}
52+
53+
$params = @{
54+
"Method" = "GET";
55+
"Uri" = "http://localhost";
56+
}
57+
58+
Set-Location "MOCK-IRMWP:\"
59+
60+
$response = Invoke-RestMethodWithProgress -Params $params
61+
$response | Should -BeExactly "a happy web response from a web server"
62+
}
63+
64+
It "should work when run from a non-filesystem provider" {
65+
66+
Mock Invoke-RestMethod {
67+
return "a happy web response from a web server"
68+
}
69+
70+
Mock Start-Job { }
71+
72+
$params = @{
73+
"Method" = "GET";
74+
"Uri" = "http://localhost";
75+
}
76+
77+
Push-Location -StackName "MOCK-IRMWP"
78+
Set-Location "Env:\"
79+
80+
$response = Invoke-RestMethodWithProgress -Params $params
81+
$response | Should -BeExactly "a happy web response from a web server"
82+
Should -Invoke -CommandName Invoke-RestMethod -Times 1
83+
Should -Invoke -CommandName Start-Job -Times 0
84+
}
85+
3486
It "should throw if the API call fails" {
3587

3688
$params = @{

0 commit comments

Comments
 (0)