Fork me on GitHub
  ▖     
▃▀▄▄▖▄▄▖
 ▀▖     
        


#!/bin/bash
# usage: plash test [TEST-PREFIX]
# Run unit tests.
# You can filter tests to run by a prefix passed as first argument

set -eu

test " ${1:-}" = ' --help' && {
  echo 'usage: plash test [TEST-PREFIX]'
  exit 2
}

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

PS4='+ ${BASH_SOURCE}:${LINENO} '

#mkdir -p /var/lib/plash/tmp
export PATH=$DIR/libexec:$PATH
export PYTHONPATH=$DIR/..:${PYTHONPATH:-}

log=$(mktemp)
program_exit=0
cd $DIR/../tests
testfiles=$(find . -type f ! -iname "*.sw*" -ipath "./${1:-}*" | sort)
for script in $testfiles ; do

  # setup test env
  tmp=$(mktemp -d /tmp/plashtest-XXXXXXXX) # if we want to use it inside plash we cant user an overlay mounted fs
  export PLASH_DATA=$tmp
  plash init
  plash data touch config/testmode
  plash import-tar $DIR/../fixtures/busybox.tar > /dev/null

  printf "% -20s" ${script:2} # magic 2 is the length of "./"
  env "$script" > $log 2>&1 &
  if wait $!; then
    echo ' PASS'
  else
    skip_marker=$(tail -n2 $log | head -n1)
    if test "$skip_marker" = 'SKIP MARKER'; then
      echo ' SKIP'
      continue
    fi
    echo ' FAIL'
    program_exit=1
    test_mark_line=$(grep -n '^+ : ' $log | tail -n1 | cut -d: -f1)
    if [[ $test_mark_line =~ ^-?[0-9]+$ ]]; then
        tail -n +$test_mark_line $log | sed 's/^/  | /'  >&2 
    else
        cat $log | sed 's/^/  | /'  >&2 
    fi
    echo
  fi
done

exit $program_exit