Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyrrrz committed Nov 23, 2023
1 parent 1f57973 commit 2a1a4a0
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 92 deletions.
21 changes: 21 additions & 0 deletions CliWrap.Tests.Dummy/Commands/EnvironmentCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using CliFx;
using CliFx.Attributes;
using CliFx.Infrastructure;

namespace CliWrap.Tests.Dummy.Commands;

[Command("env")]
public class EnvironmentCommand : ICommand
{
[CommandParameter(0)]
public IReadOnlyList<string> Names { get; init; } = Array.Empty<string>();

public async ValueTask ExecuteAsync(IConsole console)
{
foreach (var name in Names)
await console.Output.WriteLineAsync(Environment.GetEnvironmentVariable(name));
}
}
2 changes: 1 addition & 1 deletion CliWrap.Tests.Dummy/Commands/ExitCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace CliWrap.Tests.Dummy.Commands;
[Command("exit")]
public class ExitCommand : ICommand
{
[CommandOption("code")]
[CommandParameter(0)]
public int ExitCode { get; init; }

public ValueTask ExecuteAsync(IConsole console)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,12 @@
using CliFx;
using CliFx.Attributes;
using CliFx.Infrastructure;
using CliWrap.Tests.Dummy.Commands.Shared;

namespace CliWrap.Tests.Dummy.Commands;

[Command("print length stdin")]
public class PrintStdInLengthCommand : ICommand
[Command("length stdin")]
public class LengthStdInCommand : ICommand
{
[CommandOption("target")]
public OutputTarget Target { get; init; } = OutputTarget.StdOut;

public async ValueTask ExecuteAsync(IConsole console)
{
using var buffer = MemoryPool<byte>.Shared.Rent(81920);
Expand All @@ -28,7 +24,6 @@ public async ValueTask ExecuteAsync(IConsole console)
totalBytesRead += bytesRead;
}

foreach (var writer in console.GetWriters(Target))
await writer.WriteLineAsync(totalBytesRead.ToString(CultureInfo.InvariantCulture));
await console.Output.WriteLineAsync(totalBytesRead.ToString(CultureInfo.InvariantCulture));
}
}
19 changes: 0 additions & 19 deletions CliWrap.Tests.Dummy/Commands/PrintEnvironmentVariablesCommand.cs

This file was deleted.

16 changes: 0 additions & 16 deletions CliWrap.Tests.Dummy/Commands/PrintWorkingDirCommand.cs

This file was deleted.

2 changes: 1 addition & 1 deletion CliWrap.Tests.Dummy/Commands/SleepCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace CliWrap.Tests.Dummy.Commands;
[Command("sleep")]
public class SleepCommand : ICommand
{
[CommandOption("duration")]
[CommandParameter(0)]
public TimeSpan Duration { get; init; } = TimeSpan.FromSeconds(1);

public async ValueTask ExecuteAsync(IConsole console)
Expand Down
14 changes: 14 additions & 0 deletions CliWrap.Tests.Dummy/Commands/WorkingDirectoryCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.IO;
using System.Threading.Tasks;
using CliFx;
using CliFx.Attributes;
using CliFx.Infrastructure;

namespace CliWrap.Tests.Dummy.Commands;

[Command("cwd")]
public class WorkingDirectoryCommand : ICommand
{
public async ValueTask ExecuteAsync(IConsole console) =>
await console.Output.WriteLineAsync(Directory.GetCurrentDirectory());
}
38 changes: 14 additions & 24 deletions CliWrap.Tests/CancellationSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public async Task I_can_execute_a_command_and_cancel_it_immediately()
var stdOutBuffer = new StringBuilder();

var cmd =
Cli.Wrap(Dummy.Program.FilePath)
.WithArguments(new[] { "sleep", "--duration", "00:00:20" }) | stdOutBuffer;
Cli.Wrap(Dummy.Program.FilePath).WithArguments(new[] { "sleep", "00:00:20" })
| stdOutBuffer;

// Act
var task = cmd.ExecuteAsync(cts.Token);
Expand All @@ -47,8 +47,8 @@ public async Task I_can_execute_a_command_and_cancel_it_after_a_delay()
var stdOutBuffer = new StringBuilder();

var cmd =
Cli.Wrap(Dummy.Program.FilePath)
.WithArguments(new[] { "sleep", "--duration", "00:00:20" }) | stdOutBuffer;
Cli.Wrap(Dummy.Program.FilePath).WithArguments(new[] { "sleep", "00:00:20" })
| stdOutBuffer;

// Act
var task = cmd.ExecuteAsync(cts.Token);
Expand Down Expand Up @@ -84,8 +84,7 @@ void HandleStdOut(string line)
);

