From f3e9d659fcc2719900fff935e7e109e20ab24e24 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Mon, 23 Nov 2026 07:31:23 +0000 Subject: [PATCH] fix(fleet): restore-row-count-diff.sh missing option value is a usage error (#12788) Under set -u, '--manifest' with no value crashed with 'line 44: $2: unbound variable' (exit 1), and '--manifest --live /path' silently consumed --live as the manifest value. Parse strictly per the operator-script argument contract (#3352): a value option with no value, or a value that begins with '--', prints the usage message and exits 2. Adds scripts/tests/test_restore_row_count_diff.py (fails before, passes after), wired into scripts-test.yml and the test-suite registry following the provider-retirement-gate pattern. --- .github/test-suite-registry.json | 13 ++++ .github/workflows/scripts-test.yml | 4 ++ scripts/fleet/restore-row-count-diff.sh | 29 +++++---- scripts/tests/test_restore_row_count_diff.py | 68 ++++++++++++++++++++ 4 files changed, 103 insertions(+), 11 deletions(-) create mode 100644 scripts/tests/test_restore_row_count_diff.py diff --git a/.github/test-suite-registry.json b/.github/test-suite-registry.json index 64362da9ed..c11beee7db 100644 --- a/.github/test-suite-registry.json +++ b/.github/test-suite-registry.json @@ -1115,6 +1115,19 @@ "related_issues": [5610, 7789], "reason": "Wired by #7789: scripts-test.yml names the suite explicitly, following the sibling pattern for this directory. The workflow is active (the scripts.direct-python row's disabled_manually note is stale registry state for #6187 to refresh)." }, + { + "id": "scripts.restore-row-count-diff", + "paths": ["scripts/tests/test_restore_row_count_diff.py"], + "invocation": "python3 -m unittest discover -s scripts/tests -p 'test_restore_row_count_diff.py' -v", + "venue": { + "workflow": ".github/workflows/scripts-test.yml", + "job_step": "restore row-count diff contract tests (#12788)", + "state": "active", + "dispatchable": true + }, + "status": "mapped", + "related_issues": [12788] + }, { "id": "scripts.research-workflow", "paths": [ diff --git a/.github/workflows/scripts-test.yml b/.github/workflows/scripts-test.yml index 8e7cb8af3d..253be654c6 100644 --- a/.github/workflows/scripts-test.yml +++ b/.github/workflows/scripts-test.yml @@ -44,6 +44,10 @@ jobs: python3 -m unittest discover -s scripts/tests -p 'test_hardware_doc_init.py' -v - name: Run provider retirement gate contract tests (#5610) run: python3 -m unittest discover -s scripts/tests -p 'test_check_provider_retirement_gate.py' -v + - name: Run restore row-count diff contract tests (#12788) + run: | + test -f scripts/tests/test_restore_row_count_diff.py + python3 -m unittest discover -s scripts/tests -p 'test_restore_row_count_diff.py' -v - name: Run auto-format workflow contract tests (#7664) run: python3 -m unittest discover -s scripts/tests -p 'test_auto_format_workflow.py' -v - name: Run power-guard timing validation tests (#6477) diff --git a/scripts/fleet/restore-row-count-diff.sh b/scripts/fleet/restore-row-count-diff.sh index 7bd300a0e1..02898cc086 100755 --- a/scripts/fleet/restore-row-count-diff.sh +++ b/scripts/fleet/restore-row-count-diff.sh @@ -33,23 +33,30 @@ # Usage: # restore-row-count-diff.sh --manifest MANIFEST.tsv --live LIVE.tsv # [--inventory INVENTORY.txt] # Exit: 0 = gate passes, 1 = gate fails, 2 = usage/input error. +# The command line is parsed strictly (#3352): a value option with no +# value, or with a value that is itself an option, is a usage error. set -euo pipefail +usage() { + echo "usage: restore-row-count-diff.sh --manifest M.tsv --live L.tsv [--inventory I.txt]" >&2 +} + manifest="" live="" inventory="" while [ $# -gt 0 ]; do case "$1" in - --manifest) - manifest="$2" - shift 2 - ;; - --live) - live="$2" - shift 2 - ;; - --inventory) - inventory="$2" + --manifest | --live | --inventory) + if [ $# -lt 2 ] || [ "${2#--}" != "$2" ]; then + echo "option $1 requires a value" >&2 + usage + exit 2 + fi + case "$1" in + --manifest) manifest="$2" ;; + --live) live="$2" ;; + --inventory) inventory="$2" ;; + esac shift 2 ;; *) @@ -59,7 +66,7 @@ while [ $# -gt 0 ]; do esac done [ -n "$manifest" ] && [ -n "$live" ] || { - echo "usage: restore-row-count-diff.sh --manifest M.tsv --live L.tsv [--inventory I.txt]" >&2 + usage exit 2 } [ -f "$manifest" ] && [ -f "$live" ] || { diff --git a/scripts/tests/test_restore_row_count_diff.py b/scripts/tests/test_restore_row_count_diff.py new file mode 100644 index 0000000000..e395fb3722 --- /dev/null +++ b/scripts/tests/test_restore_row_count_diff.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python3 +"""Contract tests for restore-row-count-diff.sh (#12788). + +Oracle: the operator-script argument contract +(doc/agents/operator-script-argument-contract.md, #3352) and the +script's own header — a value option with no value, or with a value +that is itself an option, is a usage error: the usage message on +stderr and exit 2, never a raw bash error. A well-formed invocation +still runs the gate. +""" + +import subprocess +import tempfile +import unittest +from pathlib import Path + + +ROOT = Path(__file__).resolve().parents[2] +SCRIPT = ROOT / "scripts" / "fleet" / "restore-row-count-diff.sh" + + +def run_diff(args): + return subprocess.run( + ["bash", str(SCRIPT), *args], + text=True, + capture_output=True, + check=False, + ) + + +class TestRestoreRowCountDiffArgs(unittest.TestCase): + def test_missing_option_value_is_a_usage_error(self): + for option in ("--manifest", "--live", "--inventory"): + with self.subTest(option=option): + result = run_diff([option]) + self.assertEqual(result.returncode, 2, result.stderr) + self.assertIn("usage:", result.stderr) + self.assertNotIn("unbound variable", result.stderr) + + def test_option_looking_value_is_not_consumed(self): + result = run_diff(["--manifest", "--live", "/path"]) + self.assertEqual(result.returncode, 2, result.stderr) + self.assertIn("usage:", result.stderr) + self.assertNotIn("unknown argument: /path", result.stderr) + + def test_unknown_argument_is_a_usage_error(self): + result = run_diff(["--bogus"]) + self.assertEqual(result.returncode, 2, result.stderr) + self.assertIn("unknown argument: --bogus", result.stderr) + + def test_matching_counts_pass_the_gate(self): + with tempfile.TemporaryDirectory(prefix="row-count-diff-") as tmp: + manifest = Path(tmp) / "manifest.tsv" + manifest.write_text( + "table\tsource_count\tdisposition\tallowed_delta\tnote\n" + "t1\t5\tload\t0\t\n" + ) + live = Path(tmp) / "live.tsv" + live.write_text("table\tlive_count\nt1\t5\n") + result = run_diff( + ["--manifest", str(manifest), "--live", str(live)] + ) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertIn("GATE PASSED", result.stdout) + + +if __name__ == "__main__": + unittest.main()