Skip to content

Instantly share code, notes, and snippets.

@adibhanna
Created April 12, 2025 17:56
Show Gist options
  • Save adibhanna/f2704f6b3a34dca7c310c1c5daf04125 to your computer and use it in GitHub Desktop.
Save adibhanna/f2704f6b3a34dca7c310c1c5daf04125 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
test_file=$(find . -type f -name '*_test.go' | fzf --prompt="Select test file > " --preview 'bat --color=always {}')
if [ -z "$test_file" ]; then
echo "No test file selected. Exiting..."
exit 1
fi
tests=$(grep -nE '^func Test[A-Z]' "$test_file")
if [ -z "$tests" ]; then
echo "No test functions found in $test_file"
exit 1
fi
selected_line=$(echo "$tests" | fzf --delimiter ':' \
--nth=2.. --prompt="Select a test > " \
--preview="bat --color=always --highlight-line {1} $test_file")
if [ -z "$selected_line" ]; then
echo "No test selected. Exiting..."
exit 1
fi
line_number=$(echo "$selected_line" | cut -d: -f1)
test_name=$(echo "$selected_line" | sed -E 's/^[0-9]+:func ([^(]+).*$/\1/')
echo "Running test '$test_name' (found at line $line_number) in $test_file..."
cd "$(dirname "$test_file")" || exit 1
go test -v -run="^${test_name}$"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment