This repository has been archived on 2023-02-02. You can view files and clone it, but cannot push or open issues or pull requests.
mfgames-toolbuilder-cil/tests/MfGames.ToolBuilder.Tests/SampleToolTests.cs

106 lines
3.4 KiB
C#
Raw Normal View History

using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using CliWrap;
using CliWrap.Exceptions;
2021-12-07 23:34:02 +00:00
using MfGames.IO.Extensions;
using Xunit;
namespace MfGames.ToolBuilder.Tests
{
/// <summary>
/// Tests the SampleTool in the tests directory to make sure the
/// basic functionality is correct.
/// </summary>
public class SampleToolTests
{
[Fact]
2021-11-30 04:24:20 +00:00
public void CrashCommandFails()
{
// Run the executable using CliWrap.
2021-12-07 23:34:02 +00:00
FileInfo projectFile = this.GetProjectFile();
StringBuilder output = new();
CancellationToken cancellationToken =
new CancellationTokenSource(TimeSpan.FromSeconds(20))
.Token;
2021-11-30 04:24:20 +00:00
Task<CommandExecutionException> exception =
Assert.ThrowsAsync<CommandExecutionException>(
async () => await Cli.Wrap("dotnet")
.WithArguments(
new[]
{
"run",
"--project", projectFile.FullName,
"--",
2021-11-30 04:24:20 +00:00
"crash",
})
.WithWorkingDirectory(projectFile.DirectoryName!)
.WithStandardOutputPipe(
PipeTarget.ToStringBuilder(output))
.ExecuteAsync(cancellationToken)
.ConfigureAwait(false));
// Verify the return code.
Assert.NotNull(exception);
}
2021-12-07 23:34:02 +00:00
[Fact]
public async Task TableCommandWorks()
{
// Run the executable using CliWrap.
2021-12-07 23:34:02 +00:00
FileInfo projectFile = this.GetProjectFile();
StringBuilder output = new();
2021-12-07 23:34:02 +00:00
var delay = TimeSpan.FromMinutes(1);
CancellationTokenSource cancellationTokenSource = new(delay);
CancellationToken cancellationToken = cancellationTokenSource.Token;
CommandResult result = await Cli.Wrap("dotnet")
.WithArguments(
new[]
{
"run", "--project", projectFile.FullName, "--", "table",
})
.WithWorkingDirectory(projectFile.DirectoryName!)
.WithStandardOutputPipe(PipeTarget.ToStringBuilder(output))
.ExecuteAsync(cancellationToken)
.ConfigureAwait(false);
// Verify the return code.
Assert.Equal(0, result.ExitCode);
// Check the output.
Assert.Equal(
new[]
{
"DefaultString DefaultInt32",
"-------------+------------",
"Row 1 1",
"Row 2 10",
"Row 3 100",
"",
},
output.ToString().Split("\n"));
}
/// <summary>
/// Gets the file object representing the sample tool's project.
/// </summary>
2021-12-07 23:34:02 +00:00
private FileInfo GetProjectFile()
{
2021-12-07 23:34:02 +00:00
FileInfo file = this
.GetType()
.GetDirectory()
.FindGitRoot()
!.GetFile("tests", "SampleTool", "SampleTool.csproj");
return file;
}
}
}