Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit f7bec37

Browse files
authored
Fix/Update System.Diagnostics.Tests.ProcessTests/ProcessStart_UseShellExecuteTrue_OpenMissingFile_Throw (#27557)
Related to #27547 * Wrapping Process.Start with missing using statement * remove confusing/incorrect comment * Undo add using statement when asserting exception throws * Assert process is not null * HasWindowsShell should also satisfy IsWindows condition * update existingt tests * Removing unnecessary exception variable
1 parent d0cb451 commit f7bec37

File tree

4 files changed

+57
-33
lines changed

4 files changed

+57
-33
lines changed

src/CoreFx.Private.TestUtilities/src/System/PlatformDetection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static partial class PlatformDetection
1818
// do it in a way that failures don't cascade.
1919
//
2020

21-
public static bool HasWindowsShell => IsNotWindowsServerCore && IsNotWindowsNanoServer && IsNotWindowsIoTCore;
21+
public static bool HasWindowsShell => IsWindows && IsNotWindowsServerCore && IsNotWindowsNanoServer && IsNotWindowsIoTCore;
2222
public static bool IsUap => IsInAppContainer || IsNetNative;
2323
public static bool IsFullFramework => RuntimeInformation.FrameworkDescription.StartsWith(".NET Framework", StringComparison.OrdinalIgnoreCase);
2424
public static bool IsNetNative => RuntimeInformation.FrameworkDescription.StartsWith(".NET Native", StringComparison.OrdinalIgnoreCase);

src/System.Diagnostics.Process/tests/ProcessStartInfoTests.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ public void TestCreateNoWindowProperty(bool value)
346346
public void TestWorkingDirectoryProperty()
347347
{
348348
CreateDefaultProcess();
349-
349+
350350
// check defaults
351351
Assert.Equal(string.Empty, _process.StartInfo.WorkingDirectory);
352352

@@ -498,7 +498,7 @@ public void TestEnvironmentVariables_Environment_DataRoundTrips()
498498
Assert.Equal("NewValue", kvpaOrdered[2].Value);
499499

500500
psi.EnvironmentVariables.Remove("NewKey3");
501-
Assert.False(psi.Environment.Contains(new KeyValuePair<string,string>("NewKey3", "NewValue3")));
501+
Assert.False(psi.Environment.Contains(new KeyValuePair<string, string>("NewKey3", "NewValue3")));
502502
}
503503

504504
[Fact]
@@ -603,7 +603,7 @@ public void Verbs_GetWithNonStringValue_ReturnsEmpty()
603603
// modify the registry.
604604
return;
605605
}
606-
606+
607607
tempKey.Key.SetValue("", 123);
608608

609609
var info = new ProcessStartInfo { FileName = FileName };
@@ -626,7 +626,7 @@ public void Verbs_GetWithNoShellSubKey_ReturnsEmpty()
626626
// modify the registry.
627627
return;
628628
}
629-
629+
630630
tempKey.Key.SetValue("", "nosuchshell");
631631

632632
var info = new ProcessStartInfo { FileName = FileName };
@@ -653,7 +653,7 @@ public void Verbs_GetWithSubkeys_ReturnsEmpty()
653653
}
654654

655655
extensionKey.Key.SetValue("", SubKeyValue);
656-
656+
657657
shellKey.Key.CreateSubKey("verb1");
658658
shellKey.Key.CreateSubKey("NEW");
659659
shellKey.Key.CreateSubKey("new");
@@ -746,7 +746,7 @@ public void TestEnvironmentVariablesPropertyUnix()
746746
Assert.Throws<KeyNotFoundException>(() =>
747747
{
748748
string stringout = environmentVariables["NewKey99"];
749-
});
749+
});
750750

751751
//Exception not thrown with invalid key
752752
Assert.Throws<ArgumentNullException>(() =>
@@ -944,7 +944,10 @@ public void StartInfo_WebPage()
944944
FileName = @"http://www.microsoft.com"
945945
};
946946

947-
Process.Start(info); // Returns null after navigating browser
947+
using (var p = Process.Start(info))
948+
{
949+
Assert.NotNull(p);
950+
}
948951
}
949952

950953
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsNanoServer))] // No Notepad on Nano
@@ -1075,7 +1078,6 @@ private static string GetAssociationDetails()
10751078
return sb.ToString();
10761079
}
10771080

