gitnu 0.3.0

gitnu indexes your git status so you can use numbers instead of filenames.
Documentation
#!/usr/bin/env bash

# navigate to where this script is located
pushd $(dirname "$(readlink -f "$0")") >/dev/null
source ./utils
setup # sets the current directory to this script's location
trap cleanup EXIT
HERE=$PWD

# parse arguments
for i in "$@"; do
  [[ "$i" =~ [0-9]+ ]] && [ -z $ONE_TEST ] && printf -v ONE_TEST "%02d" $i
done

N=0 # current id
let PASSES=0
let TOTAL=0

# $1 : aboslute path to ./cases/*.sh
test() {
  [ ! -f $1 ] && echo "No such test file" && return
  local recfile=$HERE/tmp/$N.rec
  local expfile=$HERE/tmp/$N.exp
  local difffile=$HERE/failed/$N.diff
  save() {
    eval "$@" >$recfile
  }
  source $1 &>/dev/null
  local EX_START=0
  # generate expected file from comments in test file
  while read -r line; do
    [ $EX_START = 1 ] && echo "${line:2}" >>$expfile
    [[ $line = *"------------------------------"* ]] && EX_START=1
  done <$1
  local title=$(get_test_title $1) && local testfile=${1##$HERE/}
  local rc=0 # return code
  [ ! -f $recfile ] && not_found $testfile $N.rec && rc=1
  [ ! -f $expfile ] && not_found $testfile $N.exp && rc=1

  # either received/expected not found
  [ $rc = 1 ] && return 1

  # gather stats and diff
  diff -y -W 70 $recfile $expfile >$difffile
  rc=$?
  if [[ $rc == 0 ]]; then
    pass $testfile "$title"
    rm -f $difffile
  else
    fail $testfile "$title"
  fi
  return $rc
}

# $1 : absolute path to test file
wrap_test() {
  let TOTAL++
  assert $PWD $HERE && N=$(get_test_id $1)
  tmp_dir=$HERE/tmp/$N && mkdir -p $tmp_dir
  cd $tmp_dir && test "$1" &
  pids+=("$!")
}

main() {
  # build
  cargo build --quiet || exit 1

  # ensure correct binary is used
  if ! command -v gitnu >/dev/null; then
    echo 'gitnu not found. Aborting.' && exit 1
  fi

  pids=()

  # run test cases
  if [ $ONE_TEST ]; then
    wrap_test $HERE/cases/$ONE_TEST.sh # run one test
    wait $pid
    echo '[ final output ] ───────────────'
    cat $HERE/tmp/$ONE_TEST.rec
    echo '────────────────────────────────'
  else
    for t in $HERE/cases/*; do # run all tests
      wrap_test $t
    done
  fi

  # wait for all tests to finish
  [ -z $ONE_TEST ] && for pid in ${pids[@]}; do
    wait $pid
    [[ $? == 0 ]] && let PASSES++
  done

  # summarize
  printf "\n $PASSES/$TOTAL tests passed\n"
  [ $PASSES = $TOTAL ] && [ $TOTAL -gt 0 ]
}

log_results() {
  assert $PWD $HERE
  local file=$HERE/log/$(date +'%Y-%m-%d-%H:%M:%S').txt
  local ENR="echo '% < received | expected >'"
  rm -f $file
  ls failed | xargs -I % sh -c \
    "$ENR && cat failed/% && printf '\n-----\n\n'" \
    >$file >latest.log
  printf "\n ${HERE/$HOME/~}/latest.log\n\n"
}

main
ec=$?
[[ $ec = 1 ]] && log_results
exit $ec