Files
Anubis/tests/Anubis.Tests/DependencyPolicyTests.cs
T
Alby96andClaude Opus 5.5 b4bf864f74
ci / test (postgres) (push) Successful in 1m18s
ci / test (sqlite) (push) Successful in 1m23s
ci / image (push) Successful in 41s
Set up phase 0: .NET 10 solution, toolchain, minimal app, Docker and CI
Empty but complete solution so every later phase starts from a working
toolchain: analyzers as errors, locked restore, format check, policy tests
that keep packages within ADR 0004 and keep live trading switched off,
a /health endpoint served from Docker, and a Gitea workflow with only
run steps. API facts of SPEC section 4 re-checked in docs/API-NOTES.md;
ADR 0006 records what changed (MSTest base packages, Ubuntu images,
OKX WebSocket on 443, Alpaca margin-only accounts).

refs #1 refs #2 refs #3 refs #4 refs #5

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 09:51:41 +01:00

109 lines
4.4 KiB
C#

using System.Text.Json;
using System.Xml.Linq;
namespace Anubis.Tests;
/// <summary>ADR 0004: only the packages the user authorised, and nothing else sneaking in.</summary>
[TestClass]
public sealed class DependencyPolicyTests
{
/// <summary>The list of ADR 0004. Changing it needs the user's permission and a new ADR.</summary>
private static readonly HashSet<string> _authorised = new(StringComparer.OrdinalIgnoreCase)
{
"Microsoft.Data.Sqlite",
"System.Security.Cryptography.ProtectedData",
"Microsoft.Web.WebView2",
"MSTest.TestFramework",
"MSTest.TestAdapter",
"Microsoft.NET.Test.Sdk",
"Npgsql",
"Microsoft.ML",
"Microsoft.ML.LightGbm",
"Microsoft.ML.FastTree",
"Microsoft.ML.OnnxRuntime",
"Microsoft.ML.Tokenizers",
};
/// <summary>
/// Third-party packages pulled in by an authorised one, each listed in docs/API-NOTES.md and
/// reported to the user. Microsoft.* and System.* ids are reserved to Microsoft on nuget.org.
/// </summary>
private static readonly HashSet<string> _knownThirdPartyTransitive = new(StringComparer.OrdinalIgnoreCase);
[TestMethod]
[TestCategory(Category.Unit)]
public void CentralVersionsListOnlyAuthorisedPackages()
{
var props = XDocument.Load(Path.Combine(Repo.Root.FullName, "Directory.Packages.props"));
var listed = props.Descendants("PackageVersion").Select(e => (string)e.Attribute("Include")!).ToList();
var notAllowed = listed.Where(id => !_authorised.Contains(id)).ToList();
Assert.IsEmpty(notAllowed, "Pacchetti non autorizzati (ADR 0004): " + string.Join(", ", notAllowed));
}
[TestMethod]
[TestCategory(Category.Unit)]
public void ProjectsDoNotPinVersionsOrBypassTheCentralList()
{
var problems = new List<string>();
foreach (var project in Repo.Files().Where(f => f.Extension.Equals(".csproj", StringComparison.OrdinalIgnoreCase)))
{
var doc = XDocument.Load(project.FullName);
foreach (var reference in doc.Descendants("PackageReference"))
{
var id = (string?)reference.Attribute("Include") ?? "?";
if (reference.Attribute("Version") is not null || reference.Attribute("VersionOverride") is not null)
{
problems.Add($"{Repo.Relative(project)}: {id} ha una versione propria");
}
if (!_authorised.Contains(id))
{
problems.Add($"{Repo.Relative(project)}: {id} non è autorizzato");
}
}
}
Assert.IsEmpty(problems, string.Join(Environment.NewLine, problems));
}
[TestMethod]
[TestCategory(Category.Unit)]
public void LockFilesContainNoUnknownThirdPartyPackage()
{
var lockFiles = Repo.Files().Where(f => f.Name.Equals("packages.lock.json", StringComparison.OrdinalIgnoreCase)).ToList();
Assert.IsNotEmpty(lockFiles, "Nessun packages.lock.json: i file di blocco devono essere nel repository.");
var unknown = new SortedSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (var file in lockFiles)
{
using var doc = JsonDocument.Parse(File.ReadAllText(file.FullName));
foreach (var framework in doc.RootElement.GetProperty("dependencies").EnumerateObject())
{
foreach (var package in framework.Value.EnumerateObject())
{
var type = package.Value.TryGetProperty("type", out var t) ? t.GetString() : null;
if (type == "Project")
{
continue;
}
var id = package.Name;
if (!_authorised.Contains(id) && !IsMicrosoftId(id) && !_knownThirdPartyTransitive.Contains(id))
{
unknown.Add($"{id} (in {Repo.Relative(file)})");
}
}
}
}
Assert.IsEmpty(unknown, "Dipendenze di terze parti non dichiarate: " + string.Join(", ", unknown));
}
private static bool IsMicrosoftId(string id) =>
id.StartsWith("Microsoft.", StringComparison.OrdinalIgnoreCase)
|| id.StartsWith("System.", StringComparison.OrdinalIgnoreCase)
|| id.StartsWith("MSTest.", StringComparison.OrdinalIgnoreCase)
|| id.Equals("NETStandard.Library", StringComparison.OrdinalIgnoreCase);
}