|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +if [[ $# -lt 3 || $# -gt 4 ]]; then |
| 6 | + echo "Usage: $0 <project> <zero-based-shard-index> <shard-count> [configuration]" >&2 |
| 7 | + exit 2 |
| 8 | +fi |
| 9 | + |
| 10 | +project=$1 |
| 11 | +shard_index=$2 |
| 12 | +shard_count=$3 |
| 13 | +configuration=${4:-Release} |
| 14 | + |
| 15 | +if ! [[ $shard_index =~ ^[0-9]+$ && $shard_count =~ ^[1-9][0-9]*$ ]]; then |
| 16 | + echo "Shard index and count must be non-negative integers, with a positive count." >&2 |
| 17 | + exit 2 |
| 18 | +fi |
| 19 | + |
| 20 | +if (( shard_index >= shard_count )); then |
| 21 | + echo "Shard index $shard_index is outside the $shard_count available shards." >&2 |
| 22 | + exit 2 |
| 23 | +fi |
| 24 | + |
| 25 | +dotnet build "$project" --configuration "$configuration" --nologo |
| 26 | + |
| 27 | +tests=() |
| 28 | +while IFS= read -r test_name; do |
| 29 | + tests+=("$test_name") |
| 30 | +done < <( |
| 31 | + dotnet test "$project" \ |
| 32 | + --configuration "$configuration" \ |
| 33 | + --nologo \ |
| 34 | + --no-build \ |
| 35 | + --list-tests | |
| 36 | + awk ' |
| 37 | + /The following Tests are available:/ { capture = 1; next } |
| 38 | + capture && /^ / { |
| 39 | + sub(/^[[:space:]]+/, "") |
| 40 | + sub(/[[:space:]]+$/, "") |
| 41 | + print |
| 42 | + } |
| 43 | + ' |
| 44 | +) |
| 45 | + |
| 46 | +if (( ${#tests[@]} == 0 )); then |
| 47 | + echo "No tests were discovered in $project." >&2 |
| 48 | + exit 1 |
| 49 | +fi |
| 50 | + |
| 51 | +duplicate_names=$(printf '%s\n' "${tests[@]}" | sort | uniq -d) |
| 52 | +if [[ -n $duplicate_names ]]; then |
| 53 | + echo "Test names must be unique for name-based sharding. Duplicates:" >&2 |
| 54 | + echo "$duplicate_names" >&2 |
| 55 | + exit 1 |
| 56 | +fi |
| 57 | + |
| 58 | +filters=() |
| 59 | +for test_index in "${!tests[@]}"; do |
| 60 | + if (( test_index % shard_count == shard_index )); then |
| 61 | + filters+=("Name=${tests[$test_index]}") |
| 62 | + fi |
| 63 | +done |
| 64 | + |
| 65 | +if (( ${#filters[@]} == 0 )); then |
| 66 | + echo "Shard $shard_index has no tests." >&2 |
| 67 | + exit 1 |
| 68 | +fi |
| 69 | + |
| 70 | +filter=$(IFS='|'; printf '%s' "${filters[*]}") |
| 71 | +printf 'Running shard %d of %d with %d of %d tests.\n' \ |
| 72 | + "$((shard_index + 1))" \ |
| 73 | + "$shard_count" \ |
| 74 | + "${#filters[@]}" \ |
| 75 | + "${#tests[@]}" |
| 76 | + |
| 77 | +dotnet test "$project" \ |
| 78 | + --configuration "$configuration" \ |
| 79 | + --nologo \ |
| 80 | + --logger GitHubActions \ |
| 81 | + --no-build \ |
| 82 | + --filter "$filter" |
0 commit comments