Fixed failing tests (from inattention to detail refactoring)
MODIFY
src/Chapter10.Tests/Listing10.23.RegisteringAFinalizerWithProcessExit.Tests.cs
src/Chapter10.Tests/Listing10.23.RegisteringAFinalizerWithProcessExit.Tests.cs
@@ -11,8 +11,28 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter10.Listing10_23.Tests
[TestClass]
public class DisposeTests
{
+ public TestContext TestContext { get; set; } = null!; // Auto-initialized by MSTest.
+
static string Ps1Path { get; } = Path.GetFullPath("../../../../Chapter10/Listing10.23.RegisteringAFinalizerWithProcessExit.ps1", Environment.CurrentDirectory);
+ [ClassInitialize]
+ public static void ClassInitialize(TestContext testContext)
+ {
+ string testStatus = "create";
+ Assert.AreEqual<int>(0, PowerShellTestUtilities.RunPowerShellScript(
+ Ps1Path, $"0 test {testStatus}", out string psOutput));
+ testContext.WriteLine(psOutput);
+ Assert.IsTrue(File.Exists(@"C:\Git\EssentialCSharp\SCC\src\Chapter10\ProcessExitTestProgram\ProcessExitTestProgram.csproj"));
+ }
+
+ [ClassCleanup]
+ public static void RemoveProcessExitProj()
+ {
+ string testStatus = "cleanup";
+ Assert.AreEqual<int>(0, PowerShellTestUtilities.RunPowerShellScript(
+ Ps1Path, $"0 test {testStatus}", out string _));
+ }
+
[DataTestMethod]
[DataRow("processExit", FinalizerRegisteredWithProcessExit, DisplayName = "Finalizer Registered With ProcessExit")]
[DataRow("dispose", DisposeManuallyCalledExpectedOutput, DisplayName = "Dispose called before ProcessExit does finalizer")]
@@ -22,12 +42,15 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter10.Listing10_23.Tests
string traceValue = "0";
string testStatus = "run";
+ TestContext.WriteLine($"Ps1Path = '{Path.GetFullPath(Ps1Path)}'");
+
int exitCode = PowerShellTestUtilities.RunPowerShellScript(
Ps1Path, $"{traceValue} {finalizerOrderOption} {testStatus}", out string psOutput);
- Assert.AreEqual(0, exitCode);
+ Assert.AreEqual(0, exitCode, $"PowerShell Output: {psOutput}");
- Assert.AreEqual<string>(RemoveWhiteSpace(expectedOutput), RemoveWhiteSpace(psOutput));
+ Assert.AreEqual<string>(RemoveWhiteSpace(expectedOutput), RemoveWhiteSpace(psOutput),
+ $"Unexpected output from '{Ps1Path} {traceValue} {finalizerOrderOption} {testStatus}:{Environment.NewLine}{psOutput}");
}
public const string DisposeManuallyCalledExpectedOutput =

MODIFY
src/Chapter10/Listing10.23.RegisteringAFinalizerWithProcessExit.ps1
src/Chapter10/Listing10.23.RegisteringAFinalizerWithProcessExit.ps1
@@ -11,57 +11,62 @@ if('traceLevel' -notin $PSBoundParameters.Keys) {
- 0: Turn script tracing off.
- 1: Trace script lines as they run.
- 2: Trace script lines, variable assignments, function calls, and scripts.
-
"@
}
#$LibraryProjectName = 'ProcessExit'
$ConsoleProgramProjectName = 'ProcessExitTestProgram'
-if($testStatus -eq "create"){
+[string]$projectDirectory = (Join-Path $PSScriptRoot $ConsoleProgramProjectName)
+
+switch ($testStatus) {
+ "create" {
try {
- Get-Item "$PSScriptRoot/$ConsoleProgramProjectName" -ErrorAction Ignore | Remove-Item -Recurse
+ Write-Host "`$projectDirectory: $projectDirectory"
+ Get-Item $projectDirectory -ErrorAction Ignore | Remove-Item -Recurse
Set-PSDebug -Trace $traceLevel
- dotnet new Console --output "$PSScriptRoot/$ConsoleProgramProjectName"
- Set-Content $PSScriptRoot/$ConsoleProgramProjectName/Directory.Build.props '<Project><PropertyGroup><Nullable>enable</Nullable></PropertyGroup></Project>'
+ dotnet new Console --output $projectDirectory
+ Set-Content (Join-Path $projectDirectory 'Directory.Build.props') '<Project><PropertyGroup><Nullable>enable</Nullable></PropertyGroup></Project>'
$SutCSFile = split-path -leaf $MyInvocation.MyCommand.Definition
- $SutCSFile = "$PSScriptRoot/$([IO.Path]::GetFileNameWithoutExtension($SutCSFile)).cs"
+ $SutCSFile = Join-Path $PSScriptRoot "$([IO.Path]::GetFileNameWithoutExtension($SutCSFile)).cs"
if(-not (Test-Path $SutCSFile)) { throw "Unable to find the file with the type to export ('$SutCSFile')"}
$codeListing = @('namespace ProcessExitTestProgram') + (
Get-Content $SutCSFile | Where-Object { $_ -notlike 'namespace *'})
- $codeListing > "$PSScriptRoot/$ConsoleProgramProjectName/Program.cs"
- Get-Content "$PSScriptRoot/$ConsoleProgramProjectName/Program.cs" # Display the listing
+ $codeListing > (Join-Path $projectDirectory 'Program.cs')
+ Get-Content (Join-Path $projectDirectory 'Program.cs') # Display the listing
}catch{
- Write-Error "Unable to create project"
+ throw "Unable to create project"
}
-
}
+ "cleanup" {
+ Get-Item $projectDirectory -ErrorAction Ignore | Remove-Item -Recurse
+ }
+ "run" {
+ try{
+ [string]$projectFile= Join-Path $projectDirectory "$ConsoleProgramProjectName.csproj"
-if($testStatus -eq "cleanup"){
- Get-Item "$PSScriptRoot/$ConsoleProgramProjectName" -ErrorAction Ignore | Remove-Item -Recurse
+ if(!(Test-Path $projectFile)) {
+ Write-Error "Unable to find project file ($projectFile)"
}
-if($testStatus -eq "run"){
- try{
if($finalizerOption -eq 'dispose'){
- dotnet run --project "$PSScriptRoot/$ConsoleProgramProjectName/$ConsoleProgramProjectName.csproj" -- -dispose
+ dotnet run --project $projectFile -- -dispose
}
elseif($finalizerOption -eq 'gc'){
- dotnet run --project "$PSScriptRoot/$ConsoleProgramProjectName/$ConsoleProgramProjectName.csproj" -- -gc
+ dotnet run --project $projectFile -- -gc
}
elseif($finalizerOption -eq 'processExit'){
- dotnet run --project "$PSScriptRoot/$ConsoleProgramProjectName/$ConsoleProgramProjectName.csproj"
+ dotnet run --project $projectFile
}
else{
Write-Error "finalizerOption: $finalizerOption not valid with the testStatus: run"
}
-
}
finally {
Set-PSDebug -Off
-
+ }
}
}
\ No newline at end of file

Fixed failing tests (from inattention to detail refactoring)