Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions EssentialCSharp.Web.Tests/PlaceholderShapeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using System.Text.Json;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;

namespace EssentialCSharp.Web.Tests;

/// <summary>
/// The placeholders in <c>Placeholders/</c> stand in for the content package in PR builds, so they must have
/// the same JSON shape. When the build uses the real package (<c>-p:AccessToNugetFeed=true</c>) the content
/// files differ from the placeholders and this fails if the package changed shape. Without the package the
/// content files are the placeholders, so it passes trivially. Only property names and value kinds are
/// compared, and failures report only those, so no book content reaches a public log.
/// </summary>
public class PlaceholderShapeTests : IntegrationTestBase
{
[Test]
[Arguments("Chapters/sitemap.json", "sitemap.json")]
[Arguments("Chapters/sitemap.descriptions.json", "sitemap.descriptions.json")]
[Arguments("Guidelines/guidelines.json", "guidelines.json")]
public async Task PlaceholderMatchesContentShape(string contentPath, string placeholderName)
{
string root = Factory.Inner.Services.GetRequiredService<IWebHostEnvironment>().ContentRootPath;
// Path.Join (unlike Path.Combine) never discards earlier segments if a later one is rooted.
string placeholderPath = Path.Join(root, "Placeholders", placeholderName);
string actualPath = Path.Join(root, contentPath);

Dictionary<string, HashSet<string>> placeholder = ReadShape(placeholderPath);
Dictionary<string, HashSet<string>> actual = ReadShape(actualPath);

List<string> differences = [];
foreach (string name in placeholder.Keys.Except(actual.Keys))
{
differences.Add($"property \"{name}\" is only in the placeholder");
}
foreach (string name in actual.Keys.Except(placeholder.Keys))
{
differences.Add($"property \"{name}\" is only in the content package");
}
// Nulls are ignored: optional fields may be null throughout one file and populated in the other.
foreach (string name in placeholder.Keys.Intersect(actual.Keys)
.Where(name => placeholder[name].Count > 0 && actual[name].Count > 0 && !placeholder[name].SetEquals(actual[name])))
{
differences.Add($"property \"{name}\" is {string.Join("/", placeholder[name].Order())} in the placeholder but {string.Join("/", actual[name].Order())} in the content package");
}

await Assert.That(differences)
.IsEmpty()
.Because($"Placeholders/{placeholderName} must mirror {contentPath} from the content package: update the placeholder to the package's shape. {string.Join("; ", differences)}");
}

// Maps each property name used by the entries of a JSON array to the non-null value kinds seen for it
// (arrays are described by their element kinds, e.g. "Array<String>").
private static Dictionary<string, HashSet<string>> ReadShape(string path)
{
using FileStream stream = File.OpenRead(path);
using JsonDocument document = JsonDocument.Parse(stream);
if (document.RootElement.ValueKind != JsonValueKind.Array)
{
throw new InvalidDataException($"Expected a JSON array in {Path.GetFileName(path)}");
}

Dictionary<string, HashSet<string>> shape = [];
foreach (JsonElement entry in document.RootElement.EnumerateArray())
{
if (entry.ValueKind != JsonValueKind.Object)
{
throw new InvalidDataException($"Expected every entry in {Path.GetFileName(path)} to be an object");
}

foreach (JsonProperty property in entry.EnumerateObject())
{
if (!shape.TryGetValue(property.Name, out HashSet<string>? kinds))
{
shape[property.Name] = kinds = [];
}

if (property.Value.ValueKind != JsonValueKind.Null)
{
kinds.Add(Describe(property.Value));
}
}
}

return shape;
}

private static string Describe(JsonElement value) =>
value.ValueKind == JsonValueKind.Array
? $"Array<{string.Join("|", value.EnumerateArray().Select(Describe).Distinct().Order())}>"
: value.ValueKind.ToString();
}
2 changes: 2 additions & 0 deletions EssentialCSharp.Web/EssentialCSharp.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<PlaceholderChapterOneHtmlFile Include="$(ProjectDir)/Placeholders/Chapters/01/Pages/*.html" />
<PlaceholderChapterTwoHtmlFile Include="$(ProjectDir)/Placeholders/Chapters/02/Pages/*.html" />
<PlaceholderSitemapJsonFile Include="$(ProjectDir)/Placeholders/sitemap.json" />
<PlaceholderSitemapDescriptionsJsonFile Include="$(ProjectDir)/Placeholders/sitemap.descriptions.json" />
<PlaceholderGuidelinesJsonFile Include="$(ProjectDir)/Placeholders/guidelines.json" />
</ItemGroup>

