Skip to content

Instantly share code, notes, and snippets.

@donnfelker
Created February 17, 2025 16:25
Show Gist options
  • Save donnfelker/bbb25e1d578201e604494638f93166a2 to your computer and use it in GitHub Desktop.
Save donnfelker/bbb25e1d578201e604494638f93166a2 to your computer and use it in GitHub Desktop.
run_test - a shell script function that allows you to quickly run rails tests.
# Allows you to run a rails test from the CLI with:
#
# run_test "when x happens then abcd should happen"
#
# Which turns it into:
# rails test --name "/^test_when_x_happens_then_abcd_should_happen$/"
#
run_test() {
# Check if a test description was provided
if [ -z "$1" ]; then
echo "Usage: run_test \"test description\""
return 1
fi
# Convert spaces to underscores
local transformed=$(echo "$1" | sed 's/ /_/g')
# Build the regex. Adjust the prefix if necessary.
local regex="^test_${transformed}\$"
# Run the rails test command
rails test --name "/${regex}/"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment