Clean up ShellExecute code to use the .NET Core implementation (#4523)

* Clean up ShellExecuteHelper and enable ShellExecute in NativeCommandProcessor

* Minor fix

* [Feature] Fix NativeCommandProcessor to clean up in case an exception is thron

* [Feature] Update tests

* [Feature] Address comments

* address one more comment

* Address some more comments
This commit is contained in:
Dongbo Wang
2017-08-10 13:32:57 -07:00
committed by Aditya Patwardhan
parent 7762d7bf49
commit 384a9fe3fc
8 changed files with 226 additions and 337 deletions
@@ -119,5 +119,102 @@ Describe "Native Command Processor" -tags "Feature" {
$ps.Dispose()
}
}
}
Describe "Open a text file with NativeCommandProcessor" -tags @("Feature", "RequireAdminOnWindows") {
BeforeAll {
if ($IsWindows) {
$TestFile = Join-Path -Path $TestDrive -ChildPath "TextFileTest.foo"
} else {
$TestFile = Join-Path -Path $TestDrive -ChildPath "TextFileTest.txt"
}
Set-Content -Path $TestFile -Value "Hello" -Force
$supportedEnvironment = $true
if ($IsLinux) {
$appFolder = "$HOME/.local/share/applications"
$supportedEnvironment = Test-Path $appFolder
if ($supportedEnvironment) {
$mimeDefault = xdg-mime query default text/plain
Remove-Item $HOME/nativeCommandProcessor.Success -Force -ErrorAction SilentlyContinue
Set-Content -Path "$appFolder/nativeCommandProcessor.desktop" -Force -Value @"
[Desktop Entry]
Version=1.0
Name=nativeCommandProcessor
Comment=Validate_native_command_processor_open_text_file
Exec=/bin/sh -c 'echo %u > ~/nativeCommandProcessor.Success'
Icon=utilities-terminal
Terminal=true
Type=Application
Categories=Application;
"@
xdg-mime default nativeCommandProcessor.desktop text/plain
}
}
elseif ($IsWindows) {
$supportedEnvironment = [System.Management.Automation.Platform]::IsWindowsDesktop
if ($supportedEnvironment) {
cmd /c assoc .foo=foofile
cmd /c ftype foofile=cmd /c echo %1^> $TestDrive\foo.txt
Remove-Item $TestDrive\foo.txt -Force -ErrorAction SilentlyContinue
}
}
}
AfterAll {
Remove-Item -Path $TestFile -Force -ErrorAction SilentlyContinue
if ($IsLinux -and $supportedEnvironment) {
xdg-mime default $mimeDefault text/plain
Remove-Item $appFolder/nativeCommandProcessor.desktop -Force -ErrorAction SilentlyContinue
Remove-Item $HOME/nativeCommandProcessor.Success -Force -ErrorAction SilentlyContinue
}
elseif ($IsWindows -and $supportedEnvironment) {
cmd /c assoc .foo=
cmd /c ftype foofile=
}
}
It "Should open text file without error" -Skip:(!$supportedEnvironment) {
if ($IsOSX) {
$expectedTitle = Split-Path $TestFile -Leaf
open -F -a TextEdit
$beforeCount = [int]('tell application "TextEdit" to count of windows' | osascript)
& $TestFile
$startTime = Get-Date
$title = [String]::Empty
while (((Get-Date) - $startTime).TotalSeconds -lt 30 -and ($title -ne $expectedTitle)) {
Start-Sleep -Milliseconds 100
$title = 'tell application "TextEdit" to get name of front window' | osascript
}
$afterCount = [int]('tell application "TextEdit" to count of windows' | osascript)
$afterCount | Should Be ($beforeCount + 1)
$title | Should Be $expectedTitle
"tell application ""TextEdit"" to close window ""$expectedTitle""" | osascript
'tell application "TextEdit" to quit' | osascript
}
elseif ($IsLinux) {
# Validate on Linux by reassociating default app for text file
& $TestFile
$startTime = Get-Date
# It may take time for handler to start
while (((Get-Date) - $startTime).TotalSeconds -lt 10 -and (-not (Test-Path "$HOME/nativeCommandProcessor.Success"))) {
Start-Sleep -Milliseconds 100
}
Get-Content $HOME/nativeCommandProcessor.Success | Should Be $TestFile
}
else {
& $TestFile
$startTime = Get-Date
while (((Get-Date) - $startTime).TotalSeconds -lt 10 -and (!(Test-Path $TestDrive\foo.txt))) {
Start-Sleep -Milliseconds 100
}
"$TestDrive\foo.txt" | Should Exist
Get-Content $TestDrive\foo.txt | Should BeExactly $TestFile
}
}
It "Opening a file with an unregistered extension on Windows should fail" -Skip:(!$IsWindows) {
{ $dllFile = "$PSHOME\System.Management.Automation.dll"; & $dllFile } | ShouldBeErrorId "NativeCommandFailed"
}
}