Skip to content

Commit d7689ac

Browse files
committed
Adde mutex when executing PowerShell scripts.
1 parent 9076301 commit d7689ac

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

src/Chapter10.Tests/Listing10.17.RegisteringAFinalizerWithProcessExit.Tests.cs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter10.Listing10_17.Tests;
88
[TestClass]
99
public partial class DisposeTests
1010
{
11+
private static Mutex Mutext { get; } = new (false, typeof(DisposeTests).FullName);
12+
1113
public TestContext TestContext { get; set; } = null!; // Auto-initialized by MSTest.
1214

1315
static string Ps1DirectoryPath { get; } =
@@ -24,8 +26,6 @@ public partial class DisposeTests
2426
public static void ClassInitialize(TestContext testContext)
2527
{
2628
string psOutput;
27-
using Mutex mutext = new(true, nameof(Listing10_17));
28-
2929
// Clean up at the start in case the class cleanup doesn't run (due to debug for example)
3030
string testStage = "cleanup";
3131
Assert.AreEqual<int>(0, RunPowerShellScript(testStage, out psOutput),
@@ -38,13 +38,15 @@ public static void ClassInitialize(TestContext testContext)
3838
Path.Join(Ps1DirectoryPath, ProjectName, $"{ProjectName}.csproj");
3939
Assert.IsTrue(File.Exists(projectFilePath),
4040
$"The expected project file, '{projectFilePath}', was not created.");
41+
4142
}
4243

4344
[ClassCleanup]
4445
public static void ClassCleanup()
4546
{
4647
// No return check since an exception here will be ignored.
4748
RunPowerShellScript("cleanup", out string _);
49+
Mutext.Dispose();
4850
}
4951

5052
[DataTestMethod]
@@ -53,18 +55,25 @@ public static void ClassCleanup()
5355
[DataRow("gc", GCCalled, DisplayName = "Garbage Collected called")]
5456
public void FinalizerRunsAsPredicted_ConsoleOutputIsInOrder(string finalizerOrderOption, string expectedOutput)
5557
{
56-
using Mutex mutext = new(true, nameof(Listing10_17));
57-
int traceValue = 0;
58-
string testStatus = "run";
59-
60-
TestContext.WriteLine($"Ps1Path = '{Path.GetFullPath(Ps1Path)}'");
61-
int exitCode = RunPowerShellScript(
62-
testStatus, finalizerOrderOption, traceValue, out string psOutput);
63-
64-
Assert.AreEqual(0, exitCode, $"PowerShell Output: {psOutput}");
65-
66-
Assert.AreEqual<string>(RemoveWhiteSpace(expectedOutput), RemoveWhiteSpace(psOutput),
67-
$"Unexpected output from '{Ps1Path} {traceValue} {finalizerOrderOption} {testStatus}:{Environment.NewLine}{psOutput}");
58+
try
59+
{
60+
Mutext.WaitOne();
61+
int traceValue = 0;
62+
string testStatus = "run";
63+
64+
TestContext.WriteLine($"Ps1Path = '{Path.GetFullPath(Ps1Path)}'");
65+
int exitCode = RunPowerShellScript(
66+
testStatus, finalizerOrderOption, traceValue, out string psOutput);
67+
68+
Assert.AreEqual(0, exitCode, $"PowerShell Output: {psOutput}");
69+
70+
Assert.AreEqual<string>(RemoveWhiteSpace(expectedOutput), RemoveWhiteSpace(psOutput),
71+
$"Unexpected output from '{Ps1Path} {traceValue} {finalizerOrderOption} {testStatus}:{Environment.NewLine}{psOutput}");
72+
}
73+
finally
74+
{
75+
Mutext.ReleaseMutex();
76+
}
6877
}
6978

7079
private static int RunPowerShellScript(string testStage, out string psOutput) =>

src/Shared/Tests/PowerShellTestUtilities.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Shared.Tests
55
{
66
public class PowerShellTestUtilities
77
{
8+
private readonly static Mutex Mutext = new (false, typeof(PowerShellTestUtilities).FullName);
9+
810
public static bool WindowsEnvironment()
911
{
1012
return RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
1113
}
12-
13-
public static Lazy<string?> _PowerShellCommand = new(() =>
14+
15+
private readonly static Lazy<string?> _PowerShellCommand = new(() =>
1416
{
1517
string? powershellCommand = null;
1618
// Verify that the PowerShell command executes successfully.
@@ -35,14 +37,13 @@ public static bool WindowsEnvironment()
3537
return powershellCommand;
3638
});
3739
public static int RunPowerShellScript(string scriptPath, string arguments) =>
38-
RunPowerShellScript(scriptPath, arguments, out string psOutput);
40+
RunPowerShellScript(scriptPath, arguments, out string _);
3941

4042
public static int RunPowerShellScript(string scriptPath, string arguments, out string psOutput)
4143
{
42-
using Mutex mutext = new(false, typeof(PowerShellTestUtilities).FullName);
4344
try
4445
{
45-
mutext.WaitOne();
46+
Mutext.WaitOne();
4647
if (PowerShellCommand is null)
4748
{
4849
throw new InvalidOperationException("PowerShell is not installed");
@@ -71,7 +72,7 @@ public static int RunPowerShellScript(string scriptPath, string arguments, out s
7172
}
7273
finally
7374
{
74-
mutext.ReleaseMutex();
75+
Mutext.ReleaseMutex();
7576
}
7677
}
7778

0 commit comments

Comments
 (0)