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.

Remove the population-hash test's temporary content copy (#1199)

Aviel Cohen9c231f97ddf8
diff --git a/sim/Statecraft.Sim.Tests/SaveGameTests.cs b/sim/Statecraft.Sim.Tests/SaveGameTests.cs
index 88608c540..78b785676 100644
--- a/sim/Statecraft.Sim.Tests/SaveGameTests.cs
+++ b/sim/Statecraft.Sim.Tests/SaveGameTests.cs
@@ -415,14 +415,18 @@ public class SaveGameTests
var real = RealContentTests.ContentRoot();
Assert.True(File.Exists(Path.Combine(real, "map", "settlement_population_1945.yaml")));
var copy = Path.Combine(Path.GetTempPath(), "statecraft-nopop-" + Guid.NewGuid().ToString("N"));
- foreach (var f in Directory.GetFiles(real, "*", SearchOption.AllDirectories))
+ try
{
- var rel = Path.GetRelativePath(real, f);
- if (rel.Replace('\\', '/') == "map/settlement_population_1945.yaml") continue;
- Directory.CreateDirectory(Path.GetDirectoryName(Path.Combine(copy, rel))!);
- File.Copy(f, Path.Combine(copy, rel));
+ foreach (var f in Directory.GetFiles(real, "*", SearchOption.AllDirectories))
+ {
+ var rel = Path.GetRelativePath(real, f);
+ if (rel.Replace('\\', '/') == "map/settlement_population_1945.yaml") continue;
+ Directory.CreateDirectory(Path.GetDirectoryName(Path.Combine(copy, rel))!);
+ File.Copy(f, Path.Combine(copy, rel));
+ }
+ Assert.Equal(ContentLoader.Load(real).Hash, ContentLoader.Load(copy).Hash);
}
- Assert.Equal(ContentLoader.Load(real).Hash, ContentLoader.Load(copy).Hash);
+ finally { if (Directory.Exists(copy)) Directory.Delete(copy, true); }
}
/// <summary>content/map/localities_1948.yaml (the 1948 localities and Bedouin camp areas the map's kits and labels draw)
diff --git a/tools/tests/test_population_hash_cleanup.py b/tools/tests/test_population_hash_cleanup.py
new file mode 100644
index 000000000..d28ebef6a
--- /dev/null
+++ b/tools/tests/test_population_hash_cleanup.py
@@ -0,0 +1,74 @@
+"""Run the real population-hash test in isolation; its content copy must never survive.
+
+Faults are injected only into a private source copy. This keeps the tested path real without
+adding failure hooks to the C# test or touching another test process's temporary files.
+"""
+import os
+from pathlib import Path
+import shutil
+import subprocess
+import tempfile
+import xml.etree.ElementTree as ET
+
+import pytest
+
+
+REPO = Path(__file__).resolve().parents[2]
+TEST = "SaveGameTests.TheKitPopulationFileIsOutsideTheContentHash"
+
+
[email protected]("fault", [None, "copy", "load", "assertion"])
+def test_population_hash_check_cleans_its_copy_on_success_and_failure(tmp_path, fault):
+ with tempfile.TemporaryDirectory(prefix="population-cleanup-", dir=tmp_path) as directory:
+ root = Path(directory)
+ repo = root / "repo"
+ shutil.copytree(REPO / "sim", repo / "sim", ignore=shutil.ignore_patterns("bin", "obj", "TestResults"))
+ shutil.copytree(REPO / "content", repo / "content")
+
+ source = repo / "sim/Statecraft.Sim.Tests/SaveGameTests.cs"
+ text = source.read_text(encoding="utf-8")
+ start = text.index(" public void TheKitPopulationFileIsOutsideTheContentHash()")
+ end = text.index("\n /// <summary>", start)
+ body = text[start:end]
+ if fault == "copy":
+ target = "File.Copy(f, Path.Combine(copy, rel));"
+ replacement = target + '\n if (File.Exists(Path.Combine(copy, rel))) throw new IOException("population cleanup copy probe");'
+ else:
+ target = "Assert.Equal(ContentLoader.Load(real).Hash, ContentLoader.Load(copy).Hash);"
+ if fault == "load":
+ replacement = 'File.WriteAllText(Path.Combine(copy, "rules.yaml"), "invalid: [");\n ' + target
+ elif fault == "assertion":
+ replacement = 'File.AppendAllText(Path.Combine(copy, "rules.yaml"), "\\n# population cleanup assertion probe\\n");\n ' + target
+ else:
+ replacement = target
+ assert body.count(target) == 1, "the real population-hash test path must be selected exactly once"
+ source.write_text(text[:start] + body.replace(target, replacement) + text[end:], encoding="utf-8")
+
+ temporary = root / "temporary"
+ temporary.mkdir()
+ sentinel = temporary / "statecraft-nopop-another-run"
+ sentinel.mkdir()
+ (sentinel / "keep.txt").write_text("another run's data", encoding="utf-8")
+ results = root / "results"
+ run = subprocess.run(
+ ["dotnet", "test", str(repo / "sim/Statecraft.sln"), "-c", "Release", "--nologo",
+ "--filter", f"FullyQualifiedName={TEST}", "--logger", "trx;LogFileName=population.trx",
+ "--results-directory", str(results)],
+ cwd=repo, env={**os.environ, "TMPDIR": str(temporary), "TMP": str(temporary), "TEMP": str(temporary)},
+ capture_output=True, text=True, timeout=180,
+ )
+ output = run.stdout + run.stderr
+ report = results / "population.trx"
+ assert report.is_file(), output
+ counters = ET.parse(report).find(".//{*}Counters")
+ assert counters is not None and counters.get("executed") == "1", output
+ assert counters.get("total") == "1", output
+ assert "warning CS" not in output, output
+ assert run.returncode == (0 if fault is None else 1), output
+ assert counters.get("passed" if fault is None else "failed") == "1", output
+ if fault is not None:
+ expected = {"copy": "population cleanup copy probe", "load": "rules.yaml", "assertion": "Assert.Equal() Failure"}[fault]
+ assert expected in output + report.read_text(encoding="utf-8"), output
+ assert (sentinel / "keep.txt").read_text(encoding="utf-8") == "another run's data"
+ survivors = sorted(p.name for p in temporary.glob("statecraft-nopop-*") if p != sentinel)
+ assert survivors == [], f"population-hash test left its own content copy behind: {survivors}"