Skip to content

Commit e3a4012

Browse files
Test script: 4x speed increase and improved LLM-friendly output (#278)
* Clean up output of the run_all_tests_and_fixes tool * parallelize * Make it less noisy * Improve printing of results * fix: Address PR feedback * fix: Address PR feedback
1 parent 5f9e1d7 commit e3a4012

File tree

1 file changed

+97
-43
lines changed

1 file changed

+97
-43
lines changed

tool/run_all_tests_and_fixes.sh

Lines changed: 97 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -21,78 +21,132 @@ command -v flutter >/dev/null 2>&1 || { echo >&2 "Error: 'flutter' command not f
2121
# calls for each one.
2222

2323
FAILURE_LOG=$(mktemp)
24-
trap 'rm -f "$FAILURE_LOG"' EXIT
24+
LOG_DIR=$(mktemp -d)
25+
trap 'rm -f "$FAILURE_LOG"; rm -rf "$LOG_DIR"' EXIT
2526

26-
readonly PROJECT_TOTAL_STEPS=3
27+
readonly PROJECT_TOTAL_STEPS=4
2728

2829
run_project_step() {
2930
local project_dir="$1"
3031
local description="$2"
3132
local step_num="$3"
3233
shift 3
33-
local cmd_str=$(printf '%q ' "$@"); cmd_str=${cmd_str% }
34-
35-
echo "[$step_num/$PROJECT_TOTAL_STEPS] $description..."
36-
echo "To rerun this command:"
37-
echo "cd \"$project_dir\" && $cmd_str"
38-
if ! "$@"; then
39-
echo "'$cmd_str' failed in \"$project_dir\"" >> "$FAILURE_LOG"
34+
local cmd_to_run=($@)
35+
local cmd_str_to_run=$(printf '%q ' "${cmd_to_run[@]}"); cmd_str_to_run=${cmd_str_to_run% }
36+
37+
# Create a version of the command for display, removing the reporter flag.
38+
local cmd_for_display=()
39+
for arg in "${cmd_to_run[@]}"; do
40+
if [[ "$arg" != "--reporter=failures-only" ]]; then
41+
cmd_for_display+=("$arg")
42+
fi
43+
done
44+
local cmd_str_for_display=$(printf '%q ' "${cmd_for_display[@]}"); cmd_str_for_display=${cmd_str_for_display% }
45+
46+
echo ""
47+
echo "### [$step_num/$PROJECT_TOTAL_STEPS] $description"
48+
echo "> To rerun this command:"
49+
echo ">
50+
> (cd \"$project_dir\" && $cmd_str_for_display)
51+
> "
52+
if ! "${cmd_to_run[@]}"; then
53+
echo "'(cd \"$project_dir\" && $cmd_str_to_run)' failed" >> "$FAILURE_LOG"
4054
fi
4155
}
4256

57+
process_project() {
58+
local project_dir="$1"
59+
(
60+
echo "## Processing project in: \`$project_dir\`"
61+
echo "---"
62+
63+
# Navigate into the project's directory.
64+
cd "$project_dir" || exit 1
65+
66+
run_project_step "$project_dir" "Applying fixes with 'dart fix --apply'" 1 dart fix --apply
67+
run_project_step "$project_dir" "Formatting with 'dart format .'" 2 dart format .
68+
if [ -d "test" ]; then
69+
run_project_step "$project_dir" "Running tests with 'flutter test'" 3 flutter test --reporter=failures-only
70+
else
71+
echo ""
72+
echo "### [3/$PROJECT_TOTAL_STEPS] Skipping tests, no 'test' directory found."
73+
fi
74+
run_project_step "$project_dir" "Analyzing code with 'flutter analyze'" 4 flutter analyze
75+
76+
echo ""
77+
echo "---"
78+
echo "Finished processing \`$project_dir\`."
79+
echo ""
80+
)
81+
}
4382

4483
# --- 0. Run commands at the root project level ---
45-
echo "Running root-level commands..."
46-
echo "--------------------------------------------------"
84+
echo "## Running root-level commands"
85+
echo "---"
4786
# Check if the copyright tool exists before running
4887
if [ -f "tool/fix_copyright/bin/fix_copyright.dart" ]; then
49-
echo "Running copyright fix. To rerun:"
50-
echo "dart run tool/fix_copyright/bin/fix_copyright.dart --force"
88+
echo "### Running copyright fix"
89+
echo "> To rerun this command:"
90+
echo ">
91+
> dart run tool/fix_copyright/bin/fix_copyright.dart --force
92+
> "
5193
# Log failures without stopping the script.
52-
dart run tool/fix_copyright/bin/fix_copyright.dart --force || echo "'dart run tool/fix_copyright/bin/fix_copyright.dart --force' failed" >> "$FAILURE_LOG"
94+
dart run tool/fix_copyright/bin/fix_copyright.dart --force >/dev/null 2>&1 || true
5395
else
54-
echo "Warning: Copyright tool not found. Skipping."
96+
97+
echo "### Skipping copyright fix: tool not found."
5598
fi
56-
# Log failures without stopping the script.
57-
echo "Running dart format. To rerun:"
58-
echo "dart format ."
59-
dart format . || echo "'dart format .' failed" >> "$FAILURE_LOG"
99+
echo "---"
60100
echo "Root-level commands complete."
61101
echo ""
62102

63103
# --- 1. Find all Flutter projects ---
64104
# We find all `pubspec.yaml` files and process each one.
65105
# The `find ... -print0 | while ...` construct safely handles file paths with spaces.
66106
echo "Searching for Flutter projects..."
67-
echo "=================================================="
68-
find . -name "build" -type d -prune -o -name ".dart_tool" -type d -prune -o -path "./melos_tool" -prune -o -name "pubspec.yaml" -exec grep -q 'sdk: flutter' {} \; -print0 | while IFS= read -r -d '' pubspec_path; do
69-
(
70-
# Get the directory containing the pubspec.yaml file.
71-
project_dir=$(dirname "$pubspec_path")
72-
73-
echo "Processing project in: $project_dir"
74-
echo "--------------------------------------------------"
75107

76-
# Navigate into the project's directory.
77-
cd "$project_dir" || exit 1
78-
79-
run_project_step "$project_dir" "Applying fixes with 'dart fix --apply'" 1 dart fix --apply
80-
echo ""
81-
run_project_step "$project_dir" "Running tests with 'flutter test'" 2 flutter test
82-
echo ""
83-
run_project_step "$project_dir" "Analyzing code with 'flutter analyze'" 3 flutter analyze
84-
85-
echo "Finished processing $project_dir."
86-
echo ""
87-
)
108+
# Collect all project directories first to process them in a stable order.
109+
project_dirs=()
110+
while IFS= read -r -d '' pubspec_path; do
111+
project_dirs+=("$(dirname "$pubspec_path")")
112+
done < <(find . -name "build" -type d -prune -o -name ".dart_tool" -type d -prune -o -path "./melos_tool" -prune -o -path "./packages/spikes" -prune -o -name "pubspec.yaml" -exec grep -q 'sdk: flutter' {} \; -print0)
113+
114+
# Run processing in parallel for each project.
115+
pids=()
116+
log_files=()
117+
for i in "${!project_dirs[@]}"; do
118+
project_dir="${project_dirs[$i]}"
119+
log_file="$LOG_DIR/project_$i.log"
120+
log_files+=("$log_file")
121+
122+
# Run the processing in the background, redirecting output to the log file.
123+
process_project "$project_dir" > "$log_file" 2>&1 &
124+
pids+=($!)
88125
done
89126

127+
# Wait for all background jobs to complete.
128+
echo "## Running tests and analysis for ${#project_dirs[@]} projects in parallel"
129+
for project_dir in "${project_dirs[@]}"; do
130+
echo "- \`$project_dir\`"
131+
done
132+
echo ""
133+
wait "${pids[@]}"
134+
echo "## All projects have been processed."
90135
echo "=================================================="
91-
echo " All projects have been processed."
92-
echo "=================================================="
136+
echo ""
137+
138+
# Print the logs from each project in the original order.
139+
for log_file in "${log_files[@]}"; do
140+
cat "$log_file"
141+
done
93142

94143
if [ -s "$FAILURE_LOG" ]; then
95-
echo "Tooling errors occurred:" >&2
144+
echo ""
145+
echo "## Tooling errors occurred:" >&2
146+
echo '
147+
```' >&2
96148
cat "$FAILURE_LOG" >&2
149+
echo '
150+
```' >&2
97151
exit 1
98-
fi
152+
fi

0 commit comments

Comments
 (0)