mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Merge pull request #1291 from PowerShell/andschwa/useshellexecute
Fix use of UseShellExecute for .NET Core
This commit is contained in:
@@ -190,9 +190,14 @@ namespace Microsoft.PackageManagement.Internal.Implementation {
|
||||
public int StartProcess(string filename, string arguments, bool requiresElevation, out string standardOutput, IRequest requestObject) {
|
||||
Process p = new Process();
|
||||
|
||||
if (requiresElevation) {
|
||||
#if !CORECLR
|
||||
if (requiresElevation)
|
||||
{
|
||||
p.StartInfo.UseShellExecute = true;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
p.StartInfo.UseShellExecute = false;
|
||||
p.StartInfo.RedirectStandardOutput = true;
|
||||
}
|
||||
|
||||
@@ -699,18 +699,17 @@ namespace Microsoft.PowerShell.Commands
|
||||
{
|
||||
this.WriteVerbose(string.Format(CultureInfo.InvariantCulture, HelpDisplayStrings.OnlineHelpUri, uriToLaunch.OriginalString));
|
||||
System.Diagnostics.Process browserProcess = new System.Diagnostics.Process();
|
||||
browserProcess.StartInfo.UseShellExecute = true;
|
||||
if (Platform.IsWindows)
|
||||
{
|
||||
browserProcess.StartInfo.FileName = uriToLaunch.OriginalString;
|
||||
} else if (Platform.IsOSX) {
|
||||
browserProcess.StartInfo.FileName = "open";
|
||||
browserProcess.StartInfo.Arguments = uriToLaunch.OriginalString;
|
||||
} else if (Platform.IsLinux) {
|
||||
browserProcess.StartInfo.FileName = "xdg-open";
|
||||
browserProcess.StartInfo.Arguments = uriToLaunch.OriginalString;
|
||||
}
|
||||
|
||||
#if LINUX
|
||||
browserProcess.StartInfo.FileName = Platform.IsLinux ? "xdg-open" : /* OS X */ "open";
|
||||
browserProcess.StartInfo.Arguments = uriToLaunch.OriginalString;
|
||||
browserProcess.Start();
|
||||
#elif CORECLR
|
||||
throw new PlatformNotSupportedException();
|
||||
#else
|
||||
browserProcess.StartInfo.FileName = uriToLaunch.OriginalString;
|
||||
browserProcess.Start();
|
||||
#endif
|
||||
}
|
||||
catch (InvalidOperationException ioe)
|
||||
{
|
||||
|
||||
@@ -1333,20 +1333,16 @@ namespace Microsoft.PowerShell.Commands
|
||||
{
|
||||
System.Diagnostics.Process invokeProcess = new System.Diagnostics.Process();
|
||||
|
||||
if (Platform.IsWindows)
|
||||
{
|
||||
System.Diagnostics.Process.Start(path);
|
||||
}
|
||||
else if (Platform.IsOSX) {
|
||||
invokeProcess.StartInfo.FileName = "open";
|
||||
invokeProcess.StartInfo.Arguments = path;
|
||||
}
|
||||
else if (Platform.IsLinux) {
|
||||
invokeProcess.StartInfo.FileName = "xdg-open";
|
||||
invokeProcess.StartInfo.Arguments = path;
|
||||
}
|
||||
|
||||
#if LINUX
|
||||
invokeProcess.StartInfo.FileName = Platform.IsLinux ? "xdg-open" : /* OS X */ "open";
|
||||
invokeProcess.StartInfo.Arguments = path;
|
||||
invokeProcess.Start();
|
||||
#elif CORECLR
|
||||
throw new PlatformNotSupportedException();
|
||||
#else
|
||||
invokeProcess.StartInfo.FileName = path;
|
||||
invokeProcess.Start();
|
||||
#endif
|
||||
}
|
||||
} // InvokeDefaultAction
|
||||
|
||||
|
||||
@@ -2,14 +2,6 @@ using namespace System.Diagnostics
|
||||
|
||||
Describe "Invoke-Item" {
|
||||
|
||||
$tmpDirectory = $TestDrive
|
||||
$testfile = "testfile.txt"
|
||||
$testfolder = "newDirectory"
|
||||
$testlink = "testlink"
|
||||
$FullyQualifiedFile = Join-Path -Path $tmpDirectory -ChildPath $testfile
|
||||
$FullyQualifiedFolder = Join-Path -Path $tmpDirectory -ChildPath $testfolder
|
||||
$FullyQualifiedLink = Join-Path -Path $tmpDirectory -ChildPath $testlink
|
||||
|
||||
function NewProcessStartInfo([string]$CommandLine, [switch]$RedirectStdIn)
|
||||
{
|
||||
return [ProcessStartInfo]@{
|
||||
@@ -39,38 +31,20 @@ Describe "Invoke-Item" {
|
||||
}
|
||||
}
|
||||
|
||||
function Clean-State
|
||||
{
|
||||
if (Test-Path $FullyQualifiedLink)
|
||||
{
|
||||
Remove-Item $FullyQualifiedLink -Force
|
||||
}
|
||||
|
||||
if (Test-Path $FullyQualifiedFile)
|
||||
{
|
||||
Remove-Item $FullyQualifiedFile -Force
|
||||
}
|
||||
|
||||
if (Test-Path $FullyQualifiedFolder)
|
||||
{
|
||||
Remove-Item $FullyQualifiedFolder -Force
|
||||
}
|
||||
}
|
||||
|
||||
BeforeAll {
|
||||
$powershell = Join-Path -Path $PsHome -ChildPath "powershell"
|
||||
Setup -File testfile.txt -Content "Hello World"
|
||||
$testfile = Join-Path $TestDrive testfile.txt
|
||||
}
|
||||
|
||||
#Both tests are pending due to a bug in Invoke-Item on Windows. Fixed for Linux
|
||||
|
||||
It "Should call the function without error" -Pending:$IsWindows {
|
||||
{ New-Item -Name $testfile -Path $tmpDirectory -ItemType file } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should invoke a text file without error" -Pending:$IsWindows {
|
||||
$debugfn = NewProcessStartInfo "-noprofile ""``Invoke-Item $FullyQualifiedFile`n" -RedirectStdIn
|
||||
It "Should invoke a text file without error" -Skip:($IsWindows -and $IsCore) {
|
||||
$debugfn = NewProcessStartInfo "-noprofile ""``Invoke-Item $testfile`n" -RedirectStdIn
|
||||
$process = RunPowerShell $debugfn
|
||||
EnsureChildHasExited $process
|
||||
$process.ExitCode | Should Be 0
|
||||
}
|
||||
}
|
||||
|
||||
It "Should throw not supported on Windows with .NET Core" -Skip:($IsLinux -or $IsOSX -or !$IsCore) {
|
||||
{ Invoke-Item $testfile }| Should Throw "Operation is not supported on this platform."
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user