Skip to content

A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.

sim tests: delete Fixture.Write temp folders at the end of the run (fixes #1017) (#1020)

Berif346e7df35b5
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 95c6dd774..68a5ea0e5 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -57,6 +57,8 @@ adds a line under **Unreleased**; CI's `Changelog` job checks this (see `CONTRIB
outside the content hash, so no golden moves (#200).
### Fixed
+- The sim tests no longer leave a temp folder behind for every `Fixture.Write` call (about 1,150 per run): the
+ folders share one root per run, deleted when the run ends (#1017).
- Event city effects preserve the city's housing condition when changing its housing capacity or job slots (#1000).
- The Cabinet's Hebrew "already holds" refusal uses the holder's office title, including the feminine form for a woman;
English is unchanged (#990).
diff --git a/sim/Statecraft.Sim.Tests/Fixture.cs b/sim/Statecraft.Sim.Tests/Fixture.cs
index 990ef57f6..91a3ee83e 100644
--- a/sim/Statecraft.Sim.Tests/Fixture.cs
+++ b/sim/Statecraft.Sim.Tests/Fixture.cs
@@ -1,6 +1,10 @@
using System;
using System.Collections.Generic;
using System.IO;
+using Xunit.Abstractions;
+using Xunit.Sdk;
+
+[assembly: TestFramework("FixtureCleanupFramework", "Statecraft.Sim.Tests")]
/// <summary>Writes a small, valid content set to a temp dir. Tests override single files to test errors.</summary>
public static class Fixture
@@ -281,9 +285,18 @@ sites:
status: fact-checked
";
+ // Every Write goes under one root per test run, which FixtureCleanupFramework deletes when the run ends.
+ // One folder per call (~1,150 per `dotnet test` run) used to stay behind (#1017).
+ static readonly string Root = Path.Combine(Path.GetTempPath(), "statecraft-fixture-" + Guid.NewGuid().ToString("N"));
+
+ public static void DeleteAll()
+ {
+ if (Directory.Exists(Root)) Directory.Delete(Root, recursive: true);
+ }
+
public static string Write(Dictionary<string, string>? overrides = null, Dictionary<string, string>? events = null)
{
- var dir = Path.Combine(Path.GetTempPath(), "statecraft-fixture-" + Guid.NewGuid().ToString("N"));
+ var dir = Path.Combine(Root, Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(Path.Combine(dir, "events"));
Directory.CreateDirectory(Path.Combine(dir, "map"));
foreach (var kv in Files)
@@ -296,3 +309,18 @@ sites:
return dir;
}
}
+
+// xunit 2 has no assembly teardown; the test framework is disposed once the run is over, while the
+// test host still waits for it. ProcessExit is too late: the host is killed mid-delete (measured: 845 of
+// 1,156 folders were still there). TestFramework.Dispose is not virtual, so IDisposable is re-implemented.
+public sealed class FixtureCleanupFramework : XunitTestFramework, IDisposable
+{
+ public FixtureCleanupFramework(IMessageSink sink) : base(sink) { }
+
+ void IDisposable.Dispose()
+ {
+ try { Fixture.DeleteAll(); }
+ catch (Exception e) when (e is IOException || e is UnauthorizedAccessException) { } // a file held open (antivirus on Windows) must not fail a green run
+ base.Dispose();
+ }
+}
diff --git a/sim/Statecraft.Sim.Tests/FixtureCleanupTests.cs b/sim/Statecraft.Sim.Tests/FixtureCleanupTests.cs
new file mode 100644
index 000000000..83ec3f9b4
--- /dev/null
+++ b/sim/Statecraft.Sim.Tests/FixtureCleanupTests.cs
@@ -0,0 +1,21 @@
+using System;
+using System.IO;
+using Xunit.Sdk;
+
+// Disposing the framework removes the whole per-run fixture root, so this must not run beside tests that
+// still read their fixture folders: a DisableParallelization collection runs after the parallel ones, alone.
+[CollectionDefinition("FixtureCleanup", DisableParallelization = true)]
+public class FixtureCleanupCollection { }
+
+[Collection("FixtureCleanup")]
+public class FixtureCleanupTests
+{
+ [Fact]
+ public void EndOfRunTeardownRemovesTheFoldersWriteCreated()
+ {
+ var dir = Fixture.Write();
+ Assert.True(Directory.Exists(dir));
+ ((IDisposable)new FixtureCleanupFramework(new NullMessageSink())).Dispose();
+ Assert.False(Directory.Exists(dir));
+ }
+}