2020-03-24 19:08:37 +01:00
|
|
|
# Copyright (c) Microsoft Corporation.
|
2018-10-30 17:55:39 +01:00
|
|
|
# Licensed under the MIT License.
|
|
|
|
|
2018-11-10 00:11:02 +01:00
|
|
|
Describe "TestImplicitRemotingBatching hook should correctly batch simple remote command pipelines" -Tag 'Feature','RequireAdminOnWindows' {
|
2018-10-30 17:55:39 +01:00
|
|
|
|
|
|
|
BeforeAll {
|
|
|
|
|
2020-01-13 20:27:21 +01:00
|
|
|
if (! $IsWindows) { return }
|
2018-10-30 17:55:39 +01:00
|
|
|
|
2018-11-10 00:11:02 +01:00
|
|
|
function ThrowSetupError
|
|
|
|
{
|
|
|
|
param (
|
|
|
|
[string] $errorMessage,
|
|
|
|
[System.Management.Automation.ErrorRecord[]] $eRecords
|
|
|
|
)
|
|
|
|
|
|
|
|
$msg = @()
|
|
|
|
foreach ($err in $powerShell.Streams.Error)
|
|
|
|
{
|
|
|
|
$msg += $err.ToString() + "`n"
|
|
|
|
}
|
|
|
|
|
|
|
|
throw "$errorMessage : '$msg'"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Make sure we can create a remote session
|
|
|
|
$remotePSSession = New-RemoteSession
|
|
|
|
if ($remotePSSession -eq $null)
|
|
|
|
{
|
|
|
|
Write-Verbose "Unable to create a remote session in test."
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Remove-PSSession $remotePSSession
|
|
|
|
}
|
|
|
|
|
2018-10-30 17:55:39 +01:00
|
|
|
[powershell] $powerShell = [powershell]::Create([System.Management.Automation.RunspaceMode]::NewRunspace)
|
|
|
|
|
|
|
|
# Create remote session in new PowerShell session
|
|
|
|
$powerShell.AddScript('Import-Module -Name HelpersRemoting; $remoteSession = New-RemoteSession').Invoke()
|
2018-11-10 00:11:02 +01:00
|
|
|
if ($powerShell.Streams.Error.Count -gt 0)
|
|
|
|
{
|
|
|
|
ThrowSetupError -errorMessage "Unable to create remote session for test with error" -eRecords $powerShell.Streams.Error
|
|
|
|
}
|
2018-10-30 17:55:39 +01:00
|
|
|
|
|
|
|
# Import implicit commands from remote session
|
|
|
|
$powerShell.Commands.Clear()
|
|
|
|
$powerShell.AddScript('Import-PSSession -Session $remoteSession -CommandName Get-Process,Write-Output -AllowClobber').Invoke()
|
2018-11-10 00:11:02 +01:00
|
|
|
if ($powerShell.Streams.Error.Count -gt 0)
|
|
|
|
{
|
|
|
|
ThrowSetupError -errorMessage "Unable to import pssession for test" -eRecords $powerShell.Streams.Error
|
|
|
|
}
|
2018-10-30 17:55:39 +01:00
|
|
|
|
|
|
|
# Define $filter variable in local session
|
|
|
|
$powerShell.Commands.Clear()
|
|
|
|
$powerShell.AddScript('$filter = "pwsh","powershell"').Invoke()
|
|
|
|
$localRunspace = $powerShell.Runspace
|
|
|
|
|
|
|
|
[powershell] $psInvoke = [powershell]::Create([System.Management.Automation.RunspaceMode]::NewRunspace)
|
|
|
|
|
|
|
|
$testCases = @(
|
|
|
|
@{
|
|
|
|
Name = 'Two implicit commands should be successfully batched'
|
|
|
|
CommandLine = 'Get-Process -Name "pwsh" | Write-Output'
|
|
|
|
ExpectedOutput = $true
|
|
|
|
},
|
|
|
|
@{
|
|
|
|
Name = 'Two implicit commands with Where-Object should be successfully batched'
|
|
|
|
CommandLine = 'Get-Process | Write-Output | Where-Object { $_.Name -like "*pwsh*" }'
|
|
|
|
ExpectedOutput = $true
|
|
|
|
},
|
|
|
|
@{
|
|
|
|
Name = 'Two implicit commands with Where-Object alias (?) should be successfully batched'
|
|
|
|
CommandLine = 'Get-Process | Write-Output | ? { $_.Name -like "*pwsh*" }'
|
|
|
|
ExpectedOutput = $true
|
|
|
|
},
|
|
|
|
@{
|
|
|
|
Name = 'Two implicit commands with Where-Object alias (where) should be successfully batched'
|
|
|
|
CommandLine = 'Get-Process | Write-Output | where { $_.Name -like "*pwsh*" }'
|
|
|
|
ExpectedOutput = $true
|
|
|
|
},
|
|
|
|
@{
|
|
|
|
Name = 'Two implicit commands with Sort-Object should be successfully batched'
|
|
|
|
CommandLine = 'Get-Process -Name "pwsh" | Sort-Object -Property Name | Write-Output'
|
|
|
|
ExpectedOutput = $true
|
|
|
|
},
|
|
|
|
@{
|
|
|
|
Name = 'Two implicit commands with Sort-Object alias (sort) should be successfully batched'
|
|
|
|
CommandLine = 'Get-Process -Name "pwsh" | sort -Property Name | Write-Output'
|
|
|
|
ExpectedOutput = $true
|
|
|
|
},
|
|
|
|
@{
|
|
|
|
Name = 'Two implicit commands with ForEach-Object should be successfully batched'
|
|
|
|
CommandLine = 'Get-Process -Name "pwsh" | Write-Output | ForEach-Object { $_ }'
|
|
|
|
ExpectedOutput = $true
|
|
|
|
},
|
|
|
|
@{
|
|
|
|
Name = 'Two implicit commands with ForEach-Object alias (%) should be successfully batched'
|
|
|
|
CommandLine = 'Get-Process -Name "pwsh" | Write-Output | % { $_ }'
|
|
|
|
ExpectedOutput = $true
|
|
|
|
},
|
|
|
|
@{
|
|
|
|
Name = 'Two implicit commands with ForEach-Object alias (foreach) should be successfully batched'
|
|
|
|
CommandLine = 'Get-Process -Name "pwsh" | Write-Output | foreach { $_ }'
|
|
|
|
ExpectedOutput = $true
|
|
|
|
},
|
|
|
|
@{
|
|
|
|
Name = 'Two implicit commands with Measure-Command should be successfully batched'
|
|
|
|
CommandLine = 'Measure-Command { Get-Process | Write-Output }'
|
|
|
|
ExpectedOutput = $true
|
|
|
|
},
|
|
|
|
@{
|
|
|
|
Name = 'Two implicit commands with Measure-Object should be successfully batched'
|
|
|
|
CommandLine = 'Get-Process | Write-Output | Measure-Object'
|
|
|
|
ExpectedOutput = $true
|
|
|
|
},
|
|
|
|
@{
|
|
|
|
Name = 'Two implicit commands with Measure-Object alias (measure) should be successfully batched'
|
|
|
|
CommandLine = 'Get-Process | Write-Output | measure'
|
|
|
|
ExpectedOutput = $true
|
|
|
|
},
|
|
|
|
@{
|
|
|
|
Name = 'Implicit commands with variable arguments should be successfully batched'
|
|
|
|
CommandLine = 'Get-Process -Name $filter | Write-Output'
|
|
|
|
ExpectedOutput = $true
|
|
|
|
},
|
|
|
|
@{
|
|
|
|
Name = 'Pipeline with non-implicit command should not be batched'
|
|
|
|
CommandLine = 'Get-Process | Write-Output | Select-Object -Property Name'
|
|
|
|
ExpectedOutput = $false
|
|
|
|
},
|
|
|
|
@{
|
|
|
|
Name = 'Non-simple pipeline should not be batched'
|
|
|
|
CommandLine = '1..2 | % { Get-Process pwsh | Write-Output }'
|
|
|
|
ExpectedOutput = $false
|
|
|
|
}
|
|
|
|
@{
|
|
|
|
Name = 'Pipeline with single command should not be batched'
|
|
|
|
CommandLine = 'Get-Process pwsh'
|
|
|
|
ExpectedOutput = $false
|
|
|
|
},
|
|
|
|
@{
|
|
|
|
Name = 'Pipeline without any implicit commands should not be batched'
|
|
|
|
CommandLine = 'Get-PSSession | Out-Default'
|
|
|
|
ExpectedOutput = $false
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
AfterAll {
|
|
|
|
|
2020-01-13 20:27:21 +01:00
|
|
|
if (! $IsWindows) { return }
|
2018-10-30 17:55:39 +01:00
|
|
|
|
|
|
|
if ($remoteSession -ne $null) { Remove-PSSession $remoteSession -ErrorAction Ignore }
|
|
|
|
if ($powershell -ne $null) { $powershell.Dispose() }
|
|
|
|
if ($psInvoke -ne $null) { $psInvoke.Dispose() }
|
|
|
|
}
|
|
|
|
|
|
|
|
It "<Name>" -TestCases $testCases -Skip:(! $IsWindows) {
|
|
|
|
param ($CommandLine, $ExpectedOutput)
|
|
|
|
|
|
|
|
$psInvoke.Commands.Clear()
|
|
|
|
$psInvoke.Commands.AddScript('param ($cmdLine, $runspace) [System.Management.Automation.Internal.InternalTestHooks]::TestImplicitRemotingBatching($cmdLine, $runspace)').AddArgument($CommandLine).AddArgument($localRunspace)
|
|
|
|
|
|
|
|
$result = $psInvoke.Invoke()
|
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
|
|
|
$result | Should -Be $ExpectedOutput
|
2018-10-30 17:55:39 +01:00
|
|
|
}
|
|
|
|
}
|