Skip to content

Commit

Permalink
feat: add InvocationResult return type to Terminal class
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusrodrigues committed Oct 11, 2024
1 parent 8b81e6e commit aea21a4
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 62 deletions.
6 changes: 3 additions & 3 deletions src/Dotnet.Installer.Core/Services/Contracts/ISnapService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ namespace Dotnet.Installer.Core.Services.Contracts;
public interface ISnapService
{
bool IsSnapInstalled(string name, CancellationToken cancellationToken = default);
Task<InvocationResult> Install(string name, CancellationToken cancellationToken = default);
Task<InvocationResult> Remove(string name, bool purge = false, CancellationToken cancellationToken = default);
}
Task<Terminal.InvocationResult> Install(string name, CancellationToken cancellationToken = default);
Task<Terminal.InvocationResult> Remove(string name, bool purge = false, CancellationToken cancellationToken = default);
}
10 changes: 5 additions & 5 deletions src/Dotnet.Installer.Core/Services/Contracts/ISystemdService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ namespace Dotnet.Installer.Core.Services.Contracts;

public interface ISystemdService
{
Task<InvocationResult> DaemonReload();
Task<InvocationResult> EnableUnit(string unit);
Task<InvocationResult> DisableUnit(string unit);
Task<InvocationResult> StartUnit(string unit);
Task<InvocationResult> StopUnit(string unit);
Task<Terminal.InvocationResult> DaemonReload();
Task<Terminal.InvocationResult> EnableUnit(string unit);
Task<Terminal.InvocationResult> DisableUnit(string unit);
Task<Terminal.InvocationResult> StartUnit(string unit);
Task<Terminal.InvocationResult> StopUnit(string unit);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,21 @@ public bool IsSnapInstalled(string name, CancellationToken cancellationToken = d
return Directory.Exists(Path.Combine("/", "snap", name));
}

public async Task<InvocationResult> Install(string name, CancellationToken cancellationToken = default)
public Task<Terminal.InvocationResult> Install(string name, CancellationToken cancellationToken = default)
{
var result = await Terminal.Invoke("snap", "install", name);
return new InvocationResult(result == 0, "", "");
return Terminal.Invoke("snap", "install", name);
}

public async Task<InvocationResult> Remove(string name, bool purge = false, CancellationToken cancellationToken = default)
public Task<Terminal.InvocationResult> Remove(string name, bool purge = false, CancellationToken cancellationToken = default)
{
var arguments = new List<string>
{
"remove"
};

if (purge) arguments.Add("--purge");
arguments.Add(name);

var result = await Terminal.Invoke("snap", arguments.ToArray());
return new InvocationResult(result == 0, "", "");

return Terminal.Invoke("snap", arguments.ToArray());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,29 @@ public class SystemdService : ISystemdService
RedirectStandardError = true,
RedirectStandardOutput = true,
};
public async Task<InvocationResult> DaemonReload()

public Task<Terminal.InvocationResult> DaemonReload()
{
var result = await Terminal.Invoke("systemctl", _globalSystemdOptions, "daemon-reload");
return new InvocationResult(result == 0, string.Empty, string.Empty);
return Terminal.Invoke("systemctl", _globalSystemdOptions, "daemon-reload");
}

public async Task<InvocationResult> EnableUnit(string unit)
public Task<Terminal.InvocationResult> EnableUnit(string unit)
{
var result = await Terminal.Invoke("systemctl", _globalSystemdOptions, "enable", unit);
return new InvocationResult(result == 0, string.Empty, string.Empty);
return Terminal.Invoke("systemctl", _globalSystemdOptions, "enable", unit);
}

public async Task<InvocationResult> DisableUnit(string unit)
public Task<Terminal.InvocationResult> DisableUnit(string unit)
{
var result = await Terminal.Invoke("systemctl", _globalSystemdOptions, "disable", unit);
return new InvocationResult(result == 0, string.Empty, string.Empty);
return Terminal.Invoke("systemctl", _globalSystemdOptions, "disable", unit);
}

public async Task<InvocationResult> StartUnit(string unit)
public Task<Terminal.InvocationResult> StartUnit(string unit)
{
var result = await Terminal.Invoke("systemctl", _globalSystemdOptions, "start", unit);
return new InvocationResult(result == 0, string.Empty, string.Empty);
return Terminal.Invoke("systemctl", _globalSystemdOptions, "start", unit);
}

public async Task<InvocationResult> StopUnit(string unit)
public Task<Terminal.InvocationResult> StopUnit(string unit)
{
var result = await Terminal.Invoke("systemctl", _globalSystemdOptions, "stop", unit);
return new InvocationResult(result == 0, string.Empty, string.Empty);
return Terminal.Invoke("systemctl", _globalSystemdOptions, "stop", unit);
}
}
8 changes: 0 additions & 8 deletions src/Dotnet.Installer.Core/Types/InvocationResult.cs

