Last active
May 14, 2020 19:13
-
-
Save roger-mo-gusto/08ac878641c8d1b14b6ea67946cd3ed6 to your computer and use it in GitHub Desktop.
Copies spec file prepended with yarn test or bundle exec rspec
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
RED='\e[31m' | |
GREEN='\e[32m' | |
CYAN='\e[36m' | |
BLUE='\e[34m' | |
LIGHTBLUE="\e[94m" | |
PURPLE='\e[1;35m' | |
NC='\e[0m' | |
runtest() { | |
# git status --porcelain | sed s/^...// # chops first couple characters off | |
local modified_files="$(git status -s -u)" | |
# modified_files="$(git status --porcelain)" | |
local regex="[M|\?\?|A|AM|MM|UU] (.*_spec\..*)" | |
if [[ "$modified_files" =~ $regex ]] | |
then | |
# echo "${BASH_REMATCH[1]}" | |
# matched="$(echo "${BASH_REMATCH[1]}" | grep --line-buffered -E "[M|\?\?|A|AM|MM] (.*_spec\..*)" | cut -c 4-)" | |
# matched="$(echo "${BASH_REMATCH[1]}" | grep --line-buffered -E "(.*_spec\..*)" | cut -c 4-)" | |
# echo "${matched}" | |
local spec_line_number="" | |
local spec_to_run="" | |
local spec_file_count=0 | |
local spec_files=() | |
for MATCHED in $BASH_REMATCH | |
do | |
if [[ "$MATCHED" =~ (.*_spec\..*) ]] | |
then | |
spec_files+=("${MATCHED}") | |
spec_file_count=$((spec_file_count+1)) | |
fi | |
done | |
if [[ "$spec_file_count" -gt 1 ]] | |
then | |
echo "Which spec file would you like to run?" | |
print_spec_files | |
echo "" | |
while true; do | |
read -p ">> " file_index spec_line_number | |
if [ "$file_index" = "q" ] | |
then | |
exit 0 | |
elif [ $file_index -eq $file_index 2>/dev/null -o $file_index -eq 0 2>/dev/null ] | |
then | |
if [[ "$file_index" -le "$spec_file_count" ]] | |
then | |
spec_to_run="${spec_files[$file_index]}" | |
break | |
fi | |
fi | |
spec_line_number="" | |
done | |
else | |
spec_to_run="$spec_files" | |
fi | |
if [[ $# -eq 0 ]] | |
then | |
unset spec_line_number | |
else | |
spec_line_number=":$1" | |
fi | |
run_spec_by_filename | |
else | |
printf "${RED}No rb, js, jsx, or tsx spec files found${NC}\n" | |
fi | |
# unset modified_files | |
# unset regex | |
# unset spec_line_number | |
# unset spec_to_run | |
# unset spec_file_count | |
# unset spec_files | |
} | |
print_spec_files() { | |
local spec_file_count=0 | |
local filename_regex="[^/]+$" | |
local filepath_regex="(.*)\/" | |
for FILE_WITH_PATH in "${spec_files[@]}" | |
do | |
local filename=$(echo "${FILE_WITH_PATH}" | grep -E -o "${filename_regex}") | |
local filepath=$(echo "${FILE_WITH_PATH}" | grep -E -o "${filepath_regex}") | |
printf "[${CYAN}${spec_file_count}${NC}]: ${BLUE}${filename}${NC} - ${filepath}\n" | |
spec_file_count=$((spec_file_count+1)) | |
done | |
unset FILE_WITH_PATH | |
} | |
run_spec_by_filename() { | |
echo "" | |
# RUN_TEST_COMMAND="" | |
if [[ "${spec_to_run}" =~ .*.(js|jsx|tsx).* ]] | |
then | |
if ! [[ -z ${spec_line_number+x} ]] | |
then | |
printf "${RED}A line number given for a front-end spec file, but that only works for ruby specs${NC}\n" | |
printf "${RED}To run a specific front-end spec, specify it in the test with 'fdescribe(...' or 'fit(...'${NC}\n" | |
echo "" | |
fi | |
printf "${BLUE}Running spec ${spec_to_run}${NC}\n" | |
echo "" | |
# print -s "yarn test ${spec_to_run}" | |
# history -s "yarn test ${spec_to_run}" # no longer works | |
# RUN_TEST_COMMAND="yarn test ${spec_to_run}" | |
eval "yarn test ${spec_to_run}" | |
elif [[ "${spec_to_run}" =~ .*.rb.* ]] | |
then | |
# printf "${PURPLE}Running spec ${spec_to_run}${spec_line_number} ${NC}\n" | |
# print -s "bundle exec rspec ${spec_to_run}${spec_line_number}" | |
# RUN_TEST_COMMAND="bundle exec rspec ${spec_to_run}${spec_line_number}" | |
# history -s "bundle exec rspec ${spec_to_run}${spec_line_number}" #no longer works | |
# matches ../{engine_name}/../*_spec.rb and engines/{engine_name}/../*_spec.rb | |
local engine_regex="(engines\/.*\/|\.\.\/banking_bridge\/|\.\.\/consumer_banking\/)(spec.*)" | |
if [[ $spec_to_run =~ $engine_regex ]] | |
then | |
printf "${PURPLE}Navigating to engine dir: cd ${BASH_REMATCH[1]}${NC}\n\n" | |
eval "cd ${BASH_REMATCH[1]}" | |
printf "${PURPLE}Running spec ${BASH_REMATCH[2]}${spec_line_number} ${NC}\n" | |
eval "bundle exec rspec ${BASH_REMATCH[2]}${spec_line_number}" | |
else | |
printf "${PURPLE}Running spec ${spec_to_run}${spec_line_number} ${NC}\n" | |
eval "bundle exec rspec ${spec_to_run}${spec_line_number}" | |
fi | |
fi | |
} | |
runtest "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment