136 lines
3.5 KiB
C#
136 lines
3.5 KiB
C#
|
|
using Geekeey.Process.Buffered;
|
||
|
|
|
||
|
|
namespace Geekeey.Process.Tests;
|
||
|
|
|
||
|
|
internal sealed class ExecuteTests
|
||
|
|
{
|
||
|
|
[Test]
|
||
|
|
public async Task I_can_execute_a_command_and_get_the_exit_code_and_execution_time()
|
||
|
|
{
|
||
|
|
// Arrange
|
||
|
|
var cmd = new Command(Testing.Fixture.Program.FilePath)
|
||
|
|
.WithArguments(["echo"]);
|
||
|
|
|
||
|
|
// Act
|
||
|
|
var result = await cmd.ExecuteAsync();
|
||
|
|
|
||
|
|
await Assert.That(result.ExitCode).IsZero();
|
||
|
|
|
||
|
|
// Assert
|
||
|
|
using (Assert.Multiple())
|
||
|
|
{
|
||
|
|
await Assert.That(result.ExitCode).IsZero();
|
||
|
|
await Assert.That(result.IsSuccess).IsTrue();
|
||
|
|
await Assert.That(result.RunTime).IsGreaterThan(TimeSpan.Zero);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
[Test]
|
||
|
|
public async Task I_can_execute_a_command_and_get_the_associated_process_id()
|
||
|
|
{
|
||
|
|
// Arrange
|
||
|
|
var cmd = new Command(Testing.Fixture.Program.FilePath)
|
||
|
|
.WithArguments(["echo"]);
|
||
|
|
|
||
|
|
// Act
|
||
|
|
var task = cmd.ExecuteAsync();
|
||
|
|
|
||
|
|
// Assert
|
||
|
|
await Assert.That(task.ProcessId).IsNotZero();
|
||
|
|
|
||
|
|
await task;
|
||
|
|
}
|
||
|
|
|
||
|
|
[Test]
|
||
|
|
public async Task I_can_execute_a_command_with_a_configured_awaiter()
|
||
|
|
{
|
||
|
|
// Arrange
|
||
|
|
var cmd = new Command(Testing.Fixture.Program.FilePath)
|
||
|
|
.WithArguments(["echo"]);
|
||
|
|
|
||
|
|
// Act + Assert
|
||
|
|
await cmd.ExecuteAsync().ConfigureAwait(false);
|
||
|
|
}
|
||
|
|
|
||
|
|
[Test]
|
||
|
|
public async Task I_can_try_to_execute_a_command_and_get_an_error_if_the_target_file_does_not_exist()
|
||
|
|
{
|
||
|
|
// Arrange
|
||
|
|
var cmd = new Command("some_exe_with_does_not_exits");
|
||
|
|
|
||
|
|
// Act + Assert
|
||
|
|
await Assert.That(() => cmd.ExecuteAsync()).Throws<InvalidOperationException>()
|
||
|
|
.WithInnerException();
|
||
|
|
}
|
||
|
|
|
||
|
|
[Test]
|
||
|
|
public async Task I_can_execute_a_command_with_a_custom_working_directory()
|
||
|
|
{
|
||
|
|
// Arrange
|
||
|
|
using var dir = TestTempDirectory.Create();
|
||
|
|
|
||
|
|
var cmd = new Command(Testing.Fixture.Program.FilePath)
|
||
|
|
.WithArguments("cwd")
|
||
|
|
.WithWorkingDirectory(dir.Path);
|
||
|
|
|
||
|
|
// Act
|
||
|
|
var result = await cmd.ExecuteBufferedAsync();
|
||
|
|
|
||
|
|
await Assert.That(result.ExitCode).IsZero();
|
||
|
|
|
||
|
|
// Assert
|
||
|
|
var lines = result.StandardOutput.Split(Environment.NewLine);
|
||
|
|
await Assert.That(lines).Contains(dir.Path);
|
||
|
|
}
|
||
|
|
|
||
|
|
[Test]
|
||
|
|
public async Task I_can_execute_a_command_with_additional_environment_variables()
|
||
|
|
{
|
||
|
|
// Arrange
|
||
|
|
var cmd = new Command(Testing.Fixture.Program.FilePath)
|
||
|
|
.WithArguments(["env", "foo", "bar"])
|
||
|
|
.WithEnvironment(env => env
|
||
|
|
.Set("foo", "hello")
|
||
|
|
.Set("bar", "world"));
|
||
|
|
|
||
|
|
// Act
|
||
|
|
var result = await cmd.ExecuteBufferedAsync();
|
||
|
|
|
||
|
|
await Assert.That(result.ExitCode).IsZero();
|
||
|
|
|
||
|
|
// Assert
|
||
|
|
var lines = result.StandardOutput.Split(Environment.NewLine);
|
||
|
|
await Assert.That(lines).Contains("hello");
|
||
|
|
await Assert.That(lines).Contains("world");
|
||
|
|
}
|
||
|
|
|
||
|
|
[Test]
|
||
|
|
public async Task I_can_execute_a_command_with_some_environment_variables_overwritten()
|
||
|
|
{
|
||
|
|
// Arrange
|
||
|
|
var key = Guid.NewGuid();
|
||
|
|
var variableToKeep = $"GKY_TEST_KEEP_{key}";
|
||
|
|
var variableToOverwrite = $"GKY_TEST_OVERWRITE_{key}";
|
||
|
|
var variableToUnset = $"GKY_TEST_UNSET_{key}";
|
||
|
|
|
||
|
|
using var a = TestEnvironment.Create(variableToKeep, "keep");
|
||
|
|
using var b = TestEnvironment.Create(variableToOverwrite, "overwrite");
|
||
|
|
using var c = TestEnvironment.Create(variableToUnset, "unset");
|
||
|
|
|
||
|
|
var cmd = new Command(Testing.Fixture.Program.FilePath)
|
||
|
|
.WithArguments(["env", variableToKeep, variableToOverwrite, variableToUnset])
|
||
|
|
.WithEnvironment(env => env
|
||
|
|
.Set(variableToOverwrite, "overwritten")
|
||
|
|
.Set(variableToUnset, null));
|
||
|
|
|
||
|
|
// Act
|
||
|
|
var result = await cmd.ExecuteBufferedAsync();
|
||
|
|
|
||
|
|
await Assert.That(result.ExitCode).IsZero();
|
||
|
|
|
||
|
|
// Assert
|
||
|
|
var lines = result.StandardOutput.Split(Environment.NewLine);
|
||
|
|
await Assert.That(lines).Contains("keep");
|
||
|
|
await Assert.That(lines).Contains("overwritten");
|
||
|
|
}
|
||
|
|
}
|