1078-
10791081
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsWindowsNanoServer))]
10801082
public void ShellExecute_Nano_Fails_Start()
10811083
{
@@ -1108,6 +1110,7 @@ public static TheoryData<bool> UseShellExecute
11081110
private const int ERROR_FILE_NOT_FOUND = 0x2;
11091111
private const int ERROR_BAD_EXE_FORMAT = 0xC1;
11101112

1113+
[Theory]
11111114
[MemberData(nameof(UseShellExecute))]
11121115
[PlatformSpecific(TestPlatforms.Windows)]
11131116
public void StartInfo_BadVerb(bool useShellExecute)
@@ -1122,6 +1125,7 @@ public void StartInfo_BadVerb(bool useShellExecute)
11221125
Assert.Equal(ERROR_FILE_NOT_FOUND, Assert.Throws<Win32Exception>(() => Process.Start(info)).NativeErrorCode);
11231126
}
11241127

1128+
[Theory]
11251129
[MemberData(nameof(UseShellExecute))]
11261130
[PlatformSpecific(TestPlatforms.Windows)]
11271131
public void StartInfo_BadExe(bool useShellExecute)

src/System.Diagnostics.Process/tests/ProcessTests.Unix.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,25 @@ public void ProcessStart_DirectoryNameInCurDirectorySameAsFileNameInExecDirector
103103
}
104104
}
105105

106+
[Fact]
107+
[OuterLoop]
108+
public void ProcessStart_UseShellExecute_OnUnix_OpenMissingFile_DoesNotThrow()
109+
{
110+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) &&
111+
s_allowedProgramsToRun.FirstOrDefault(program => IsProgramInstalled(program)) == null)
112+
{
113+
return;
114+
}
115+
string fileToOpen = Path.Combine(Environment.CurrentDirectory, "_no_such_file.TXT");
116+
using (var px = Process.Start(new ProcessStartInfo { UseShellExecute = true, FileName = fileToOpen }))
117+
{
118+
Assert.NotNull(px);
119+
px.Kill();
120+
px.WaitForExit();
121+
Assert.True(px.HasExited);
122+
}
123+
}
124+
106125
[Theory, InlineData(true), InlineData(false)]
107126
[OuterLoop("Opens program")]
108127
public void ProcessStart_UseShellExecute_OnUnix_SuccessWhenProgramInstalled(bool isFolder)

src/System.Diagnostics.Process/tests/ProcessTests.cs

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -122,19 +122,19 @@ public void TestEnableRaiseEvents(bool? enable)
122122
[Fact]
123123
public void ProcessStart_TryExitCommandAsFileName_ThrowsWin32Exception()
124124
{
125-
Win32Exception e = Assert.Throws<Win32Exception>(() => Process.Start(new ProcessStartInfo { UseShellExecute = false, FileName = "exit", Arguments = "42" }));
125+
Assert.Throws<Win32Exception>(() => Process.Start(new ProcessStartInfo { UseShellExecute = false, FileName = "exit", Arguments = "42" }));
126126
}
127127

128128
[Fact]
129129
public void ProcessStart_UseShellExecuteFalse_FilenameIsUrl_ThrowsWin32Exception()
130130
{
131-
Win32Exception e = Assert.Throws<Win32Exception>(() => Process.Start(new ProcessStartInfo { UseShellExecute = false, FileName = "https://www.github.com/corefx" }));
131+
Assert.Throws<Win32Exception>(() => Process.Start(new ProcessStartInfo { UseShellExecute = false, FileName = "https://www.github.com/corefx" }));
132132
}
133133

134134
[Fact]
135135
public void ProcessStart_TryOpenFolder_UseShellExecuteIsFalse_ThrowsWin32Exception()
136136
{
137-
Win32Exception e = Assert.Throws<Win32Exception>(() => Process.Start(new ProcessStartInfo { UseShellExecute = false, FileName = Path.GetTempPath() }));
137+
Assert.Throws<Win32Exception>(() => Process.Start(new ProcessStartInfo { UseShellExecute = false, FileName = Path.GetTempPath() }));
138138
}
139139

