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
2021-11-30 20:12:29 -06:00

116 lines
3.9 KiB
C#

using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using CliWrap;
using CliWrap.Exceptions;
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]
public void CrashCommandFails()
{
// Run the executable using CliWrap.
FileInfo projectFile = GetProjectFile();
StringBuilder output = new();
CancellationToken cancellationToken =
new CancellationTokenSource(TimeSpan.FromSeconds(20))
.Token;
Task<CommandExecutionException> exception =
Assert.ThrowsAsync<CommandExecutionException>(
async () => await Cli.Wrap("dotnet")
.WithArguments(
new[]
{
"run", "--project", projectFile.FullName, "--",
"crash",
})
.WithWorkingDirectory(projectFile.DirectoryName!)
.WithStandardOutputPipe(
PipeTarget.ToStringBuilder(output))
.ExecuteAsync(cancellationToken)
.ConfigureAwait(false));
// Verify the return code.
Assert.NotNull(exception);
}
[Fact(Skip = "Not working in CI for some reason")]
public async Task TableCommandWorks()
{
// Run the executable using CliWrap.
FileInfo projectFile = GetProjectFile();
StringBuilder output = new();
CancellationToken cancellationToken =
new CancellationTokenSource(TimeSpan.FromSeconds(20))
.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>
private static FileInfo GetProjectFile()
{
// Loop up until we find the directory that contains it.
var parent = new DirectoryInfo(Environment.CurrentDirectory);
while (parent?.GetDirectories("SampleTool").Length == 0)
{
parent = parent.Parent;
}
// If we got a null, we can't find it.
if (parent == null)
{
throw new DirectoryNotFoundException(
"Cannot find sample tool directory from "
+ Environment.CurrentDirectory);
}
// Get the project file inside there.
DirectoryInfo directory = parent.GetDirectories("SampleTool")[0];
FileInfo file = directory.GetFiles("SampleTool.csproj")[0];
return file;
}
}
}