#!/usr/bin/env bash
# Usage: script/test
set -e

ROOTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd )"
TMPDIR="$ROOTDIR/test/tmp"

# Remove possible remnants of previous test runs
rm -rf "${TMPDIR:?}/*"

print_test_results() {
  if [ -n "$GITHUB_STEP_SUMMARY" ]; then
    echo -e "### Test results\n" >> "$GITHUB_STEP_SUMMARY"
    echo "| Test suite | Result | Successful | Failed | Skipped | Duration |" >> "$GITHUB_STEP_SUMMARY"
    echo "|---|---|--:|--:|--:|--:|" >> "$GITHUB_STEP_SUMMARY"
    sort -V "$TMPDIR/results" >> "$GITHUB_STEP_SUMMARY"
  fi
}

# Enable verbose logging of ssh commands
export GHE_VERBOSE_SSH=true

if ! find test -name "test-*.sh" -print0 | sort -z |xargs -0 -n 1 /bin/bash; then
  print_test_results
  exit 1
fi

print_test_results