140140
[Fact]
@@ -176,13 +176,12 @@ public void TestStartWithBadWorkingDirectory()
176176
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.HasWindowsShell))]
177177
[SkipOnTargetFramework(TargetFrameworkMonikers.Uap, "not supported on UAP")]
178178
[OuterLoop("Launches File Explorer")]
179-
public void ProcessStart_UseShellExecuteTrue_OpenMissingFile_Throws()
179+
public void ProcessStart_UseShellExecute_OnWindows_OpenMissingFile_Throws()
180180
{
181181
string fileToOpen = Path.Combine(Environment.CurrentDirectory, "_no_such_file.TXT");
182-
Win32Exception e = Assert.Throws<Win32Exception>(() => Process.Start(new ProcessStartInfo { UseShellExecute = true, FileName = fileToOpen }));
182+
Assert.Throws<Win32Exception>(() => Process.Start(new ProcessStartInfo { UseShellExecute = true, FileName = fileToOpen }));
183183
}
184184

185-
[PlatformSpecific(TestPlatforms.Windows)]
186185
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.HasWindowsShell))]
187186
[InlineData(true)]
188187
[InlineData(false)]
@@ -926,7 +925,7 @@ public void GetProcesses_EmptyMachineName_ThrowsArgumentException()
926925
public void GetProcesses_InvalidMachineName_ThrowsInvalidOperationException()
927926
{
928927
Type exceptionType = PlatformDetection.IsWindows ? typeof(InvalidOperationException) : typeof(PlatformNotSupportedException);
929-
var exception = Assert.Throws(exceptionType, () => Process.GetProcesses(Guid.NewGuid().ToString()));
928+
Assert.Throws(exceptionType, () => Process.GetProcesses(Guid.NewGuid().ToString()));
930929
}
931930

932931
[Fact]
@@ -1616,14 +1615,15 @@ public void Process_StartTest()
16161615
string domain = "thisDomain";
16171616
SecureString password = AsSecureString("Value");
16181617

1619-
Process p = Process.Start(currentProcessName, userName, password, domain); // This writes junk to the Console but with this overload, we can't prevent that.
1620-
Assert.NotNull(p);
1621-
Assert.Equal(currentProcessName, p.StartInfo.FileName);
1622-
Assert.Equal(userName, p.StartInfo.UserName);
1623-
Assert.Same(password, p.StartInfo.Password);
1624-
Assert.Equal(domain, p.StartInfo.Domain);
1625-
1626-
Assert.True(p.WaitForExit(WaitInMS));
1618+
using (Process p = Process.Start(currentProcessName, userName, password, domain)) // This writes junk to the Console but with this overload, we can't prevent that.
1619+
{
1620+
Assert.NotNull(p);
1621+
Assert.Equal(currentProcessName, p.StartInfo.FileName);
1622+
Assert.Equal(userName, p.StartInfo.UserName);
1623+
Assert.Same(password, p.StartInfo.Password);
1624+
Assert.Equal(domain, p.StartInfo.Domain);
1625+
Assert.True(p.WaitForExit(WaitInMS));
1626+
}
16271627
password.Dispose();
16281628
}
16291629

@@ -1637,15 +1637,16 @@ public void Process_StartWithArgumentsTest()
16371637
string domain = Environment.UserDomainName;
16381638
string arguments = "-xml testResults.xml";
16391639
SecureString password = AsSecureString("Value");
1640-
1641-
Process p = Process.Start(currentProcessName, arguments, userName, password, domain);
1642-
Assert.NotNull(p);
1643-
Assert.Equal(currentProcessName, p.StartInfo.FileName);
1644-
Assert.Equal(arguments, p.StartInfo.Arguments);
1645-
Assert.Equal(userName, p.StartInfo.UserName);
1646-
Assert.Same(password, p.StartInfo.Password);
1647-
Assert.Equal(domain, p.StartInfo.Domain);
1648-
p.Kill();
1640+
using (Process p = Process.Start(currentProcessName, arguments, userName, password, domain))
1641+
{
1642+
Assert.NotNull(p);
1643+
Assert.Equal(currentProcessName, p.StartInfo.FileName);
1644+
Assert.Equal(arguments, p.StartInfo.Arguments);
1645+
Assert.Equal(userName, p.StartInfo.UserName);
1646+
Assert.Same(password, p.StartInfo.Password);
1647+
Assert.Equal(domain, p.StartInfo.Domain);
1648+
p.Kill();
1649+
}
16491650
password.Dispose();
16501651
}
16511652

0 commit comments

Comments
 (0)