diff --git a/app/pubspec.yaml b/app/pubspec.yaml index 1229d48..a1465f2 100644 --- a/app/pubspec.yaml +++ b/app/pubspec.yaml @@ -2,7 +2,7 @@ name: diet_guard_app description: "Companion phone app for diet_guard: log meals on the go." publish_to: 'none' -version: 1.0.190+1 +version: 1.0.191+1 environment: sdk: ^3.12.2 diff --git a/diet_guard/tests/test_test_changed_flutter.py b/diet_guard/tests/test_test_changed_flutter.py new file mode 100644 index 0000000..7a2e97b --- /dev/null +++ b/diet_guard/tests/test_test_changed_flutter.py @@ -0,0 +1,86 @@ +"""scripts/test_changed.sh runs flutter like a CI-mirror shard does. + +A bare ``flutter test`` on the shared /tmp stalled a 3-file run to 67 s under +the build queue (2026-11-21, perf.heavybuild) while the same files take ~4 s +alone; the shards fixed that with a private TMPDIR and two isolates, and the +targeted runner must do the same. +""" + +from __future__ import annotations + +import os +from pathlib import Path +import shutil +import subprocess + +REPO = Path(__file__).resolve().parents[2] +GIT = shutil.which("git") or "git" +BASH = shutil.which("bash") or "bash" +FAKE_FLUTTER = """#!/bin/bash +printf 'TMPDIR=%s\\n' "${TMPDIR:-}" >> "$FLUTTER_LOG" +printf 'ARGS=%s\\n' "$*" >> "$FLUTTER_LOG" +""" + + +def _shared_tmp(tmp_path: Path) -> Path: + """Stand-in for the machine-wide /tmp the caller would pass down.""" + shared = tmp_path / "shared-tmp" + shared.mkdir(exist_ok=True) + return shared + + +def _git(cwd: Path, *args: str) -> None: + subprocess.run([GIT, *args], cwd=cwd, check=True, capture_output=True) + + +def _run_test_changed(tmp_path: Path) -> dict[str, str]: + repo = tmp_path / "repo" + (repo / "scripts").mkdir(parents=True) + (repo / "app" / "test").mkdir(parents=True) + shutil.copy(REPO / "scripts" / "test_changed.sh", repo / "scripts") + _git(repo, "init", "-q") + _git( + repo, + "-c", + "user.name=t", + "-c", + "user.email=t@t", + "commit", + "-q", + "--allow-empty", + "-m", + "init", + ) + (repo / "app" / "test" / "a_test.dart").write_text("void main() {}\n") + bin_dir = tmp_path / "bin" + bin_dir.mkdir() + fake = bin_dir / "flutter" + fake.write_text(FAKE_FLUTTER) + fake.chmod(0o755) + log = tmp_path / "flutter.log" + env = { + **os.environ, + "PATH": f"{bin_dir}:{os.environ['PATH']}", + "FLUTTER_LOG": str(log), + "TMPDIR": str(_shared_tmp(tmp_path)), + } + subprocess.run( + [BASH, "scripts/test_changed.sh"], + cwd=repo, + env=env, + check=True, + capture_output=True, + ) + return dict(line.split("=", 1) for line in log.read_text().splitlines()) + + +def test_flutter_gets_a_private_tmpdir_that_is_removed(tmp_path: Path) -> None: + seen = _run_test_changed(tmp_path) + assert seen["TMPDIR"] not in {"", str(_shared_tmp(tmp_path))} + assert not Path(seen["TMPDIR"]).exists() + + +def test_flutter_runs_two_isolates_on_the_changed_file(tmp_path: Path) -> None: + seen = _run_test_changed(tmp_path) + assert "--concurrency=2" in seen["ARGS"] + assert "test/a_test.dart" in seen["ARGS"] diff --git a/scripts/test_changed.sh b/scripts/test_changed.sh index ebff096..a4d4a55 100755 --- a/scripts/test_changed.sh +++ b/scripts/test_changed.sh @@ -22,8 +22,16 @@ pytest_run() { } # Dart changes map to app/test/_test.dart; any other app/ change runs the app suite. +# A private TMPDIR and two isolates, as each ci_mirror.sh shard does: on the +# shared /tmp a 3-file run stalled to 67 s in the build queue (2026-11-21) +# against ~4 s alone, and the default isolate count overruns capped.sh's 2 GiB. flutter_run() { - (cd app && flutter test --reporter=compact "$@") + local tmp status=0 + tmp="$(mktemp -d "${XDG_CACHE_HOME:-$HOME/.cache}/diet-guard-flutter-test.XXXXXX")" + (cd app && TMPDIR="$tmp" flutter test --reporter=compact \ + --concurrency="${FLUTTER_TEST_CONCURRENCY:-2}" "$@") || status=$? + rm -rf -- "$tmp" + return "$status" } if grep -qs testmon pyproject.toml requirements*.txt; then