bd is a bash script to conveniently jump multiple directory levels up from the current working directory instead of a tedious and possibly inaccurate cd ../../../..Say, for example, that you are in the directory /home/user/project/src/org/main and you want to jump to project. Instead of counting the necessary levels to jump and typing cd ../../.., you can do bd -s pro (where -s is to match the start of the directory name, in this example pro for project).I came across this script in another question, and looking at the github repository I decided to jazz it up a little. But before going ahead and refactoring it, I wanted to make sure I'm not breaking anything, so I needed some sort of unit tests as a sane baseline first.This is the main function in the bd script under testing,and this is not for code review,I'm including it here only for your reference in case it helps in some way:# NOT for code review: the reference implementation under testnewpwd() {  OLDPWD=$1  if [ $2 = -s ]  then    NEWPWD=`echo $OLDPWD | sed 's|\(.*/'$3'[^/]*/\).*|\1|'`    index=`echo $NEWPWD | awk '{ print index($0,/'$3'); }'`  elif [ $2 = -si ]  then    NEWPWD=`echo $OLDPWD | sed 's|\(.*/'$3'[^/]*/\).*|\1|I'`    index=`echo $NEWPWD | awk '{ print index(tolower($0),tolower(/'$3')); }'`  else    NEWPWD=`echo $OLDPWD | sed 's|\(.*/'$2'/\).*|\1|'`    index=`echo $NEWPWD | awk '{ print index($1,/'$2'/); }'`  fi}What I'd like reviewed is my implementation of unit tests to verify the behavior of the newpwd function inside the bd script:#!/bin/bash# loading the newpwd function from the bd script, a necessary ugliness for now. bd nonexistent > /dev/nullsuccess=0failure=0total=0assertEquals() {    ((total++))    expected=$1    actual=$2    if [[ $expected = $actual ]]; then        ((success++))    else        ((failure++))        echo Assertion failed: $expected != $actual >&2        caller 0        echo    fi}sample=/home/user/project/src/org/main/site/utils/file/reader/whatever# test run with no argsnewpwd $sampleassertEquals $sample $NEWPWDassertEquals 0 $index# test run with exact matchnewpwd $sample srcassertEquals /home/user/project/src/ $NEWPWDassertEquals 19 $index# test run with prefix but no -s so not foundnewpwd $sample srassertEquals $sample $NEWPWDassertEquals 0 $index# test run with prefix foundnewpwd $sample -s srassertEquals /home/user/project/src/ $NEWPWDassertEquals 19 $index# test run with prefix not found because case sensitivenewpwd $sample -s SrassertEquals $sample $NEWPWDassertEquals 0 $index# test run with prefix found thanks to -sinewpwd $sample -si SrassertEquals /home/user/project/src/ $NEWPWDassertEquals 19 $indexsample='/home/user/my project/src'# test run with space in dirnamenewpwd $sample -s myassertEquals '/home/user/my project/' $NEWPWDassertEquals 11 $indexred='\e[0;31m'green='\e[0;32m'nocolor='\e[0m'echo[[ $failure = 0 ]] && printf $green || printf $redecho Tests run: $total ($success success, $failure failed)printf $nocolorechoMy questions:Is this easy to read? If not, how to improve it?Is this a good way to test stuff in Bash? Is there a better way? I'd rather not add external dependencies to the project though, I'm looking for something ultra-light, or a techniqueAre there any corner cases I missed?Any other improvement ideas?
Unit testing a bash script manipulating the working directory path
unit testing;bash;file system
Just 2 cents:I'd quote the right hand side of the [[ ... = ... ]] to avoid pattern interpretation. Cf.x=123y=*2*[[ $x =  $y  ]] && echo Equal without quotes[[ $x = $y ]] || echo Not equal with quotesI like naming the tests. I'd probably add a third parameter to assertEquals and include it in the report. It also improves readability. Inspired by Test::More for Perl.