scripts/test.sh
#!/bin/bash
set -euo pipefail

# throw error if more than one argument:
if [ $# -gt 1 ]; then
  echo "too many arguments. expected 0 or 1."
  exit 1
fi

export SKIP_EXPENSIVE_TESTS=1
export TESTING=1

if [ $# -eq 1 ]; then
  dirOverride="$1"
else
  dirOverride=""
  export RUNNING_ALL=1
fi

gitRoot=$(git rev-parse --show-toplevel)
cd "$gitRoot"

if [ -z "$dirOverride" ]; then
  echo "no test dir specified. will run all tests."
  dirs="cli pyhooks server shared ui"
  # cd into git root:
else
  dirs="$dirOverride"
fi

for d in $dirs; do
  # print a couple newlines:
  echo $'\n\n'"RUNNING TESTS IN $d"$'\n'
  cd $gitRoot
  cd $d

  if [ $d = 'ui' ] || [ $d = 'server' ]; then
    if [ ! -n "${SKIP_TS:-}" ]; then
      pnpm exec vitest --watch=false
    fi
  else
    if [ ! -n "${SKIP_TS:-}" ]; then
      # run all typescript tests:
      files=$(find . -name '*.test.ts' -o -name 'test.ts')
      for f in $files; do
        echo "RUNNING $d/$f"
        "$gitRoot/node_modules/.bin/esr" "$f"
      done
    fi

    if [ ! -n "${SKIP_PY:-}" ]; then
      pytest && true
      pytest_exit=$?
      # pytest exits with code 5 if there were no tests found, but not all dirs have python tests
      if [ $pytest_exit -ne 0 ] && [ $pytest_exit -ne 5 ]; then
        exit 1
      fi
    fi
  fi

done
echo "ALL DONE"
