2020-03-24 19:08:37 +01:00
|
|
|
# Copyright (c) Microsoft Corporation.
|
2018-02-13 18:23:53 +01:00
|
|
|
# Licensed under the MIT License.
|
|
|
|
Describe "Common parameters support for script cmdlets" -Tags "CI" {
|
2017-01-16 22:31:14 +01:00
|
|
|
BeforeEach {
|
2016-09-12 19:43:53 +02:00
|
|
|
$rs = [system.management.automation.runspaces.runspacefactory]::CreateRunspace()
|
|
|
|
$rs.open()
|
|
|
|
$ps = [System.Management.Automation.PowerShell]::Create()
|
2017-01-16 22:31:14 +01:00
|
|
|
$ps.Runspace = $rs
|
2016-09-12 19:43:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
AfterEach {
|
|
|
|
$ps.Dispose()
|
|
|
|
$rs.Dispose()
|
|
|
|
}
|
|
|
|
|
|
|
|
Context "Debug" {
|
|
|
|
BeforeAll {
|
|
|
|
$script = "
|
|
|
|
function get-foo
|
|
|
|
{
|
|
|
|
[CmdletBinding()]
|
|
|
|
param()
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2016-09-12 19:43:53 +02:00
|
|
|
write-output 'output foo'
|
|
|
|
write-debug 'debug foo'
|
|
|
|
}"
|
|
|
|
}
|
|
|
|
|
|
|
|
It "Debug get-foo" {
|
2017-01-16 22:31:14 +01:00
|
|
|
$command = 'get-foo'
|
|
|
|
[void] $ps.AddScript($script + $command)
|
2016-09-12 19:43:53 +02:00
|
|
|
$asyncResult = $ps.BeginInvoke()
|
|
|
|
$output = $ps.EndInvoke($asyncResult)
|
|
|
|
|
2018-03-21 18:47:08 +01:00
|
|
|
$output[0] | Should -BeExactly "output foo"
|
|
|
|
$ps.Streams.Debug.Count | Should -Be 0
|
2016-09-12 19:43:53 +02:00
|
|
|
}
|
|
|
|
|
2017-01-16 22:31:14 +01:00
|
|
|
It 'get-foo -debug' {
|
|
|
|
$command = 'get-foo -debug'
|
|
|
|
[void] $ps.AddScript($script + $command)
|
2016-09-12 19:43:53 +02:00
|
|
|
$asyncResult = $ps.BeginInvoke()
|
|
|
|
$output = $ps.EndInvoke($asyncResult)
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2018-03-21 18:47:08 +01:00
|
|
|
$output[0] | Should -BeExactly "output foo"
|
|
|
|
$ps.Streams.Debug[0].Message | Should -BeExactly "debug foo"
|
|
|
|
$ps.InvocationStateInfo.State | Should -BeExactly 'Completed'
|
2016-09-12 19:43:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Context "verbose" {
|
|
|
|
BeforeAll {
|
|
|
|
$script = "
|
|
|
|
function get-foo
|
|
|
|
{
|
|
|
|
[CmdletBinding()]
|
|
|
|
param()
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2016-09-12 19:43:53 +02:00
|
|
|
write-output 'output foo'
|
|
|
|
write-verbose 'verbose foo'
|
|
|
|
}"
|
|
|
|
}
|
|
|
|
|
|
|
|
It 'get-foo' {
|
2017-01-16 22:31:14 +01:00
|
|
|
$command = 'get-foo'
|
|
|
|
[void] $ps.AddScript($script + $command)
|
2016-09-12 19:43:53 +02:00
|
|
|
$asyncResult = $ps.BeginInvoke()
|
|
|
|
$output = $ps.EndInvoke($asyncResult)
|
|
|
|
|
2018-03-21 18:47:08 +01:00
|
|
|
$output[0] | Should -BeExactly "output foo"
|
|
|
|
$ps.streams.verbose.Count | Should -Be 0
|
2016-09-12 19:43:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It 'get-foo -verbose' {
|
|
|
|
$command = 'get-foo -verbose'
|
2017-01-16 22:31:14 +01:00
|
|
|
|
|
|
|
[void] $ps.AddScript($script + $command)
|
2016-09-12 19:43:53 +02:00
|
|
|
$asyncResult = $ps.BeginInvoke()
|
|
|
|
$output = $ps.EndInvoke($asyncResult)
|
|
|
|
|
2018-03-21 18:47:08 +01:00
|
|
|
$output[0] | Should -BeExactly "output foo"
|
|
|
|
$ps.Streams.verbose[0].Message | Should -BeExactly "verbose foo"
|
|
|
|
$ps.InvocationStateInfo.State | Should -BeExactly 'Completed'
|
2016-09-12 19:43:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Context "erroraction" {
|
|
|
|
BeforeAll {
|
|
|
|
$script = "
|
|
|
|
function get-foo
|
|
|
|
{
|
|
|
|
[CmdletBinding()]
|
|
|
|
param()
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2016-09-12 19:43:53 +02:00
|
|
|
write-error 'error foo'
|
|
|
|
write-output 'output foo'
|
|
|
|
}"
|
|
|
|
}
|
|
|
|
|
|
|
|
It 'erroraction' {
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2016-09-12 19:43:53 +02:00
|
|
|
$command = 'get-foo'
|
|
|
|
[void] $ps.AddScript($script + $command)
|
|
|
|
$asyncResult = $ps.BeginInvoke()
|
|
|
|
$output = $ps.EndInvoke($asyncResult)
|
|
|
|
|
2018-03-21 18:47:08 +01:00
|
|
|
$output[0] | Should -BeExactly "output foo"
|
|
|
|
$ps.Streams.error[0].ToString() | Should -Match "error foo"
|
2016-09-12 19:43:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It 'erroraction continue' {
|
|
|
|
|
|
|
|
$command = 'get-foo -erroraction Continue'
|
|
|
|
[void] $ps.AddScript($script + $command)
|
|
|
|
$asyncResult = $ps.BeginInvoke()
|
|
|
|
$output = $ps.EndInvoke($asyncResult)
|
|
|
|
|
2018-03-21 18:47:08 +01:00
|
|
|
$output[0] | Should -BeExactly "output foo"
|
|
|
|
$ps.Streams.error[0].ToString() | Should -Match "error foo"
|
2016-09-12 19:43:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It 'erroraction SilentlyContinue' {
|
|
|
|
|
|
|
|
$command = 'get-foo -erroraction SilentlyContinue'
|
|
|
|
[void] $ps.AddScript($script + $command)
|
|
|
|
$asyncResult = $ps.BeginInvoke()
|
|
|
|
$output = $ps.EndInvoke($asyncResult)
|
|
|
|
|
2018-03-21 18:47:08 +01:00
|
|
|
$output[0] | Should -BeExactly "output foo"
|
|
|
|
$ps.streams.error.count | Should -Be 0
|
2016-09-12 19:43:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It 'erroraction Stop' {
|
|
|
|
|
|
|
|
$command = 'get-foo -erroraction Stop'
|
|
|
|
|
|
|
|
[void] $ps.AddScript($script + $command)
|
|
|
|
$asyncResult = $ps.BeginInvoke()
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2018-05-17 23:42:04 +02:00
|
|
|
{ $ps.EndInvoke($asyncResult) } | Should -Throw -ErrorId "ActionPreferenceStopException"
|
|
|
|
# Exception: "Command execution stopped because the preference variable "ErrorActionPreference" or common parameter is set to Stop: error foo"
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2016-09-12 19:43:53 +02:00
|
|
|
# BUG in runspace api.
|
Update pester syntax to v4 (#11544)
* Capitalize 'Should' command and fix whitespace
```powershell
$_ -ireplace '\s?\|\s?should\b',' | Should'
```
* Capitalise and apply hyphen to 'Not' parameter
```powershell
$_ -ireplace '(\| Should) not\b','$1 -Not'
```
* Capitalise and apply hyphen to 'Be' parameter
```powershell
$_ -ireplace '(\| Should(?: -Not)?) -?be\b','$1 -Be'
```
* Capitalise and apply hyphen to 'BeExactly' parameter
$_ -ireplace '(\| Should(?: -Not)?) -?beexactly\b','$1 -BeExactly'
* Capitalise and apply hyphen to 'BeGreaterThan' parameter
```powershell
$_ -ireplace '(\| Should(?: -Not)?) -?begreaterthan\b','$1 -BeGreaterThan'
```
* Use 'BeTrue' parameter
```powershell
$_ -ireplace '(\| Should(?: -Not)?) -Be\s\$?true\b','$1 -BeTrue'
```
* Use 'BeFalse' parameter
```powershell
$_ -ireplace '(\| Should(?: -Not)?) -Be\s\$?false\b','$1 -BeFalse'
```
* Capitalise and apply hyphen to 'Match' parameter
```powershell
$_ -ireplace '(\| Should(?: -Not)?)\s-?match\b','$1 -Match'
```
* Capitalise and apply hyphen to 'Throw' parameter
```powershell
$_ -ireplace '(\| Should(?: -Not)?)\s-?throw\b','$1 -Throw'
```
* Capitalise and apply hyphen to 'BeNullOrEmpty' parameter
```powershell
$_ -ireplace '(\| Should(?: -Not)?)\s-?benullorempty\b','$1 -BeNullOrEmpty'
```
* Capitalise 'Because' parameter
```powershell
$_ -ireplace '\s-because\b',' -Because'
```
* Fix 'BeNullOrEmpty'
2020-01-11 16:41:59 +01:00
|
|
|
#$ps.error.count | Should -Be 1
|
2016-09-12 19:43:53 +02:00
|
|
|
|
2018-03-21 18:47:08 +01:00
|
|
|
$ps.InvocationStateInfo.State | Should -BeExactly 'Failed'
|
2016-09-12 19:43:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Context "SupportShouldprocess" {
|
|
|
|
$script = '
|
|
|
|
function get-foo
|
|
|
|
{
|
|
|
|
[CmdletBinding(SupportsShouldProcess=$true)]
|
2017-01-16 22:31:14 +01:00
|
|
|
param()
|
2016-09-12 19:43:53 +02:00
|
|
|
|
2020-01-13 20:19:12 +01:00
|
|
|
if($PSCmdlet.shouldprocess("foo", "foo action"))
|
2016-09-12 19:43:53 +02:00
|
|
|
{
|
|
|
|
write-output "foo action"
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
|
2017-01-16 22:31:14 +01:00
|
|
|
It 'SupportShouldprocess' {
|
2016-09-12 19:43:53 +02:00
|
|
|
|
|
|
|
$command = 'get-foo'
|
|
|
|
$ps = [system.management.automation.powershell]::Create()
|
|
|
|
[void] $ps.AddScript($script + $command)
|
|
|
|
$ps.RunspacePool = $rp
|
|
|
|
$asyncResult = $ps.BeginInvoke()
|
|
|
|
$output = $ps.EndInvoke($asyncResult)
|
|
|
|
|
2018-03-21 18:47:08 +01:00
|
|
|
$output[0] | Should -BeExactly 'foo action'
|
2016-09-12 19:43:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It 'shouldprocess support -whatif' {
|
|
|
|
|
|
|
|
$command = 'get-foo -whatif'
|
|
|
|
$ps = [system.management.automation.powershell]::Create()
|
|
|
|
[void] $ps.AddScript($script + $command)
|
|
|
|
$ps.RunspacePool = $rp
|
|
|
|
$asyncResult = $ps.BeginInvoke()
|
|
|
|
$output = $ps.EndInvoke($asyncResult)
|
|
|
|
|
2018-03-21 18:47:08 +01:00
|
|
|
$ps.InvocationStateInfo.State | Should -BeExactly 'Completed'
|
2016-09-12 19:43:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It 'shouldprocess support -confirm under the non-interactive host' {
|
|
|
|
|
|
|
|
$command = 'get-foo -confirm'
|
|
|
|
[void] $ps.AddScript($script + $command)
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2016-09-12 19:43:53 +02:00
|
|
|
$asyncResult = $ps.BeginInvoke()
|
|
|
|
$ps.EndInvoke($asyncResult)
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2018-03-21 18:47:08 +01:00
|
|
|
$ps.Streams.Error.Count | Should -Be 1 # the host does not implement it.
|
|
|
|
$ps.InvocationStateInfo.State | Should -BeExactly 'Completed'
|
2016-09-12 19:43:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Context 'confirmimpact support: none' {
|
|
|
|
BeforeAll {
|
|
|
|
$script = '
|
|
|
|
function get-foo
|
|
|
|
{
|
|
|
|
[CmdletBinding(supportsshouldprocess=$true, ConfirmImpact="none")]
|
|
|
|
param()
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2020-01-13 20:19:12 +01:00
|
|
|
if($PSCmdlet.shouldprocess("foo", "foo action"))
|
2016-09-12 19:43:53 +02:00
|
|
|
{
|
|
|
|
write-output "foo action"
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
}
|
|
|
|
|
|
|
|
It 'get-foo' {
|
|
|
|
$command = 'get-foo'
|
|
|
|
[void] $ps.AddScript($script + $command)
|
|
|
|
$asyncResult = $ps.BeginInvoke()
|
|
|
|
$output = $ps.EndInvoke($asyncResult)
|
|
|
|
|
2018-03-21 18:47:08 +01:00
|
|
|
$output[0] | Should -BeExactly 'foo action'
|
2016-09-12 19:43:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It 'get-foo -confirm' {
|
|
|
|
$command = 'get-foo -confirm'
|
|
|
|
[void] $ps.AddScript($script + $command)
|
|
|
|
$asyncResult = $ps.BeginInvoke()
|
|
|
|
$output = $ps.EndInvoke($asyncResult)
|
|
|
|
|
2018-03-21 18:47:08 +01:00
|
|
|
$output[0] | Should -BeExactly 'foo action'
|
2016-09-12 19:43:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Context 'confirmimpact support: low under the non-interactive host' {
|
|
|
|
BeforeAll {
|
|
|
|
$script = '
|
|
|
|
function get-foo
|
|
|
|
{
|
|
|
|
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact="low")]
|
|
|
|
param()
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2020-01-13 20:19:12 +01:00
|
|
|
if($PSCmdlet.shouldprocess("foo", "foo action"))
|
2016-09-12 19:43:53 +02:00
|
|
|
{
|
|
|
|
write-output "foo action"
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
}
|
|
|
|
It 'get-foo' {
|
|
|
|
$command = 'get-foo'
|
|
|
|
[void] $ps.AddScript($script + $command)
|
|
|
|
$asyncResult = $ps.BeginInvoke()
|
|
|
|
$output = $ps.EndInvoke($asyncResult)
|
|
|
|
|
2018-03-21 18:47:08 +01:00
|
|
|
$output[0] | Should -BeExactly 'foo action'
|
2016-09-12 19:43:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It 'get-foo -confirm' {
|
|
|
|
$command = 'get-foo -confirm'
|
|
|
|
|
|
|
|
[void] $ps.AddScript($script + $command)
|
|
|
|
$asyncResult = $ps.BeginInvoke()
|
|
|
|
$ps.EndInvoke($asyncResult)
|
|
|
|
|
2018-03-21 18:47:08 +01:00
|
|
|
$ps.Streams.Error.Count | Should -Be 1 # the host does not implement it.
|
|
|
|
$ps.InvocationStateInfo.State | Should -BeExactly 'Completed'
|
2016-09-12 19:43:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Context 'confirmimpact support: Medium under the non-interactive host' {
|
|
|
|
BeforeAll {
|
|
|
|
$script = '
|
|
|
|
function get-foo
|
|
|
|
{
|
|
|
|
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact="medium")]
|
|
|
|
param()
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2020-01-13 20:19:12 +01:00
|
|
|
if($PSCmdlet.shouldprocess("foo", "foo action"))
|
2016-09-12 19:43:53 +02:00
|
|
|
{
|
|
|
|
write-output "foo action"
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
}
|
|
|
|
|
|
|
|
It 'get-foo' {
|
|
|
|
$command = 'get-foo'
|
|
|
|
[void] $ps.AddScript($script + $command)
|
|
|
|
$asyncResult = $ps.BeginInvoke()
|
|
|
|
$output = $ps.EndInvoke($asyncResult)
|
|
|
|
|
2018-03-21 18:47:08 +01:00
|
|
|
$output[0] | Should -BeExactly 'foo action'
|
2016-09-12 19:43:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It 'get-foo -confirm' {
|
|
|
|
$command = 'get-foo -confirm'
|
|
|
|
[void] $ps.AddScript($script + $command)
|
|
|
|
|
|
|
|
$asyncResult = $ps.BeginInvoke()
|
|
|
|
$ps.EndInvoke($asyncResult)
|
|
|
|
|
2018-03-21 18:47:08 +01:00
|
|
|
$ps.Streams.Error.Count | Should -Be 1 # the host does not implement it.
|
|
|
|
$ps.InvocationStateInfo.State | Should -BeExactly 'Completed'
|
2016-09-12 19:43:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Context 'confirmimpact support: High under the non-interactive host' {
|
|
|
|
BeforeAll {
|
|
|
|
$script = '
|
|
|
|
function get-foo
|
|
|
|
{
|
|
|
|
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact="high")]
|
|
|
|
param()
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2020-01-13 20:19:12 +01:00
|
|
|
if($PSCmdlet.shouldprocess("foo", "foo action"))
|
2016-09-12 19:43:53 +02:00
|
|
|
{
|
|
|
|
write-output "foo action"
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
}
|
|
|
|
|
|
|
|
It 'get-foo' {
|
|
|
|
$command = 'get-foo'
|
|
|
|
[void] $ps.AddScript($script + $command)
|
|
|
|
$asyncResult = $ps.BeginInvoke()
|
|
|
|
$ps.EndInvoke($asyncResult)
|
|
|
|
|
2018-03-21 18:47:08 +01:00
|
|
|
$ps.Streams.Error.Count | Should -Be 1 # the host does not implement it.
|
|
|
|
$ps.InvocationStateInfo.State | Should -BeExactly 'Completed'
|
2016-09-12 19:43:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It 'get-foo -confirm' {
|
|
|
|
$command = 'get-foo -confirm'
|
|
|
|
[void] $ps.AddScript($script + $command)
|
|
|
|
$asyncResult = $ps.BeginInvoke()
|
|
|
|
$ps.EndInvoke($asyncResult)
|
|
|
|
|
2018-03-21 18:47:08 +01:00
|
|
|
$ps.Streams.Error.Count | Should -Be 1 # the host does not implement it.
|
|
|
|
$ps.InvocationStateInfo.State | Should -BeExactly 'Completed'
|
2016-09-12 19:43:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Context 'ShouldContinue Support under the non-interactive host' {
|
|
|
|
BeforeAll {
|
|
|
|
$script = '
|
2017-01-16 22:31:14 +01:00
|
|
|
function get-foo
|
2016-09-12 19:43:53 +02:00
|
|
|
{
|
|
|
|
[CmdletBinding()]
|
|
|
|
param()
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2020-01-13 20:19:12 +01:00
|
|
|
if($PSCmdlet.shouldcontinue("foo", "foo action"))
|
2016-09-12 19:43:53 +02:00
|
|
|
{
|
|
|
|
write-output "foo action"
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
}
|
|
|
|
|
|
|
|
It 'get-foo' {
|
|
|
|
$command = 'get-foo'
|
|
|
|
[void] $ps.AddScript($script + $command)
|
|
|
|
|
|
|
|
$asyncResult = $ps.BeginInvoke()
|
|
|
|
$ps.EndInvoke($asyncResult)
|
|
|
|
|
2018-03-21 18:47:08 +01:00
|
|
|
$ps.Streams.Error.Count | Should -Be 1 # the host does not implement it.
|
|
|
|
$ps.InvocationStateInfo.State | Should -BeExactly 'Completed'
|
2016-09-12 19:43:53 +02:00
|
|
|
}
|
2017-01-16 22:31:14 +01:00
|
|
|
}
|
2018-03-21 18:47:08 +01:00
|
|
|
}
|