2021-11-30 00:45:26 +00:00
|
|
|
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;
|
|
|
|
|
2021-11-30 00:45:26 +00:00
|
|
|
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()
|
2021-11-30 00:45:26 +00:00
|
|
|
{
|
|
|
|
// Run the executable using CliWrap.
|
2021-12-07 23:34:02 +00:00
|
|
|
FileInfo projectFile = this.GetProjectFile();
|
2021-11-30 00:45:26 +00:00
|
|
|
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[]
|
|
|
|
{
|
2022-04-02 23:38:49 +00:00
|
|
|
"run",
|
|
|
|
"--project", projectFile.FullName,
|
|
|
|
"--",
|
2021-11-30 04:24:20 +00:00
|
|
|
"crash",
|
|
|
|
})
|
|
|
|
.WithWorkingDirectory(projectFile.DirectoryName!)
|
|
|
|
.WithStandardOutputPipe(
|
|
|
|
PipeTarget.ToStringBuilder(output))
|
|
|
|
.ExecuteAsync(cancellationToken)
|
|
|
|
.ConfigureAwait(false));
|
2021-11-30 00:45:26 +00:00
|
|
|
|
|
|
|
// Verify the return code.
|
|
|
|
Assert.NotNull(exception);
|
|
|
|
}
|
|
|
|
|
2021-12-07 23:34:02 +00:00
|
|
|
[Fact]
|
2021-11-30 00:45:26 +00:00
|
|
|
public async Task TableCommandWorks()
|
|
|
|
{
|
|
|
|
// Run the executable using CliWrap.
|
2021-12-07 23:34:02 +00:00
|
|
|
FileInfo projectFile = this.GetProjectFile();
|
2021-11-30 00:45:26 +00:00
|
|
|
StringBuilder output = new();
|
2021-12-07 23:34:02 +00:00
|
|
|
var delay = TimeSpan.FromMinutes(1);
|
|
|
|
CancellationTokenSource cancellationTokenSource = new(delay);
|
|
|
|
CancellationToken cancellationToken = cancellationTokenSource.Token;
|
2021-11-30 00:45:26 +00:00
|
|
|
|
|
|
|
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-11-30 00:45:26 +00:00
|
|
|
{
|
2021-12-07 23:34:02 +00:00
|
|
|
FileInfo file = this
|
|
|
|
.GetType()
|
|
|
|
.GetDirectory()
|
|
|
|
.FindGitRoot()
|
|
|
|
!.GetFile("tests", "SampleTool", "SampleTool.csproj");
|
2021-11-30 00:45:26 +00:00
|
|
|
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|