using System.Xml.Linq; namespace Khaemhat.Core.Tests.Architecture; /// /// 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. /// [TestClass] public sealed class DependencyRulesTests { // What each production project may reference: exactly SPEC §3. private static readonly Dictionary 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> 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(); 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(); 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); }