forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
-StrictMode
to Invoke-Command
to allow specifying strict mode…
… when invoking command locally (PowerShell#16545)
- Loading branch information
Showing
3 changed files
with
154 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
test/powershell/Modules/Microsoft.PowerShell.Core/Invoke-Command.Tests.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT License. | ||
|
||
Describe "Invoke-Command" -Tags "CI" { | ||
Context "StrictMode tests" { | ||
BeforeAll { | ||
$skipTest = !($EnabledExperimentalFeatures -contains "PSStrictModeAssignment"); | ||
If (Test-Path Variable:InvokeCommand__Test) { | ||
Remove-Item Variable:InvokeCommand__Test | ||
} | ||
} | ||
|
||
It "Setting -StrictMode parameter with uninitialized variable throws error" -skip:$skipTest { | ||
{ Invoke-Command -StrictMode 3.0 {$InvokeCommand__Test} } | Should -Throw -ErrorId 'VariableIsUndefined' | ||
} | ||
|
||
It "Setting -StrictMode parameter with initialized variable does not throw error" -skip:$skipTest { | ||
$InvokeCommand__Test = 'Something' | ||
Invoke-Command -StrictMode 3.0 {$InvokeCommand__Test} | Should -Be 'Something' | ||
Remove-Item Variable:InvokeCommand__Test | ||
} | ||
|
||
It "-StrictMode parameter sets StrictMode back to original state after process completes" -skip:$skipTest { | ||
{ Invoke-Command -StrictMode 3.0 {$InvokeCommand__Test} } | Should -Throw -ErrorId 'VariableIsUndefined' | ||
{ Invoke-Command {$InvokeCommand__Test} } | Should -Not -Throw | ||
} | ||
|
||
It "-StrictMode parameter works on piped input" -skip:$skipTest { | ||
"There" | Invoke-Command -ScriptBlock { "Hello $input" } -StrictMode 3.0 | Should -Be 'Hello There' | ||
{ "There" | Invoke-Command -ScriptBlock { "Hello $InvokeCommand__Test" } -StrictMode 3.0 } | Should -Throw -ErrorId 'VariableIsUndefined' | ||
} | ||
|
||
It "-StrictMode latest works" -skip:$skipTest { | ||
{ Invoke-Command -StrictMode latest {$InvokeCommand__Test} } | Should -Throw -ErrorId 'VariableIsUndefined' | ||
} | ||
|
||
It "-StrictMode off works" -skip:$skipTest { | ||
{ Invoke-Command -StrictMode off {$InvokeCommand__Test} } | Should -Not -Throw | ||
} | ||
} | ||
} |