This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improving unix implementation for UseShellExecute
Making sure we are coherent with mono implementation Added unit tests per linux and osx
- Loading branch information
1 parent
43c08c9
commit 75f34a5
Showing
8 changed files
with
264 additions
and
20 deletions.
There are no files selected for viewing
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
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
86 changes: 86 additions & 0 deletions
86
src/System.Diagnostics.Process/tests/ProcessTests.Linux.cs
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 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Runtime.InteropServices; | ||
using System.Threading; | ||
using System.Security; | ||
using Xunit; | ||
using Xunit.NetCore.Extensions; | ||
|
||
namespace System.Diagnostics.Tests | ||
{ | ||
public partial class ProcessTests : ProcessTestBase | ||
{ | ||
[Fact] | ||
public void ProcessStart_UseShellExecuteTrue_OpenFile_ThrowsIfNoDefaultProgramInstalledSucceedsOtherwise() | ||
{ | ||
string fileToOpen = GetTestFilePath() + ".txt"; | ||
File.WriteAllText(fileToOpen, $"{nameof(ProcessStart_UseShellExecuteTrue_OpenFile_ThrowsIfNoDefaultProgramInstalledSucceedsOtherwise)}"); | ||
|
||
string[] allowedProgramsToRun = { "xdg-open", "gnome-open", "kfmclient" }; | ||
foreach (var program in allowedProgramsToRun) | ||
{ | ||
if (IsProgramInstalled(program)) | ||
{ | ||
var startInfo = new ProcessStartInfo { UseShellExecute = true, FileName = fileToOpen }; | ||
using (var px = Process.Start(startInfo)) | ||
{ | ||
if (px != null) | ||
{ | ||
Assert.Equal(program, px.ProcessName); | ||
px.Kill(); | ||
px.WaitForExit(); | ||
Assert.True(px.HasExited); | ||
Assert.Equal(137, px.ExitCode); // 137 means the process was killed | ||
} | ||
} | ||
return; | ||
} | ||
} | ||
|
||
Win32Exception e = Assert.Throws<Win32Exception>(() => Process.Start(new ProcessStartInfo { UseShellExecute = true, FileName = fileToOpen })); | ||
} | ||
|
||
[Fact] | ||
[OuterLoop("Returns failure exit code when default program, xdg-open, is installed")] | ||
public void ProcessStart_UseShellExecuteTrue_OpenMissingFile_DefaultProgramInstalled_ReturnsFailureExitCode() | ||
{ | ||
string fileToOpen = Path.Combine(Environment.CurrentDirectory, "_no_such_file.TXT"); | ||
using (var p = Process.Start(new ProcessStartInfo { UseShellExecute = true, FileName = fileToOpen })) | ||
{ | ||
if (p != null) | ||
{ | ||
Assert.Equal("xdg-open", p.ProcessName); | ||
p.WaitForExit(); | ||
Assert.True(p.HasExited); | ||
Assert.Equal(2, p.ExitCode); | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Gets the path to the program | ||
/// </summary> | ||
/// <param name="program"></param> | ||
/// <returns></returns> | ||
private bool IsProgramInstalled(string program) | ||
{ | ||
string path = Environment.GetEnvironmentVariable("PATH"); | ||
string[] dirs = path.Split(':'); | ||
foreach (var dir in dirs) | ||
{ | ||
string[] files = Directory.GetFiles(dir, program); | ||
if (files.Length != 0) | ||
{ | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} | ||
} |
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,58 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Runtime.InteropServices; | ||
using System.Threading; | ||
using System.Security; | ||
using Xunit; | ||
using Xunit.NetCore.Extensions; | ||
|
||
namespace System.Diagnostics.Tests | ||
{ | ||
public partial class ProcessTests : ProcessTestBase | ||
{ | ||
[Fact] | ||
[OuterLoop("Launches default application")] | ||
public void TestWithFilename_ShouldUseOpenWithDefaultApp() | ||
{ | ||
string file = Path.Combine(Environment.CurrentDirectory, "..", "..", "..", "..", "PATENTS.TXT"); | ||
using (var px = Process.Start("/usr/bin/open", file)) | ||
{ | ||
Assert.False(px.HasExited); | ||
px.WaitForExit(); | ||
Assert.True(px.HasExited); | ||
Assert.Equal(0, px.ExitCode); // Exit Code 0 from open means success | ||
} | ||
} | ||
|
||
[Fact] | ||
[OuterLoop("Launches default browser")] | ||
public void TestWithUrl_ShouldUseOpenWithDefaultApp() | ||
{ | ||
using (var px = Process.Start("/usr/bin/open", "http://www.google.com")) | ||
{ | ||
Assert.False(px.HasExited); | ||
px.WaitForExit(); | ||
Assert.True(px.HasExited); | ||
Assert.Equal(0, px.ExitCode); // Exit Code 0 from open means success | ||
} | ||
} | ||
|
||
[Fact] | ||
// TODO fix behavior to ThrowWin32Exception instead? | ||
public void ProcessStart_TryOpenFileThatDoesntExist_UseShellExecuteIsTrue_ThrowsWin32Exception() | ||
{ | ||
string file = Path.Combine(Environment.CurrentDirectory, "..", "..", "..", "..", "_no_such_file.TXT"); | ||
using (var p = Process.Start(new ProcessStartInfo { UseShellExecute = true, FileName = file })) | ||
{ | ||
Assert.True(p.WaitForExit(WaitInMS)); | ||
Assert.Equal(1, p.ExitCode); // Exit Code 1 from open means 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
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