Expand Down Expand Up @@ -88,6 +89,7 @@
</ItemGroup>
<Target Condition="!$(AccessToNugetFeed)" Name="CopyPlaceholderContent" BeforeTargets="Build">
<Copy SourceFiles="@(PlaceholderSitemapJsonFile)" DestinationFolder="$(ProjectDir)/Chapters/" SkipUnchangedFiles="true" />
<Copy SourceFiles="@(PlaceholderSitemapDescriptionsJsonFile)" DestinationFolder="$(ProjectDir)/Chapters/" SkipUnchangedFiles="true" />
<Copy SourceFiles="@(PlaceholderGuidelinesJsonFile)" DestinationFolder="$(ProjectDir)/Guidelines/" SkipUnchangedFiles="true" />
<Copy SourceFiles="@(PlaceholderChapterOneHtmlFile)" DestinationFolder="$(ProjectDir)/Chapters/01/Pages/" SkipUnchangedFiles="true" />
<Copy SourceFiles="@(PlaceholderChapterTwoHtmlFile)" DestinationFolder="$(ProjectDir)/Chapters/02/Pages/" SkipUnchangedFiles="true" />
Expand Down
236 changes: 236 additions & 0 deletions EssentialCSharp.Web/Placeholders/sitemap.descriptions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
[
{
"Keys": [
"introducing-c"
],
"Value": "Placeholder description for Introducing C# in Essential C#."
},
{
"Keys": [
"hello-world"
],
"Value": "Placeholder description for Hello, World in Essential C#."
},
{
"Keys": [
"creating-editing-compiling-and-running-c-source-code"
],
"Value": "Placeholder description for Creating, Editing, Compiling, and Running C# Source Code in Essential C#."
},
{
"Keys": [
"with-dotnet-cli"
],
"Value": "Placeholder description for With Dotnet CLI in Essential C#."
},
{
"Keys": [
"with-visual-studio-2022-windows-or-mac"
],
"Value": "Placeholder description for With Visual Studio 2022 (Windows or Mac) in Essential C#."
},
{
"Keys": [
"understanding-a-project"
],
"Value": "Placeholder description for Understanding a Project in Essential C#."
},
{
"Keys": [
"compilation-and-execution"
],
"Value": "Placeholder description for Compilation and Execution in Essential C#."
},
{
"Keys": [
"essential-c-source-code"
],
"Value": "Placeholder description for Essential C# Source Code in Essential C#."
},
{
"Keys": [
"c-syntax-fundamentals"
],
"Value": "Placeholder description for C# Syntax Fundamentals in Essential C#."
},
{
"Keys": [
"statements-and-statement-delimiters"
],
"Value": "Placeholder description for Statements and Statement Delimiters in Essential C#."
},
{
"Keys": [
"introducing-a-class-and-a-method"
],
"Value": "Placeholder description for Introducing a Class and a Method in Essential C#."
},
{
"Keys": [
"c-keywords"
],
"Value": "Placeholder description for C# Keywords in Essential C#."
},
{
"Keys": [
"identifiers"
],
"Value": "Placeholder description for Identifiers in Essential C#."
},
{
"Keys": [
"type-definition"
],
"Value": "Placeholder description for Type Definition in Essential C#."
},
{
"Keys": [
"main-method"
],
"Value": "Placeholder description for Main Method in Essential C#."
},
{
"Keys": [
"whitespace"
],
"Value": "Placeholder description for Whitespace in Essential C#."
},
{
"Keys": [
"working-with-variables"
],
"Value": "Placeholder description for Working with Variables in Essential C#."
},
{
"Keys": [
"introducing-data-types"
],
"Value": "Placeholder description for Introducing Data Types in Essential C#."
},
{
"Keys": [
"declaring-a-variable"
],
"Value": "Placeholder description for Declaring a Variable in Essential C#."
},
{
"Keys": [
"assigning-a-variable"
],
"Value": "Placeholder description for Assigning a Variable in Essential C#."
},
{
"Keys": [
"using-a-variable"
],
"Value": "Placeholder description for Using a Variable in Essential C#."
},
{
"Keys": [
"console-input-and-output"
],
"Value": "Placeholder description for Console Input and Output in Essential C#."
},
{
"Keys": [
"getting-input-from-the-console"
],
"Value": "Placeholder description for Getting Input from the Console in Essential C#."
},
{
"Keys": [
"writing-output-to-the-console"
],
"Value": "Placeholder description for Writing Output to the Console in Essential C#."
},
{
"Keys": [
"comments"
],
"Value": "Placeholder description for Comments in Essential C#."
},
{
"Keys": [
"debugging"
],
"Value": "Placeholder description for Debugging in Essential C#."
},
{
"Keys": [
"managed-execution-and-the-common-language-infrastructure"
],
"Value": "Placeholder description for Managed Execution and the Common Language Infrastructure in Essential C#."
},
{
"Keys": [
"common-intermediate-language-and-ildasm"
],
"Value": "Placeholder description for Common Intermediate Language and Ildasm in Essential C#."
},
{
"Keys": [
"multiple-net-frameworks"
],
"Value": "Placeholder description for Multiple .NET Frameworks in Essential C#."
},
{
"Keys": [
"application-programming-interface"
],
"Value": "Placeholder description for Application Programming Interface in Essential C#."
},
{
"Keys": [
"c-and-net-versioning"
],
"Value": "Placeholder description for C# and .NET Versioning in Essential C#."
},
{
"Keys": [
"chapter-1-summary"
],
"Value": "Placeholder description for Chapter 1: Summary in Essential C#."
},
{
"Keys": [
"data-types"
],
"Value": "Placeholder description for Data Types in Essential C#."
},
{
"Keys": [
"type-name-forms"
],
"Value": "Placeholder description for Type Name Forms in Essential C#."
},
{
"Keys": [
"fundamental-numeric-types"
],
"Value": "Placeholder description for Fundamental Numeric Types in Essential C#."
},
{
"Keys": [
"integer-types"
],
"Value": "Placeholder description for Integer Types in Essential C#."
},
{
"Keys": [
"floating-point-types-float-double"
],
"Value": "Placeholder description for Floating-Point Types (float, double) in Essential C#."
},
{
"Keys": [
"decimal-type"
],
"Value": "Placeholder description for Decimal Type in Essential C#."
},
{
"Keys": [
"literal-values"
],
"Value": "Placeholder description for Literal Values in Essential C#."
}
]
Loading
Loading