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.

CI and check.sh: a dotnet test run that finds no tests fails (part 2 of #2532) (#3011)

Eyal Golan0b05e0380dda
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index f80cb981a..0c8b03f8a 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -38,8 +38,9 @@ jobs:
- uses: actions/setup-dotnet@a98b56852c35b8e3190ac28c8c2271da59106c68 # v6.0.0
with:
dotnet-version: "10.0.401"
+ # VSTest exits 0 when it finds no tests; TreatNoTestsAsError makes a break in test discovery fail (#2532).
- name: Simulation and content tests
- run: dotnet test sim/Statecraft.sln -c Release
+ run: dotnet test sim/Statecraft.sln -c Release --nologo -- RunConfiguration.TreatNoTestsAsError=true
tools:
name: Tools (pytest)
diff --git a/scripts/check.sh b/scripts/check.sh
index 3bb9053ea..c82a2592b 100755
--- a/scripts/check.sh
+++ b/scripts/check.sh
@@ -64,7 +64,8 @@ elif [ ! -d unity/Library ]; then
else
run "unity compile (dotnet, outside Unity)" scripts/unity_compile/compile.sh "$unity_contents"
fi
-run "sim and content tests" dotnet test sim/Statecraft.sln -c Release --nologo
+# TreatNoTestsAsError: zero tests found is a failure, not a pass (#2532); pytest already exits 5 on no tests.
+run "sim and content tests" dotnet test sim/Statecraft.sln -c Release --nologo -- RunConfiguration.TreatNoTestsAsError=true
run "data tool tests" uv run --with pytest==9.1.1 --with rasterio==1.5.2 --with numpy==2.5.3 --with pyyaml==6.0.3 --with pillow==12.3.0 --with requests==2.32.5 --with pyproj==3.8.0 pytest tools/tests -q
echo
diff --git a/scripts/oss/release_gate.sh b/scripts/oss/release_gate.sh
index c68b9b7d3..10a70bf8d 100755
--- a/scripts/oss/release_gate.sh
+++ b/scripts/oss/release_gate.sh
@@ -25,7 +25,8 @@ step "policy tests"
uv run --with pytest==9.1.1 --with pyyaml==6.0.3 --with requests==2.32.5 pytest scripts/oss/tests -q || fail=1
step "simulation and content tests"
-dotnet test sim/Statecraft.sln -c Release --nologo -v q || fail=1
+# TreatNoTestsAsError: zero tests found is a failure, not a pass (#2532).
+dotnet test sim/Statecraft.sln -c Release --nologo -v q -- RunConfiguration.TreatNoTestsAsError=true || fail=1
step "sensitivity gate on content/ (decision D11; Statecraft.Cli --release-gate)"
dotnet run --project sim/Statecraft.Cli -v q -- --release-gate --content "$PWD/content" || fail=1
diff --git a/scripts/oss/tests/test_policies.py b/scripts/oss/tests/test_policies.py
index 351873546..7bd2085a4 100644
--- a/scripts/oss/tests/test_policies.py
+++ b/scripts/oss/tests/test_policies.py
@@ -62,6 +62,42 @@ def test_check_script_runs_every_ci_command():
assert f'UV_PYTHON:-{ci["env"]["UV_PYTHON"]}' in script
+def test_a_test_step_that_runs_zero_tests_fails():
+ # #2532: a break in test discovery (or a filter that matches nothing) must not pass. VSTest exits 0 on zero tests
+ # unless RunConfiguration.TreatNoTestsAsError is set; pytest already exits 5 (no tests collected), so the guard there
+ # is that nothing hides its exit code.
+ import yaml
+ ci = yaml.safe_load(read(".github/workflows/ci.yml"))
+ script = read("scripts/check.sh")
+ steps = [s for job in ci["jobs"].values() for s in job["steps"] if "run" in s]
+ script_lines = [l for l in script.splitlines() if not l.lstrip().startswith("#")]
+ # The release gate collects failures with `|| fail=1` and refuses at the end; that keeps the exit code.
+ gate_lines = [l.removesuffix(" || fail=1") for l in read("scripts/oss/release_gate.sh").splitlines()
+ if not l.lstrip().startswith("#")]
+ dotnet = [s["run"] for s in steps if "dotnet test" in s["run"]] + [
+ l for l in script_lines + gate_lines if "dotnet test" in l]
+ pytest_steps = [s for s in steps if "pytest " in s["run"]]
+ pytest_lines = [l for l in script_lines + gate_lines if "pytest " in l]
+ assert len(dotnet) == 3 and len(pytest_steps) == 2 and len(pytest_lines) == 4
+ for cmd in dotnet:
+ assert cmd.rstrip().endswith(" -- RunConfiguration.TreatNoTestsAsError=true"), cmd
+ for step in pytest_steps:
+ assert not step.get("continue-on-error"), step["name"]
+ for cmd in [s["run"] for s in pytest_steps] + pytest_lines:
+ assert "|| true" not in cmd and "suppress-no-test-exit-code" not in cmd and "; exit 0" not in cmd, cmd
+
+
+def test_pytest_fails_when_it_collects_no_tests(tmp_path):
+ # The guard the CI pytest steps rely on: pytest's own exit code 5 when nothing is collected.
+ import subprocess
+ import sys
+ import pytest
+ (tmp_path / "test_nothing.py").write_text("# no tests here\n", encoding="utf-8")
+ done = subprocess.run([sys.executable, "-m", "pytest", str(tmp_path), "-q", "-p", "no:cacheprovider"],
+ cwd=tmp_path, capture_output=True, text=True, timeout=120)
+ assert done.returncode == pytest.ExitCode.NO_TESTS_COLLECTED == 5, done.stdout + done.stderr
+
+
def test_changelog_has_unreleased_on_top():
text = read("CHANGELOG.md")
assert text.index("## [Unreleased]") == text.index("## [")