-
Notifications
You must be signed in to change notification settings - Fork 274
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PowerShell SDK] Add Invoke-VstsProcess (#978)
* Update package-lock * Add Invoke-Process function * Add Invoke-Process to members list * Fix $LastExitCode * Revert VstsTaskSdk crlf changes * Revert "refactor: remove Q library from Powershell SDK in favor of native promises (#944)" This reverts commit 44e727d. * Fix proc exit code * Update Invoke-Process * Fix SupportsWorkingDirectory test * Remove encoding param, update encoding test * Fix path in test * update loc resources * Revert "Revert "refactor: remove Q library from Powershell SDK in favor of native promises (#944)"" This reverts commit a703a0a. * Gen doc * Update changelog
- Loading branch information
1 parent
0b4a3c7
commit 5fb0f50
Showing
9 changed files
with
1,161 additions
and
1,403 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,86 @@ | ||
# Invoke-VstsProcess | ||
[table of contents](../Commands.md#toc) | [brief](../Commands.md#invoke-vstsprocess) | ||
``` | ||
NAME | ||
Invoke-VstsProcess | ||
SYNOPSIS | ||
Executes an external program as a child process. | ||
SYNTAX | ||
Invoke-VstsProcess [-FileName] <String> [[-Arguments] <String>] [[-WorkingDirectory] <String>] | ||
[[-StdOutPath] <String>] [[-StdErrPath] <String>] [-RequireExitCodeZero] [<CommonParameters>] | ||
DESCRIPTION | ||
Executes an external program and waits for the process to exit. | ||
After calling this command, the exit code of the process can be retrieved from the variable $LASTEXITCODE | ||
or from the pipe. | ||
PARAMETERS | ||
-FileName <String> | ||
File name (path) of the program to execute. | ||
Required? true | ||
Position? 1 | ||
Default value | ||
Accept pipeline input? false | ||
Accept wildcard characters? false | ||
-Arguments <String> | ||
Arguments to pass to the program. | ||
Required? false | ||
Position? 2 | ||
Default value | ||
Accept pipeline input? false | ||
Accept wildcard characters? false | ||
-WorkingDirectory <String> | ||
Required? false | ||
Position? 3 | ||
Default value | ||
Accept pipeline input? false | ||
Accept wildcard characters? false | ||
-StdOutPath <String> | ||
Path to a file to write the stdout of the process to. | ||
Required? false | ||
Position? 4 | ||
Default value | ||
Accept pipeline input? false | ||
Accept wildcard characters? false | ||
-StdErrPath <String> | ||
Path to a file to write the stderr of the process to. | ||
Required? false | ||
Position? 5 | ||
Default value | ||
Accept pipeline input? false | ||
Accept wildcard characters? false | ||
-RequireExitCodeZero [<SwitchParameter>] | ||
Indicates whether to write an error to the error pipeline if the exit code is not zero. | ||
Required? false | ||
Position? named | ||
Default value False | ||
Accept pipeline input? false | ||
Accept wildcard characters? false | ||
<CommonParameters> | ||
This cmdlet supports the common parameters: Verbose, Debug, | ||
ErrorAction, ErrorVariable, WarningAction, WarningVariable, | ||
OutBuffer, PipelineVariable, and OutVariable. For more information, see | ||
about_CommonParameters (https://go.microsoft.com/fwlink/?LinkID=113216). | ||
OUTPUTS | ||
Exit code of the invoked process. Also available through the $LASTEXITCODE. | ||
NOTES | ||
To change output encoding, redirect stdout to file and then read the file with the desired encoding. | ||
``` |
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
16 changes: 16 additions & 0 deletions
16
powershell/Tests/L0/Invoke-Process.ReturnsCorrectExitCode.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,16 @@ | ||
[CmdletBinding()] | ||
param() | ||
|
||
# Arrange. | ||
. $PSScriptRoot\..\lib\Initialize-Test.ps1 | ||
$Global:LASTEXITCODE = 1 | ||
|
||
Invoke-VstsTaskScript -ScriptBlock { | ||
|
||
# Act. | ||
$actualEC = Invoke-VstsProcess -FileName 'cmd.exe' -Arguments '/c echo test' | ||
|
||
# Assert. | ||
Assert-AreEqual -Expected 0 -Actual $actualEC | ||
Assert-AreEqual 0 $LASTEXITCODE | ||
} |
56 changes: 56 additions & 0 deletions
56
powershell/Tests/L0/Invoke-Process.SupportsOutputEncoding.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,56 @@ | ||
[CmdletBinding()] | ||
param() | ||
|
||
# Actually, Invoke-VstsProcess does not support encoding by itself, it just allows to get the output of the process in a file, and then you can use Get-Content to read the file with the encoding you want. | ||
|
||
# Arrange. | ||
. $PSScriptRoot\..\lib\Initialize-Test.ps1 | ||
Invoke-VstsTaskScript -ScriptBlock { | ||
$tempDirectory = [System.IO.Path]::Combine([System.IO.Path]::GetTempPath(), [System.IO.Path]::GetRandomFileName()) | ||
New-Item -Path $tempDirectory -ItemType Directory | ForEach-Object { $_.FullName } | ||
try { | ||
set-Content -LiteralPath $tempDirectory\Program.cs -Value @" | ||
namespace TestEncoding { | ||
public static class Program { | ||
public static void Main() { | ||
System.Text.Encoding encoding = System.Text.Encoding.Unicode; | ||
byte[] bytes = encoding.GetBytes("Hello world"); | ||
using (System.IO.Stream stdout = System.Console.OpenStandardOutput()) { | ||
stdout.Write(bytes, 0, bytes.Length); | ||
stdout.Flush(); | ||
} | ||
} | ||
} | ||
} | ||
"@ | ||
Add-Type -LiteralPath $tempDirectory\Program.cs -OutputType ConsoleApplication -OutputAssembly $tempDirectory\TestEncoding.exe | ||
$originalEncoding = [System.Console]::OutputEncoding | ||
$variableSets = @( | ||
@{ Encoding = $null ; Expected = "H_e_l_l_o_ _w_o_r_l_d_" } | ||
@{ Encoding = 'unicode' ; Expected = "Hello world" } | ||
) | ||
foreach ($variableSet in $variableSets) { | ||
$stdOutPath = [System.IO.Path]::Combine($tempDirectory, [System.IO.Path]::GetRandomFileName()) | ||
|
||
# Act. | ||
Invoke-VstsProcess ` | ||
-FileName $tempDirectory\TestEncoding.exe ` | ||
-StdOutPath $stdOutPath ` | ||
|
||
if ($variableSet.Encoding) { | ||
$actual = Get-Content -LiteralPath $stdOutPath -Encoding $variableSet.Encoding | ||
} | ||
else { | ||
$actual = Get-Content -LiteralPath $stdOutPath -Raw | ||
} | ||
|
||
# Assert. | ||
$actual = $actual.Replace("`0", "_") # Replace null characters with spaces in order for the string comparison to be accurate. | ||
Assert-AreEqual $variableSet.Expected $actual | ||
Assert-AreEqual $originalEncoding ([System.Console]::OutputEncoding) | ||
} | ||
} | ||
finally { | ||
Remove-Item $tempDirectory -Recurse | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
powershell/Tests/L0/Invoke-Process.SupportsWorkingDirectory.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,39 @@ | ||
[CmdletBinding()] | ||
param() | ||
|
||
# Arrange. | ||
. $PSScriptRoot\..\lib\Initialize-Test.ps1 | ||
Invoke-VstsTaskScript -ScriptBlock { | ||
$originalLocation = $PWD | ||
$tempDirectory = [System.IO.Path]::Combine([System.IO.Path]::GetTempPath(), [System.IO.Path]::GetRandomFileName()) | ||
New-Item -Path $tempDirectory -ItemType Directory | ForEach-Object { $_.FullName } | ||
try { | ||
Set-Location $env:TMP | ||
$variableSets = @( | ||
@{ Expected = [System.IO.Path]::GetTempPath().TrimEnd('\') ; Splat = @{ } } | ||
@{ Expected = [System.IO.Path]::GetTempPath().TrimEnd('\') ; Splat = @{ WorkingDirectory = [System.IO.Path]::GetTempPath() } } | ||
@{ Expected = $tempDirectory ; Splat = @{ WorkingDirectory = $tempDirectory } } | ||
) | ||
foreach ($variableSet in $variableSets) { | ||
$splat = $variableSet.Splat | ||
|
||
$stdOutPath = [System.IO.Path]::Combine($tempDirectory, [System.IO.Path]::GetRandomFileName()) | ||
|
||
# Act. | ||
Invoke-VstsProcess ` | ||
-FileName 'cmd.exe' ` | ||
-Arguments '/c "CD"' ` | ||
-StdOutPath $stdOutPath ` | ||
@splat | ||
|
||
$actual = Get-Content -LiteralPath $stdOutPath -Encoding UTF8 | ||
|
||
# Assert. | ||
Assert-AreEqual $variableSet.Expected $actual | ||
Assert-AreEqual ([System.IO.Path]::GetTempPath().TrimEnd('\')) (Get-Location).Path | ||
} | ||
} finally { | ||
Set-Location $originalLocation | ||
Remove-Item $tempDirectory -Recurse | ||
} | ||
} |
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
Oops, something went wrong.