Skip to content

Instantly share code, notes, and snippets.

@jagoncalves14
Last active March 23, 2022 17:05
Show Gist options
  • Save jagoncalves14/849e38b98d8e3e80a59d43a1504da58e to your computer and use it in GitHub Desktop.
Save jagoncalves14/849e38b98d8e3e80a59d43a1504da58e to your computer and use it in GitHub Desktop.
ZSH: Function to run unit tests on different files between two branches

Quickly add testbranch to your .zshrc file

Copy the script below for adding the testbranch function to your .zshrc file.

# With curl:
bash <(curl -sL https://gist.githubusercontent.com/jagoncalves14/849e38b98d8e3e80a59d43a1504da58e/raw/5c75984310095d5b8c31c883d6909dc53a17891d/add-testbranch-to-zsh.sh)

# With wget:
bash <(wget -nv -O - https://gist.githubusercontent.com/jagoncalves14/849e38b98d8e3e80a59d43a1504da58e/raw/5c75984310095d5b8c31c883d6909dc53a17891d/add-testbranch-to-zsh.sh)
#!/usr/bin/env bash
# Color variables
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
# Clear the color
clear='\033[0m'
if [ "$1" != "" ]
then
ZSHRC_PATH=$1
else
ZSHRC_PATH=~/.zshrc
fi
echo -e "${yellow}[FETCHING] Fetching function from gist${clear}"
TESTBRANCH_FUNCTION=$(curl https://gist.githubusercontent.com/jagoncalves14/849e38b98d8e3e80a59d43a1504da58e/raw/e47b5a4bee9b6c364e9b7abd8084e01739bafddf/testbranch.zsh)
echo -e "${green}[SUCCESS] Succesfully fetched\n${clear}"
ZSHRC_PATH_CONTENT=$(<$ZSHRC_PATH)
FUNCTION_NAME="function testbranch("
echo $TESTBRANCH_FUNCTION
echo -e "${yellow}[ADDING] Adding function 'testbranch' to '$ZSHRC_PATH'${clear}"
if [[ "$ZSHRC_PATH_CONTENT" =~ .*"$FUNCTION_NAME".* ]]; then
echo -e "${red}[FAILED] Function with name 'testbranch' already exists.${clear}"
else
echo "${green}$TESTBRANCH_FUNCTION//\033[0;32m/" >> "$ZSHRC_PATH"
echo -e "${green}[SUCCESS] Succesfully added function 'testbranch' to '$ZSHRC_PATH'${clear}"
fi
function testbranch() {
if [ "$1" != "" ]
then
BRANCH_TO_COMPARE=$1
else
BRANCH_TO_COMPARE=development
fi
###
# GET THE LIST OF FILES
#
CHANGED_FILES=$(git diff --name-status $BRANCH_TO_COMPARE | cut -f2)
##
# ADJUST FILES AS NEEDED
# Removing .vue or .ts extensions from each file name
#
TRIMMED_VUE=${CHANGED_FILES//.vue/}
TRIMMED_TS=${TRIMMED_VUE//.ts/}
TRIMMED_FILES=${TRIMMED_TS}
##
# LIST FILES
# Turning the list into array and then echoing as string.
# This is required so that each file name in the list is wrapped around quotation marks
#
CHANGED_FILES_LIST=(`echo ${TRIMMED_FILES}`)
##
# RUN UNIT TESTS
#
pnpm run unit ${CHANGED_FILES_LIST}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment