Introduces IProcessImpl.Exit and uses it

Adds IProcessImpl.Exit for controlled process termination, replaces Environment.Exit with IProcessImpl.Exit calls, and adds a test implementation for IProcessImpl.
This commit is contained in:
Kevin Bost
2025-10-02 21:43:02 -07:00
parent a9cf16ab3a
commit 097c1df2cc
5 changed files with 22 additions and 6 deletions

View File

@@ -75,5 +75,7 @@ namespace Velopack.Locators
_logger.Error($"Process {p.Id} has already exited.");
}
}
public void Exit(int exitCode) => Environment.Exit(exitCode);
}
}

View File

@@ -9,5 +9,11 @@ namespace Velopack.Locators
string GetCurrentProcessPath();
uint GetCurrentProcessId();
void StartProcess(string exePath, IEnumerable<string> args, string workDir, bool showWindow);
/// <summary>
/// Exit the current process with the given exit code.
/// </summary>
/// <param name="exitCode">The exit code</param>
void Exit(int exitCode);
}
}

View File

@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using NuGet.Versioning;
@@ -86,6 +86,8 @@ namespace Velopack.Locators
public override IProcessImpl Process => this;
public int? ExitCode { get; private set; }
public override VelopackAsset? GetLatestLocalFullPackage()
{
if (_asset != null) {
@@ -142,5 +144,13 @@ namespace Velopack.Locators
{
new DefaultProcessImpl(Log).StartProcess(exePath, args, workDir, showWindow);
}
public void Exit(int exitCode)
{
if (ExitCode is not null) {
throw new InvalidOperationException("Exit has already been called on this instance.");
}
ExitCode = exitCode;
}
}
}

View File

@@ -16,7 +16,7 @@ namespace Velopack
public void ApplyUpdatesAndRestart(VelopackAsset? toApply, string[]? restartArgs = null)
{
WaitExitThenApplyUpdates(toApply, silent: false, restart: true, restartArgs);
Environment.Exit(0);
Locator.Process.Exit(0);
}
/// <summary>
@@ -29,7 +29,7 @@ namespace Velopack
public void ApplyUpdatesAndExit(VelopackAsset? toApply)
{
WaitExitThenApplyUpdates(toApply, silent: true, restart: false);
Environment.Exit(0);
Locator.Process.Exit(0);
}
/// <summary>

View File

@@ -276,9 +276,7 @@ namespace Velopack
private static void Exit(int code)
{
if (!VelopackRuntimeInfo.InUnitTestRunner) {
Environment.Exit(code);
}
VelopackLocator.GetCurrentOrCreateDefault().Process.Exit(code);
}
}
}