using System.Diagnostics;
using System.Net.Sockets;
[assembly: Parallelize(Scope = ExecutionScope.MethodLevel)]
namespace Anubis.Tests;
/// Test categories (ADR 0003). never runs in the normal suite.
internal static class Category
{
public const string Unit = "Unit";
public const string Integration = "Integration";
public const string Simulation = "Simulation";
public const string LiveDemo = "LiveDemo";
public const string Golden = "Golden";
}
/// Locates the repository so policy tests can read its files.
internal static class Repo
{
public static DirectoryInfo Root { get; } = FindRoot();
private static DirectoryInfo FindRoot()
{
for (var dir = new DirectoryInfo(AppContext.BaseDirectory); dir is not null; dir = dir.Parent)
{
if (File.Exists(Path.Combine(dir.FullName, "Anubis.slnx")))
{
return dir;
}
}
throw new InvalidOperationException("Repository root (Anubis.slnx) not found above " + AppContext.BaseDirectory);
}
private static readonly Lazy> _files = new(ListFiles);
///
/// The files git would commit: tracked ones plus new ones that .gitignore does not exclude.
/// The local .env, /data and build output stay out, so a key the user puts there never fails the suite.
///
public static IEnumerable Files() => _files.Value;
private static List ListFiles()
{
var info = new ProcessStartInfo("git")
{
WorkingDirectory = Root.FullName,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
};
foreach (var argument in new[] { "ls-files", "-z", "--cached", "--others", "--exclude-standard" })
{
info.ArgumentList.Add(argument);
}
using var process = Process.Start(info) ?? throw new InvalidOperationException("git could not be started.");
var errors = process.StandardError.ReadToEndAsync();
var output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
if (process.ExitCode != 0)
{
throw new InvalidOperationException("git ls-files failed: " + errors.Result);
}
return output.Split('\0', StringSplitOptions.RemoveEmptyEntries)
.Select(path => new FileInfo(Path.Combine(Root.FullName, path)))
.Where(file => file.Exists)
.ToList();
}
public static string Relative(FileInfo file) =>
Path.GetRelativePath(Root.FullName, file.FullName).Replace('\\', '/');
}
/// Database engine the suite runs against: ANUBIS_TEST_DB = sqlite (default) or postgres.
internal static class TestDatabase
{
public static string Engine { get; } =
(Environment.GetEnvironmentVariable("ANUBIS_TEST_DB") ?? "sqlite").Trim().ToLowerInvariant();
/// Host and port of the PostgreSQL used by the tests, from ANUBIS_TEST_PG_HOST and ANUBIS_TEST_PG_PORT.
public static (string Host, int Port) Postgres { get; } = (
Environment.GetEnvironmentVariable("ANUBIS_TEST_PG_HOST") ?? "localhost",
int.TryParse(Environment.GetEnvironmentVariable("ANUBIS_TEST_PG_PORT"), out var port) ? port : 55432);
public static async Task PostgresReachableAsync(TimeSpan timeout)
{
using var client = new TcpClient();
using var cts = new CancellationTokenSource(timeout);
try
{
await client.ConnectAsync(Postgres.Host, Postgres.Port, cts.Token).ConfigureAwait(false);
return true;
}
catch (Exception ex) when (ex is SocketException or OperationCanceledException)
{
return false;
}
}
}