var cmd =
Cli.Wrap(Dummy.Program.FilePath)
.WithArguments(new[] { "sleep", "--duration", "00:00:20" }) | target;
Cli.Wrap(Dummy.Program.FilePath).WithArguments(new[] { "sleep", "00:00:20" }) | target;

// Act
var task = cmd.ExecuteAsync(CancellationToken.None, cts.Token);
Expand All @@ -105,8 +104,7 @@ public async Task I_can_execute_a_command_with_buffering_and_cancel_it_immediate
using var cts = new CancellationTokenSource();
cts.Cancel();

var cmd = Cli.Wrap(Dummy.Program.FilePath)
.WithArguments(new[] { "sleep", "--duration", "00:00:20" });
var cmd = Cli.Wrap(Dummy.Program.FilePath).WithArguments(new[] { "sleep", "00:00:20" });

// Act & assert
var ex = await Assert.ThrowsAnyAsync<OperationCanceledException>(
Expand All @@ -123,8 +121,7 @@ public async Task I_can_execute_a_command_with_buffering_and_cancel_it_after_a_d
using var cts = new CancellationTokenSource();
cts.CancelAfter(TimeSpan.FromSeconds(0.2));

var cmd = Cli.Wrap(Dummy.Program.FilePath)
.WithArguments(new[] { "sleep", "--duration", "00:00:20" });
var cmd = Cli.Wrap(Dummy.Program.FilePath).WithArguments(new[] { "sleep", "00:00:20" });

// Act & assert
var ex = await Assert.ThrowsAnyAsync<OperationCanceledException>(
Expand All @@ -141,8 +138,7 @@ public async Task I_can_execute_a_command_with_buffering_and_cancel_it_gracefull
using var cts = new CancellationTokenSource();
cts.CancelAfter(TimeSpan.FromSeconds(0.2));

var cmd = Cli.Wrap(Dummy.Program.FilePath)
.WithArguments(new[] { "sleep", "--duration", "00:00:20" });
var cmd = Cli.Wrap(Dummy.Program.FilePath).WithArguments(new[] { "sleep", "00:00:20" });

// Act & assert
var ex = await Assert.ThrowsAnyAsync<OperationCanceledException>(
Expand All @@ -165,8 +161,7 @@ public async Task I_can_execute_a_command_as_a_pull_based_event_stream_and_cance
using var cts = new CancellationTokenSource();
cts.Cancel();

var cmd = Cli.Wrap(Dummy.Program.FilePath)
.WithArguments(new[] { "sleep", "--duration", "00:00:20" });
var cmd = Cli.Wrap(Dummy.Program.FilePath).WithArguments(new[] { "sleep", "00:00:20" });

// Act & assert
var ex = await Assert.ThrowsAnyAsync<OperationCanceledException>(async () =>
Expand All @@ -188,8 +183,7 @@ public async Task I_can_execute_a_command_as_a_pull_based_event_stream_and_cance
using var cts = new CancellationTokenSource();
cts.CancelAfter(TimeSpan.FromSeconds(0.2));

var cmd = Cli.Wrap(Dummy.Program.FilePath)
.WithArguments(new[] { "sleep", "--duration", "00:00:20" });
var cmd = Cli.Wrap(Dummy.Program.FilePath).WithArguments(new[] { "sleep", "00:00:20" });

// Act & assert
var ex = await Assert.ThrowsAnyAsync<OperationCanceledException>(async () =>
Expand All @@ -211,8 +205,7 @@ public async Task I_can_execute_a_command_as_a_pull_based_event_stream_and_cance
using var cts = new CancellationTokenSource();
cts.CancelAfter(TimeSpan.FromSeconds(0.2));

var cmd = Cli.Wrap(Dummy.Program.FilePath)
.WithArguments(new[] { "sleep", "--duration", "00:00:20" });
var cmd = Cli.Wrap(Dummy.Program.FilePath).WithArguments(new[] { "sleep", "00:00:20" });

// Act & assert
var ex = await Assert.ThrowsAnyAsync<OperationCanceledException>(async () =>
Expand Down Expand Up @@ -241,8 +234,7 @@ public async Task I_can_execute_a_command_as_a_push_based_event_stream_and_cance
using var cts = new CancellationTokenSource();
cts.Cancel();

var cmd = Cli.Wrap(Dummy.Program.FilePath)
.WithArguments(new[] { "sleep", "--duration", "00:00:20" });
var cmd = Cli.Wrap(Dummy.Program.FilePath).WithArguments(new[] { "sleep", "00:00:20" });

// Act & assert
var ex = await Assert.ThrowsAnyAsync<OperationCanceledException>(
Expand All @@ -268,8 +260,7 @@ public async Task I_can_execute_a_command_as_a_push_based_event_stream_and_cance
using var cts = new CancellationTokenSource();
cts.CancelAfter(TimeSpan.FromSeconds(0.2));

var cmd = Cli.Wrap(Dummy.Program.FilePath)
.WithArguments(new[] { "sleep", "--duration", "00:00:20" });
var cmd = Cli.Wrap(Dummy.Program.FilePath).WithArguments(new[] { "sleep", "00:00:20" });

// Act & assert
var ex = await Assert.ThrowsAnyAsync<OperationCanceledException>(
Expand All @@ -295,8 +286,7 @@ public async Task I_can_execute_a_command_as_a_push_based_event_stream_and_cance
using var cts = new CancellationTokenSource();
cts.CancelAfter(TimeSpan.FromSeconds(0.2));

var cmd = Cli.Wrap(Dummy.Program.FilePath)
.WithArguments(new[] { "sleep", "--duration", "00:00:20" });
var cmd = Cli.Wrap(Dummy.Program.FilePath).WithArguments(new[] { "sleep", "00:00:20" });

// Act & assert
var ex = await Assert.ThrowsAnyAsync<OperationCanceledException>(
Expand Down
26 changes: 8 additions & 18 deletions CliWrap.Tests/EnvironmentSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Threading.Tasks;
using CliWrap.Buffered;
using CliWrap.Tests.Utils;
using CliWrap.Tests.Utils.Extensions;
using FluentAssertions;
using Xunit;

Expand All @@ -17,7 +18,7 @@ public async Task I_can_execute_a_command_with_a_custom_working_directory()
using var dir = TempDir.Create();

var cmd = Cli.Wrap(Dummy.Program.FilePath)
.WithArguments("print cwd")
.WithArguments("cwd")
.WithWorkingDirectory(dir.Path);

// Act
Expand All @@ -34,14 +35,14 @@ public async Task I_can_execute_a_command_with_additional_environment_variables(
var env = new Dictionary<string, string?> { ["foo"] = "bar", ["hello"] = "world" };

var cmd = Cli.Wrap(Dummy.Program.FilePath)
.WithArguments("print env")
.WithArguments(new[] { "env", "foo", "hello" })
.WithEnvironmentVariables(env);

// Act
var result = await cmd.ExecuteBufferedAsync();

// Assert
result.StandardOutput.Trim().Should().ContainAll("[foo] = bar", "[hello] = world");
result.StandardOutput.Should().ConsistOfLines("bar", "world");
}

[Fact(Timeout = 15000)]
Expand All @@ -58,7 +59,9 @@ public async Task I_can_execute_a_command_with_some_environment_variables_overwr
using (TempEnvironmentVariable.Set(variableToUnset, "unset")) // will be unset
{
var cmd = Cli.Wrap(Dummy.Program.FilePath)
.WithArguments("print env")
.WithArguments(
new[] { "env", variableToKeep, variableToOverwrite, variableToUnset }
)
.WithEnvironmentVariables(
e => e.Set(variableToOverwrite, "overwritten").Set(variableToUnset, null)
);
Expand All @@ -67,20 +70,7 @@ public async Task I_can_execute_a_command_with_some_environment_variables_overwr
var result = await cmd.ExecuteBufferedAsync();

// Assert
result
.StandardOutput
.Trim()
.Should()
.ContainAll($"[{variableToKeep}] = keep", $"[{variableToOverwrite}] = overwritten");

result
.StandardOutput
.Trim()
.Should()
.NotContainAny(
$"[{variableToOverwrite}] = overwrite",
$"[{variableToUnset}] = unset"
);
result.StandardOutput.Should().ConsistOfLines("keep", "overwritten");
}
}
}
4 changes: 2 additions & 2 deletions CliWrap.Tests/PipingSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public async Task I_can_execute_a_command_and_pipe_the_stdin_from_another_comman
var cmd =
Cli.Wrap(Dummy.Program.FilePath)
.WithArguments(new[] { "generate binary", "--length", "100000" })
| Cli.Wrap(Dummy.Program.FilePath).WithArguments("print length stdin");
| Cli.Wrap(Dummy.Program.FilePath).WithArguments("length stdin");

// Act
var result = await cmd.ExecuteBufferedAsync();
Expand All @@ -147,7 +147,7 @@ public async Task I_can_execute_a_command_and_pipe_the_stdin_from_a_chain_of_com
| Cli.Wrap(Dummy.Program.FilePath).WithArguments("echo stdin")
| Cli.Wrap(Dummy.Program.FilePath)
.WithArguments(new[] { "echo stdin", "--length", "5" })
| Cli.Wrap(Dummy.Program.FilePath).WithArguments("print length stdin");
| Cli.Wrap(Dummy.Program.FilePath).WithArguments("length stdin");

// Act
var result = await cmd.ExecuteBufferedAsync();
Expand Down
22 changes: 22 additions & 0 deletions CliWrap.Tests/Utils/Extensions/AssertionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using FluentAssertions;
using FluentAssertions.Primitives;

namespace CliWrap.Tests.Utils.Extensions;

internal static class AssertionExtensions
{
public static void ConsistOfLines(
this StringAssertions assertions,
IEnumerable<string> lines
) =>
assertions
.Subject
.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries)
.Should()
.Equal(lines);

public static void ConsistOfLines(this StringAssertions assertions, params string[] lines) =>
assertions.ConsistOfLines((IEnumerable<string>)lines);
}
6 changes: 3 additions & 3 deletions CliWrap.Tests/ValidationSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class ValidationSpecs
public async Task I_can_try_to_execute_a_command_and_get_an_error_if_it_returns_a_non_zero_exit_code()
{
// Arrange
var cmd = Cli.Wrap(Dummy.Program.FilePath).WithArguments(new[] { "exit", "--code", "1" });
var cmd = Cli.Wrap(Dummy.Program.FilePath).WithArguments(new[] { "exit", "1" });

// Act & assert
var ex = await Assert.ThrowsAsync<CommandExecutionException>(
Expand All @@ -34,7 +34,7 @@ public async Task I_can_try_to_execute_a_command_and_get_an_error_if_it_returns_
public async Task I_can_try_to_execute_a_command_with_buffering_and_get_a_detailed_error_if_it_returns_a_non_zero_exit_code()
{
// Arrange
var cmd = Cli.Wrap(Dummy.Program.FilePath).WithArguments(new[] { "exit", "--code", "1" });
var cmd = Cli.Wrap(Dummy.Program.FilePath).WithArguments(new[] { "exit", "1" });

// Act & assert
var ex = await Assert.ThrowsAsync<CommandExecutionException>(
Expand All @@ -53,7 +53,7 @@ public async Task I_can_execute_a_command_without_validating_the_exit_code()
{
// Arrange
var cmd = Cli.Wrap(Dummy.Program.FilePath)
.WithArguments(new[] { "exit", "--code", "1" })
.WithArguments(new[] { "exit", "1" })
.WithValidation(CommandResultValidation.None);

// Act
Expand Down

0 comments on commit 2a1a4a0

Please sign in to comment.