Files
Khaemhat/tests/Khaemhat.Core.Tests/Architecture/DependencyRulesTests.cs
T
Alby96andClaude Opus 5.5 a9eda510f2 feat: postal codes from GeoNames for the ISTAT municipalities (#45)
The municipalities now come with their postal codes, so the lookup from
postal code works on a new company without loading anything. GeoNames
(CC BY 4.0) lists places rather than municipalities: a postal code goes
to a municipality only when the name and the province abbreviation match
exactly one of them (the name alone within Sardinia, whose abbreviations
changed), which covers 7,750 of 7,894 municipalities; the rest are mostly
renamed or merged ones that users complete by hand.

tools/Khaemhat.PlacesTool rebuilds both lists from the ISTAT sheet and
the GeoNames file, framework only, so the yearly update is repeatable.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 09:16:07 +01:00

120 lines
5.0 KiB
C#

using System.Xml.Linq;
namespace Khaemhat.Core.Tests.Architecture;
/// <summary>
/// Project dependency rules of SPEC §3 and the package allowlist of SPEC §1.2 (ADR 0002). They read the project
/// files, so a forbidden reference fails here before anyone builds on it.
/// </summary>
[TestClass]
public sealed class DependencyRulesTests
{
// What each production project may reference: exactly SPEC §3.
private static readonly Dictionary<string, string[]> AllowedReferences = new(StringComparer.Ordinal)
{
["Khaemhat.Core"] = [],
["Khaemhat.Fiscal.Abstractions"] = ["Khaemhat.Core"],
["Khaemhat.Fiscal.Italy"] = ["Khaemhat.Core", "Khaemhat.Fiscal.Abstractions", "Khaemhat.Infrastructure"],
["Khaemhat.Data"] = ["Khaemhat.Core"],
["Khaemhat.Data.Sqlite"] = ["Khaemhat.Data"],
["Khaemhat.Data.PostgreSql"] = ["Khaemhat.Data"],
["Khaemhat.Infrastructure"] = ["Khaemhat.Core", "Khaemhat.Data"],
["Khaemhat.Application"] = ["Khaemhat.Core", "Khaemhat.Data", "Khaemhat.Infrastructure", "Khaemhat.Fiscal.Abstractions"],
["Khaemhat.Web"] = ["Khaemhat.Application", "Khaemhat.Data.Sqlite", "Khaemhat.Data.PostgreSql", "Khaemhat.Fiscal.Italy"],
["Khaemhat.LicenseTool"] = ["Khaemhat.Core", "Khaemhat.Infrastructure"],
["Khaemhat.DataModelTool"] = ["Khaemhat.Data"],
["Khaemhat.PlacesTool"] = [],
};
// The only packages, and where each may be referenced.
private static readonly Dictionary<string, Func<string, bool>> AllowedPackages = new(StringComparer.OrdinalIgnoreCase)
{
["Npgsql"] = file => Name(file) == "Khaemhat.Data.PostgreSql",
["Microsoft.Data.Sqlite"] = file => Name(file) == "Khaemhat.Data.Sqlite",
["Microsoft.NET.Test.Sdk"] = IsTestFile,
["MSTest"] = IsTestFile,
};
[TestMethod]
public void EveryProductionProject_HasDependencyRules()
{
var unknown = RepositoryLayout.Projects().Keys
.Where(name => !name.EndsWith(".Tests", StringComparison.Ordinal) && !AllowedReferences.ContainsKey(name))
.ToList();
Assert.IsEmpty(unknown, "Projects without dependency rules (add them to SPEC §3 and this test): " + string.Join(", ", unknown));
}
[TestMethod]
public void ProductionProjects_ReferenceOnlyWhatSpecSection3Allows()
{
var violations = new List<string>();
foreach (var (name, file) in RepositoryLayout.Projects())
{
if (!AllowedReferences.TryGetValue(name, out var allowed))
{
continue;
}
foreach (var reference in RepositoryLayout.ProjectReferences(file).Except(allowed, StringComparer.Ordinal))
{
violations.Add($"{name} -> {reference}");
}
}
Assert.IsEmpty(violations, "Forbidden project references: " + string.Join("; ", violations));
}
[TestMethod]
public void TestProjects_ReferenceOnlyKhaemhatProjects()
{
var violations = RepositoryLayout.Projects()
.Where(p => p.Key.EndsWith(".Tests", StringComparison.Ordinal))
.SelectMany(p => RepositoryLayout.ProjectReferences(p.Value)
.Where(r => !r.StartsWith("Khaemhat.", StringComparison.Ordinal))
.Select(r => $"{p.Key} -> {r}"))
.ToList();
Assert.IsEmpty(violations, string.Join("; ", violations));
}
[TestMethod]
public void Packages_AreOnTheAllowlistAndInTheirProjects()
{
var violations = new List<string>();
foreach (var file in RepositoryLayout.MsBuildFiles())
{
foreach (var package in RepositoryLayout.PackageReferences(file))
{
if (!AllowedPackages.TryGetValue(package, out var allowedIn))
{
violations.Add($"{RepositoryLayout.Relative(file)}: {package} is not on the allowlist (SPEC §1.2)");
}
else if (!allowedIn(file))
{
violations.Add($"{RepositoryLayout.Relative(file)}: {package} is not allowed in this project");
}
}
}
Assert.IsEmpty(violations, string.Join(Environment.NewLine, violations));
}
[TestMethod]
public void CentralPackageVersions_ListOnlyAllowedPackages()
{
var file = Path.Combine(RepositoryLayout.Root, "Directory.Packages.props");
var extra = XDocument.Load(file).Descendants()
.Where(e => e.Name.LocalName == "PackageVersion")
.Select(e => (string?)e.Attribute("Include") ?? string.Empty)
.Where(p => !AllowedPackages.ContainsKey(p))
.ToList();
Assert.IsEmpty(extra, "Directory.Packages.props declares packages outside the allowlist: " + string.Join(", ", extra));
}
private static string Name(string file) => Path.GetFileNameWithoutExtension(file);
private static bool IsTestFile(string file) => RepositoryLayout.Relative(file).StartsWith("tests/", StringComparison.Ordinal);
}