This file was deleted.

38 changes: 35 additions & 3 deletions src/Dotnet.Installer.Core/Types/Terminal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ namespace Dotnet.Installer.Core.Types;

public static class Terminal
{
public static async Task<int> Invoke(string program, params string[] arguments)
public static async Task<InvocationResult> Invoke(string program, params string[] arguments)
{
return await Invoke(program, options: null, arguments);
}

public static async Task<int> Invoke(string program, InvocationOptions? options = default,
public static async Task<InvocationResult> Invoke(string program, InvocationOptions? options = default,
params string[] arguments)
{
options ??= InvocationOptions.Default;
Expand All @@ -28,7 +28,16 @@ public static async Task<int> Invoke(string program, InvocationOptions? options
process.Start();
await process.WaitForExitAsync();

return process.ExitCode;
return new InvocationResult
{
ExitCode = process.ExitCode,
RedirectedStandardError = options.RedirectStandardError,
RedirectedStandardOutput = options.RedirectStandardOutput,
StandardError = options.RedirectStandardError ?
await process.StandardError.ReadToEndAsync() : default,
StandardOutput = options.RedirectStandardOutput ?
await process.StandardOutput.ReadToEndAsync() : default
};
}

public class InvocationOptions
Expand All @@ -38,4 +47,27 @@ public class InvocationOptions
public bool RedirectStandardOutput { get; set; } = false;
public bool RedirectStandardError { get; set; } = false;
}

public class InvocationResult
{
public InvocationResult()
{ }

public InvocationResult(int exitCode, string standardOutput, string standardError)
{
ExitCode = exitCode;
StandardOutput = standardOutput;
StandardError = standardError;

RedirectedStandardError = true;
RedirectedStandardOutput = true;
}

public bool IsSuccess => ExitCode == 0;
public int ExitCode { get; init; }
public bool RedirectedStandardOutput { get; init; }
public bool RedirectedStandardError { get; init; }
public string? StandardOutput { get; init; }
public string? StandardError { get; init; }
}
}
36 changes: 18 additions & 18 deletions tests/Dotnet.Installer.Core.Tests/Models/ComponentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ public async Task Install_WithValidVersions_ShouldInvokeInstallationStartedEvent
var systemDService = new Mock<ISystemdService>();

snapService.Setup(s => s.Install(It.IsAny<string>(), CancellationToken.None))
.ReturnsAsync(new InvocationResult(
isSuccess: true, standardOutput: string.Empty, standardError: string.Empty));
.ReturnsAsync(new Terminal.InvocationResult(
exitCode: 0, standardOutput: string.Empty, standardError: string.Empty));

systemDService.Setup(s => s.DaemonReload())
.ReturnsAsync(new InvocationResult(true, string.Empty, string.Empty));
.ReturnsAsync(new Terminal.InvocationResult(0, string.Empty, string.Empty));
systemDService.Setup(s => s.EnableUnit(It.IsAny<string>()))
.ReturnsAsync(new InvocationResult(true, string.Empty, string.Empty));
.ReturnsAsync(new Terminal.InvocationResult(0, string.Empty, string.Empty));
systemDService.Setup(s => s.StartUnit(It.IsAny<string>()))
.ReturnsAsync(new InvocationResult(true, string.Empty, string.Empty));
.ReturnsAsync(new Terminal.InvocationResult(0, string.Empty, string.Empty));

// Act
var evt = await Assert.RaisesAsync<InstallationStartedEventArgs>(
Expand Down Expand Up @@ -73,15 +73,15 @@ public async Task Install_WithValidVersions_ShouldInvokeInstallationFinishedEven
var systemDService = new Mock<ISystemdService>();

snapService.Setup(s => s.Install(It.IsAny<string>(), CancellationToken.None))
.ReturnsAsync(new InvocationResult(
isSuccess: true, standardOutput: string.Empty, standardError: string.Empty));
.ReturnsAsync(new Terminal.InvocationResult(
exitCode: 0, standardOutput: string.Empty, standardError: string.Empty));

systemDService.Setup(s => s.DaemonReload())
.ReturnsAsync(new InvocationResult(true, string.Empty, string.Empty));
.ReturnsAsync(new Terminal.InvocationResult(0, string.Empty, string.Empty));
systemDService.Setup(s => s.EnableUnit(It.IsAny<string>()))
.ReturnsAsync(new InvocationResult(true, string.Empty, string.Empty));
.ReturnsAsync(new Terminal.InvocationResult(0, string.Empty, string.Empty));
systemDService.Setup(s => s.StartUnit(It.IsAny<string>()))
.ReturnsAsync(new InvocationResult(true, string.Empty, string.Empty));
.ReturnsAsync(new Terminal.InvocationResult(0, string.Empty, string.Empty));

// Act
var evt = await Assert.RaisesAsync<InstallationFinishedEventArgs>(
Expand Down Expand Up @@ -146,15 +146,15 @@ public async Task Install_WithMultipleDependencies_ShouldTraverseAndInstallDepen
});

snapService.Setup(s => s.Install(It.IsAny<string>(), CancellationToken.None))
.ReturnsAsync(new InvocationResult(
isSuccess: true, standardOutput: string.Empty, standardError: string.Empty));
.ReturnsAsync(new Terminal.InvocationResult(
exitCode: 0, standardOutput: string.Empty, standardError: string.Empty));

systemDService.Setup(s => s.DaemonReload())
.ReturnsAsync(new InvocationResult(true, string.Empty, string.Empty));
.ReturnsAsync(new Terminal.InvocationResult(0, string.Empty, string.Empty));
systemDService.Setup(s => s.EnableUnit(It.IsAny<string>()))
.ReturnsAsync(new InvocationResult(true, string.Empty, string.Empty));
.ReturnsAsync(new Terminal.InvocationResult(0, string.Empty, string.Empty));
systemDService.Setup(s => s.StartUnit(It.IsAny<string>()))
.ReturnsAsync(new InvocationResult(true, string.Empty, string.Empty));
.ReturnsAsync(new Terminal.InvocationResult(0, string.Empty, string.Empty));

// Act
await component1.Install(fileService.Object, manifestService.Object, snapService.Object, systemDService.Object,
Expand Down Expand Up @@ -201,11 +201,11 @@ public async Task Uninstall_WithInstalledComponent_ShouldUninstall()
});

systemDService.Setup(s => s.DaemonReload())
.ReturnsAsync(new InvocationResult(true, string.Empty, string.Empty));
.ReturnsAsync(new Terminal.InvocationResult(0, string.Empty, string.Empty));
systemDService.Setup(s => s.DisableUnit(It.IsAny<string>()))
.ReturnsAsync(new InvocationResult(true, string.Empty, string.Empty));
.ReturnsAsync(new Terminal.InvocationResult(0, string.Empty, string.Empty));
systemDService.Setup(s => s.StopUnit(It.IsAny<string>()))
.ReturnsAsync(new InvocationResult(true, string.Empty, string.Empty));
.ReturnsAsync(new Terminal.InvocationResult(0, string.Empty, string.Empty));

installedComponents.Add(component1);

Expand Down

0 comments on commit aea21a4

Please sign in to comment.