beads/.github/scripts/embedded-test-shard.sh
Julian Knutsen 8988340994
ci(embedded): run every BEADS_TEST_EMBEDDED_DOLT-gated cmd/bd test in the embedded lane (#7708)
* test(cmd/bd): repair the embedded-gated tests that rotted while no lane ran them

These tests skip unless BEADS_TEST_EMBEDDED_DOLT=1 but no CI lane selected
them, so they rotted unseen. Running them shows:

- TestEventsTailFollowReportsTruncationOnTheStream raced on a plain
  bytes.Buffer that os/exec's copy goroutine writes while the test polls it.
  Use the package's syncBuffer.
- TestCorruptMetadataDiagnosticsRunAndDataFailsLoud still asserted that
  doctor and init run on a corrupt metadata.json. #4857 deliberately made
  every workspace command refuse instead (pinned for doctor by
  TestDoctorRejectsCorruptMetadataBeforeSharedServerChecks). Rewrite it to
  the shipped contract as TestEmbeddedCorruptMetadataFailsLoudUntilRestored:
  version warns, doctor/list/init refuse naming the file without touching
  it, and restoring the file brings the data back.
- dolt_remote_origin_guard_embedded_test.go was tagged
  `embeddeddolt && cgo`, a tag no lane sets, so it never compiled in CI
  (gazelle left it out of bd_test srcs). Retag it `cgo`, as #4153 did for
  its dolt_local_only sibling, and add it to srcs. Drop its four bd doctor
  git-origin tests: bd doctor runs no checks on an embedded workspace
  (GH#3794), so two could never pass and two passed vacuously on empty
  output. Rename the surviving tests into the TestEmbedded* prefix.

Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of Julian Knutsen
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* ci(embedded): run every BEADS_TEST_EMBEDDED_DOLT-gated cmd/bd test in the embedded lane

The embedded-Dolt cmd lane selected tests with
grep '^func TestEmbedded' cmd/bd/*_embedded_test.go, and only its two
Bazel targets set BEADS_TEST_EMBEDDED_DOLT. A test that skips without the
variable but missed the name prefix or the file suffix skipped in every
lane and reported green: 92 of them, including #7203's --if-revision CLI
coverage (TestIfRevisionMatchAndMismatch, TestReassignFenceCLI, ...).

- embedded-test-shard.sh and gen_embedded_cmd_shard_manifest.py select
  TestEmbedded* by name across cmd/bd/*_test.go, as proxied-test-shard.sh
  does, leaving authors one rule.
- Rename the gated tests into the TestEmbedded* prefix
  (TestDoctorEmbedded<X> -> TestEmbeddedDoctor<X>) and update every
  reference.
- Place the new names with gen_embedded_cmd_shard_manifest.py 100
  --weights=duration --write (incremental).
- Add TestEmbeddedGatedTestsRunInTheEmbeddedLane: it walks cmd/bd's tests
  for any that reach os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" and fails
  if a shard of the lane never lists one. The proxied guard's helpers now
  take the variable and the script; a fixture test pins the walk.

Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of Julian Knutsen
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-19 22:02:22 +00:00

139 lines
4.7 KiB
Bash
Executable file

#!/usr/bin/env bash
# embedded-test-shard.sh — run a shard of embedded dolt cmd/bd tests.
#
# Usage: embedded-test-shard.sh <shard_number> <total_shards>
#
# Discovers all TestEmbedded* top-level functions in cmd/bd/*_test.go.
# Tests listed in embedded-cmd-test-shards.txt for the requested shard count use
# that committed assignment; newly-added tests fall back to hash(name) % total.
# Runs the matching subset using the pre-built test binary at
# BEADS_TEST_CMD_BINARY (or /tmp/bd-cmd-test).
#
# Environment:
# BEADS_TEST_EMBEDDED_DOLT=1 required (tests skip without it)
# BEADS_TEST_BD_BINARY=<path> optional pre-built bd binary (used by tests)
# BEADS_TEST_CMD_BINARY=<path> pre-built cmd/bd test binary (default: /tmp/bd-cmd-test)
# BEADS_TEST_SHARD_LIST_ONLY=1 print selected tests without running them
set -euo pipefail
SHARD_NUMBER="${1:?usage: $0 <shard_number> <total_shards>}"
TOTAL_SHARDS="${2:?usage: $0 <shard_number> <total_shards>}"
shift 2
if ! [[ "$SHARD_NUMBER" =~ ^[0-9]+$ ]] || ! [[ "$TOTAL_SHARDS" =~ ^[0-9]+$ ]]; then
echo "Shard number and total shards must be positive integers" >&2
exit 1
fi
if (( TOTAL_SHARDS < 1 || SHARD_NUMBER < 1 || SHARD_NUMBER > TOTAL_SHARDS )); then
echo "Invalid shard ${SHARD_NUMBER}/${TOTAL_SHARDS}" >&2
exit 1
fi
SHARD_INDEX=$(( SHARD_NUMBER - 1 ))
MANIFEST="${BEADS_TEST_SHARD_MANIFEST:-.github/scripts/embedded-cmd-test-shards.txt}"
# Discover all top-level TestEmbedded* functions across every cmd/bd test
# file, by name rather than by file glob (as proxied-test-shard.sh does): a
# test gated on BEADS_TEST_EMBEDDED_DOLT runs in this lane or in none, wherever
# it lives. scripts/embedded_lane_coverage_test.go fails on a gated test this
# selection misses.
ALL_TESTS=$(grep -rh '^func TestEmbedded' cmd/bd/*_test.go \
| sed 's/func \(TestEmbedded[A-Za-z0-9_]*\).*/\1/' \
| sort -u)
if [ -z "$ALL_TESTS" ]; then
echo "No TestEmbedded* functions found" >&2
exit 1
fi
declare -A ALL_TEST_SET=()
while IFS= read -r name; do
ALL_TEST_SET["$name"]=1
done <<< "$ALL_TESTS"
declare -A MANIFEST_SHARDS=()
if [ -f "$MANIFEST" ]; then
while IFS= read -r line || [ -n "$line" ]; do
line="${line%%#*}"
read -r manifest_total manifest_shard test_name extra <<< "$line"
if [ -z "${manifest_total:-}" ]; then
continue
fi
if [ -n "${extra:-}" ] || ! [[ "$manifest_total" =~ ^[0-9]+$ ]] || ! [[ "$manifest_shard" =~ ^[0-9]+$ ]] || ! [[ "$test_name" =~ ^TestEmbedded[A-Za-z0-9_]+$ ]]; then
echo "Invalid shard manifest line: $line" >&2
exit 1
fi
if (( manifest_total != TOTAL_SHARDS )); then
continue
fi
if (( manifest_shard < 1 || manifest_shard > TOTAL_SHARDS )); then
echo "Invalid shard ${manifest_shard}/${manifest_total} for $test_name in $MANIFEST" >&2
exit 1
fi
if [ -n "${MANIFEST_SHARDS[$test_name]:-}" ]; then
echo "Duplicate shard manifest entry for $test_name in $MANIFEST" >&2
exit 1
fi
MANIFEST_SHARDS["$test_name"]="$manifest_shard"
done < "$MANIFEST"
fi
for name in "${!MANIFEST_SHARDS[@]}"; do
if [ -z "${ALL_TEST_SET[$name]:-}" ]; then
echo "Shard manifest entry does not match a discovered test: $name" >&2
exit 1
fi
done
SHARD_TESTS=()
MANIFEST_COUNT=0
FALLBACK_COUNT=0
while IFS= read -r name; do
assigned_shard="${MANIFEST_SHARDS[$name]:-}"
if [ -n "$assigned_shard" ]; then
if (( assigned_shard == SHARD_NUMBER )); then
SHARD_TESTS+=("$name")
MANIFEST_COUNT=$(( MANIFEST_COUNT + 1 ))
fi
continue
fi
# Use cksum for a portable numeric hash fallback for newly-added tests.
hash=$(printf %s "$name" | cksum | cut -d ' ' -f 1)
if (( hash % TOTAL_SHARDS == SHARD_INDEX )); then
SHARD_TESTS+=("$name")
FALLBACK_COUNT=$(( FALLBACK_COUNT + 1 ))
fi
done <<< "$ALL_TESTS"
if [ ${#SHARD_TESTS[@]} -eq 0 ]; then
echo "Shard ${SHARD_NUMBER}/${TOTAL_SHARDS}: no tests assigned (all hashed to other shards)"
exit 0
fi
# Build the -run regex: "^(TestA|TestB|TestC)$"
RUN_REGEX="^($(IFS='|'; echo "${SHARD_TESTS[*]}"))$"
echo "Shard ${SHARD_NUMBER}/${TOTAL_SHARDS}: running ${#SHARD_TESTS[@]} test(s)"
echo " manifest: ${MANIFEST_COUNT}, fallback: ${FALLBACK_COUNT}"
printf " %s\n" "${SHARD_TESTS[@]}"
echo ""
# Use pre-built test binary if available, otherwise fall back to go test.
CMD_BINARY="${BEADS_TEST_CMD_BINARY:-/tmp/bd-cmd-test}"
if [ "${BEADS_TEST_SHARD_LIST_ONLY:-}" = "1" ]; then
exit 0
fi
if [ -x "$CMD_BINARY" ]; then
exec "$CMD_BINARY" -test.v -test.count=1 -test.timeout=20m \
-test.run "$RUN_REGEX" \
"$@"
else
echo "Warning: pre-built test binary not found at $CMD_BINARY, falling back to go test"
exec go test -tags=gms_pure_go -v -race -count=1 -timeout 20m \
-run "$RUN_REGEX" \
"$@" \
./cmd/bd/
fi