114 lines
3.7 KiB
C#
114 lines
3.7 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 async Task CrashCommandFails()
|
||
|
{
|
||
|
// Run the executable using CliWrap.
|
||
|
FileInfo projectFile = GetProjectFile();
|
||
|
StringBuilder output = new();
|
||
|
CancellationToken cancellationToken =
|
||
|
new CancellationTokenSource(TimeSpan.FromSeconds(20))
|
||
|
.Token;
|
||
|
|
||
|
var 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]
|
||
|
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;
|
||
|
}
|
||
|
}
|
||
|
}
|