From 695a8f8e4231599c4141d4d12fb45d6711bcf2d3 Mon Sep 17 00:00:00 2001 From: Dave Glick Date: Sat, 16 May 2020 13:03:14 -0400 Subject: [PATCH] More data pipeline tests --- Directory.Build.props | 4 +- .../Modules/ApplyDirectoryMetadata.cs | 3 +- .../BootstrapperExtensions.cs | 55 ------- .../Statiq.Web.Tests/Pipelines/DataFixture.cs | 136 +++++++++++++++++- 4 files changed, 136 insertions(+), 62 deletions(-) delete mode 100644 tests/Statiq.Web.Tests/BootstrapperExtensions.cs diff --git a/Directory.Build.props b/Directory.Build.props index 7e3c673f8..3838d9e02 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -2,9 +2,9 @@ - false + true - 1.0.0-beta.10 + 1.0.0-beta.11 diff --git a/src/Statiq.Web/Modules/ApplyDirectoryMetadata.cs b/src/Statiq.Web/Modules/ApplyDirectoryMetadata.cs index eccd3bc03..efaee93dc 100644 --- a/src/Statiq.Web/Modules/ApplyDirectoryMetadata.cs +++ b/src/Statiq.Web/Modules/ApplyDirectoryMetadata.cs @@ -31,7 +31,7 @@ protected override IEnumerable ExecuteContext(IExecutionContext conte .ToImmutableArray()); // Iterate through all input documents in parallel - return context.Inputs.Select(input => + IEnumerable results = context.Inputs.Select(input => { IDocument merged = input; @@ -65,6 +65,7 @@ protected override IEnumerable ExecuteContext(IExecutionContext conte return merged; }); + return results; } private class DirectoryMetadataData diff --git a/tests/Statiq.Web.Tests/BootstrapperExtensions.cs b/tests/Statiq.Web.Tests/BootstrapperExtensions.cs deleted file mode 100644 index 847959d7d..000000000 --- a/tests/Statiq.Web.Tests/BootstrapperExtensions.cs +++ /dev/null @@ -1,55 +0,0 @@ -using System; -using System.Collections.Immutable; -using System.Threading.Tasks; -using NUnit.Framework; -using Octokit; -using Shouldly; -using Statiq.App; -using Statiq.Common; -using Statiq.Core; -using Statiq.Testing; -using Statiq.Web.Pipelines; - -namespace Statiq.Web.Tests -{ - public static class BootstrapperExtensions - { - public static async Task> RunTestAsync( - this Bootstrapper bootstrapper, - string outputsPipeline, - Phase outputsPhase, - IFileProvider fileProvider) - { - ImmutableArray outputs; - bootstrapper.ConfigureEngine(engine => - { - IPipeline pipeline = engine.Pipelines[outputsPipeline]; - ModuleList modules = null; - switch (outputsPhase) - { - case Phase.Input: - modules = pipeline.InputModules; - break; - case Phase.Process: - modules = pipeline.ProcessModules; - break; - case Phase.PostProcess: - modules = pipeline.PostProcessModules; - break; - case Phase.Output: - modules = pipeline.OutputModules; - break; - } - modules.Add(new ExecuteConfig(Config.FromContext(ctx => - { - outputs = ctx.Inputs; - return ctx.Inputs; - }))); - engine.FileSystem.RootPath = "/"; - engine.FileSystem.FileProvider = fileProvider; - }); - await bootstrapper.RunAsync(); - return outputs; - } - } -} diff --git a/tests/Statiq.Web.Tests/Pipelines/DataFixture.cs b/tests/Statiq.Web.Tests/Pipelines/DataFixture.cs index a52547c99..60bd001f4 100644 --- a/tests/Statiq.Web.Tests/Pipelines/DataFixture.cs +++ b/tests/Statiq.Web.Tests/Pipelines/DataFixture.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Immutable; +using System.Linq; using System.Threading.Tasks; using NUnit.Framework; using Shouldly; @@ -26,10 +27,11 @@ public async Task ParsesJson() }; // When - ImmutableArray outputs = await bootstrapper.RunTestAsync(nameof(Data), Phase.Process, fileProvider); + BootstrapperTestResult result = await bootstrapper.RunTestAsync(fileProvider); // Then - IDocument document = outputs.ShouldHaveSingleItem(); + result.ExitCode.ShouldBe((int)ExitCode.Normal); + IDocument document = result.Outputs[nameof(Data)][Phase.Process].ShouldHaveSingleItem(); document["Foo"].ShouldBe("Bar"); } @@ -44,10 +46,136 @@ public async Task ParsesYaml() }; // When - ImmutableArray outputs = await bootstrapper.RunTestAsync(nameof(Data), Phase.Process, fileProvider); + BootstrapperTestResult result = await bootstrapper.RunTestAsync(fileProvider); // Then - IDocument document = outputs.ShouldHaveSingleItem(); + result.ExitCode.ShouldBe((int)ExitCode.Normal); + IDocument document = result.Outputs[nameof(Data)][Phase.Process].ShouldHaveSingleItem(); + document["Foo"].ShouldBe("Bar"); + } + + [Test] + public async Task HonorsDataFilesSetting() + { + // Given + Bootstrapper bootstrapper = Bootstrapper.Factory.CreateWeb(Array.Empty()); + bootstrapper.AddSetting(WebKeys.DataFiles, "x/**/*.json"); + TestFileProvider fileProvider = new TestFileProvider + { + { "/input/a/b/c.json", "{ \"Foo\": \"Bar\" }" }, + { "/input/x/y/z.json", "{ \"Foo\": \"Buz\" }" } + }; + + // When + BootstrapperTestResult result = await bootstrapper.RunTestAsync(fileProvider); + + // Then + result.ExitCode.ShouldBe((int)ExitCode.Normal); + IDocument document = result.Outputs[nameof(Data)][Phase.Process].ShouldHaveSingleItem(); + document["Foo"].ShouldBe("Buz"); + } + + [Test] + public async Task SupportsMultipleDataFilesPatterns() + { + // Given + Bootstrapper bootstrapper = Bootstrapper.Factory.CreateWeb(Array.Empty()); + bootstrapper.AddSetting(WebKeys.DataFiles, new[] { "a/**/*.json", "x/**/*.json" }); + TestFileProvider fileProvider = new TestFileProvider + { + { "/input/a/b/c.json", "{ \"Foo\": \"Bar\" }" }, + { "/input/x/y/z.json", "{ \"Foo\": \"Buz\" }" } + }; + + // When + BootstrapperTestResult result = await bootstrapper.RunTestAsync(fileProvider); + + // Then + result.ExitCode.ShouldBe((int)ExitCode.Normal); + result.Outputs[nameof(Data)][Phase.Process].Select(x => x["Foo"]).ShouldBe(new[] { "Bar", "Buz" }, true); + } + + [Test] + public async Task IncludesDocumentsFromDependencies() + { + // Given + Bootstrapper bootstrapper = Bootstrapper.Factory.CreateWeb(Array.Empty()); + bootstrapper.AddSetting(WebKeys.DataFiles, "x/**/*.json"); + bootstrapper.BuildPipeline("Test", builder => builder + .WithInputReadFiles("a/**/*.json") + .AsDependencyOf(nameof(Data))); + TestFileProvider fileProvider = new TestFileProvider + { + { "/input/a/b/c.json", "{ \"Foo\": \"Bar\" }" }, + { "/input/x/y/z.json", "{ \"Foo\": \"Buz\" }" } + }; + + // When + BootstrapperTestResult result = await bootstrapper.RunTestAsync(fileProvider); + + // Then + result.ExitCode.ShouldBe((int)ExitCode.Normal); + result.Outputs[nameof(Data)][Phase.Process].Select(x => x["Foo"]).ShouldBe(new[] { "Bar", "Buz" }, true); + } + + [Test] + public async Task AddsDiectoryMetadata() + { + // Given + Bootstrapper bootstrapper = Bootstrapper.Factory.CreateWeb(Array.Empty()); + TestFileProvider fileProvider = new TestFileProvider + { + { "/input/a/b/c.json", "{ \"Foo\": \"Bar\" }" }, + { "/input/a/_directory.json", "{ \"Fizz\": \"Buzz\" }" } + }; + + // When + BootstrapperTestResult result = await bootstrapper.RunTestAsync(fileProvider); + + // Then + result.ExitCode.ShouldBe((int)ExitCode.Normal); + IDocument document = result.Outputs[nameof(Data)][Phase.Process].ShouldHaveSingleItem(); + document["Foo"].ShouldBe("Bar"); + document["Fizz"].ShouldBe("Buzz"); + } + + [Test] + public async Task FiltersExcludedFiles() + { + // Given + Bootstrapper bootstrapper = Bootstrapper.Factory.CreateWeb(Array.Empty()); + TestFileProvider fileProvider = new TestFileProvider + { + { "/input/a/b/c.json", "{ \"Foo\": \"Bar\" }" }, + { "/input/x/y/z.json", "{ \"Excluded\": \"true\" }" } + }; + + // When + BootstrapperTestResult result = await bootstrapper.RunTestAsync(fileProvider); + + // Then + result.ExitCode.ShouldBe((int)ExitCode.Normal); + IDocument document = result.Outputs[nameof(Data)][Phase.Process].ShouldHaveSingleItem(); + document["Foo"].ShouldBe("Bar"); + } + + [Test] + public async Task DoesNotReadUnderscoreFilesByDefault() + { + // Given + Bootstrapper bootstrapper = Bootstrapper.Factory.CreateWeb(Array.Empty()); + TestFileProvider fileProvider = new TestFileProvider + { + { "/input/a/b/c.json", "{ \"Foo\": \"Bar\" }" }, + { "/input/x/y/_z.json", "{ \"Fizz\": \"Buzz\" }" } + }; + + // When + BootstrapperTestResult result = await bootstrapper.RunTestAsync(fileProvider); + + // Then + result.ExitCode.ShouldBe((int)ExitCode.Normal); + IDocument document = result.Outputs[nameof(Data)][Phase.Process].ShouldHaveSingleItem(); document["Foo"].ShouldBe("Bar"); } }