test: working on CopyFiles example generator and tests

This commit is contained in:
Dylan R. E. Moonfire 2021-12-07 00:00:13 -06:00
parent 88fd6d5cea
commit dfdecdcbae
9 changed files with 225 additions and 20 deletions

6
TASKS.md Normal file
View file

@ -0,0 +1,6 @@
# Tasks
- [ ] Does not return a proper code when failing
- [ ] CopyFiles sample does not generate data
- [ ] ReadFiles should have With methods
- [ ] WriteFiles should have With methods

View file

@ -1,8 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Nitride.IO\Nitride.IO.csproj" />
<ProjectReference Include="..\..\src\Nitride\Nitride.csproj" />
<ProjectReference Include="..\..\tests\Nitride.Tests\Nitride.Tests.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="CliWrap" Version="3.3.3" />
<PackageReference Include="JunitXml.TestLogger" Version="3.0.110" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="MfGames.IO" Version="1.2.3" />
<PackageReference Include="coverlet.collector" Version="3.0.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
</Project>

View file

@ -0,0 +1,20 @@
using Autofac;
namespace CopyFiles
{
public class CopyFilesModule : Module
{
/// <inheritdoc />
protected override void Load(ContainerBuilder builder)
{
// This just registers all the non-static classes as singletons
// within the system. We use lifetimes in other components depending
// on how they are used, but in this case, we don't need it.
builder
.RegisterAssemblyTypes(this.GetType().Assembly)
.AsSelf()
.AsImplementedInterfaces()
.SingleInstance();
}
}
}

View file

@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Gallium;
using Nitride.IO.Contents;
using Nitride.Pipelines;
namespace CopyFiles
{
/// <summary>
/// The single pipeline used by the CopyFiles project.
/// </summary>
public class CopyFilesPipeline : PipelineBase
{
private readonly ReadFiles readFiles;
private readonly WriteFiles writeFiles;
public CopyFilesPipeline(
ReadFiles readFiles,
WriteFiles writeFiles)
{
// While we can configure these during runtime, it seems cleaner to
// build them up during the constructor to call out the ones that
// require runtime data.
this.readFiles = readFiles;
this.writeFiles = writeFiles;
}
/// <inheritdoc />
public override Task<IEnumerable<Entity>> RunAsync(
IEnumerable<Entity> entities)
{
throw new NotImplementedException();
}
}
}

View file

@ -0,0 +1,55 @@
using System;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using Autofac;
using MfGames.IO.Extensions;
using Nitride;
using Nitride.IO;
namespace CopyFiles
{
/// <summary>
/// Main entry point into the CopyFiles sample generator.
/// </summary>
public static class CopyFilesProgram
{
public static async Task<int> Main(string[] args)
{
// All of the builder methods are fluent in that they return the
// builder to allow them to be chained. However, for documentation
// purposes, we are going to split them apart to explain the details.
var builder = new NitrideBuilder(args);
// Filesystem access is provided by Zio, which allows for mutliple
// folders to be combined together into a single unified file
// system. At the moment, we set the "root" directory which will
// contains all the paths, both input and output.
DirectoryInfo rootDir = typeof(CopyFilesProgram)
.GetDirectory()
!.FindGitRoot()
!.GetDirectory("examples/CopyFiles");
builder.WithRootDirectory(rootDir);
// Like Serilog, we use a number of extension methods on the builder
// to inject functionality such as plugins and extensions. In this
// case, we only need the Nitride.IO module so we use the `UseIO`
// to inject the requisite modules and configure it.
builder.UseIO();
// We use Autofac for the bulk of our registration handling. This
// was mainly because we are more comfortable with Autofac, but it
// also has a clean interface for handling some of the more esoteric
// problems we've encountered.
builder.ConfigureContainer(
x => x.RegisterModule<CopyFilesModule>());
// Finally, we build the site generator object and run it.
return await builder.BuildAsync();
}
}
}

View file

@ -0,0 +1,68 @@
using System;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using CliWrap;
using MfGames.IO.Extensions;
using Nitride.Tests;
using Xunit;
using Xunit.Abstractions;
namespace CopyFiles
{
/// <summary>
/// Tests the execution of the tool and ensures it is working correctly.
/// </summary>
public class CopyFilesTest : NitrideTestsBase
{
public CopyFilesTest(ITestOutputHelper output)
: base(output)
{
}
[Fact]
public async Task Run()
{
// Figure out the paths for this test.
DirectoryInfo rootDir = typeof(CopyFilesProgram)
.GetDirectory()
!.FindGitRoot()
!.GetDirectory("examples/CopyFiles");
DirectoryInfo outputDir = rootDir.GetDirectory("output");
FileInfo projectFile = rootDir.GetFile("CopyFiles.csproj");
this.Logger.Error("A {0}", rootDir);
// Clear out the output directory if we have an old one.
if (outputDir.Exists)
{
outputDir.Delete(true);
}
// Execute the generator. This will throw if there is an exception.
await Cli
.Wrap("dotnet")
.WithArguments(
x => x
.Add("run")
.Add("--project")
.Add(projectFile.FullName)
.Add("--")
.Add("build"))
.ExecuteAsync();
// Make sure we have our output.
FileInfo aFile = outputDir.GetFile("a.txt");
Assert.True(aFile.Exists);
string aText = aFile.ReadAllText().Trim();
Assert.Equal("This is the 'A' file.", aText);
}
}
}

View file

@ -1,12 +0,0 @@
using System;
namespace CopyFiles
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}

View file

@ -0,0 +1,6 @@
# Copy Files
This is probably the most basic generator possible. It simply copies files from
the input and places them into the output. However, it also demonstrates a
basic setup including creating a pipeline, wiring everything up with modules,
and configuring everything.

View file

@ -0,0 +1 @@
This is the 'A' file.