#!/bin/sh

# testscript to run liblog4cxx unit tests.
# Part of the Debian package for liblog4cxx

# unstable tests, known to fail once in a while.
# upstream is working on those: LOGCXX-527, LOGCXX-532
FLAKY_TESTS='(timebasedrollingtest|multithreadtest)'

set -e

# We need to patch the sources so that the test suite runs against the
# packaged version of the library. So we need a copy...

WORKDIR=$(mktemp -d)
echo "this is our $WORKDIR"

trap "rm -rf $WORKDIR" 0 INT QUIT ABRT PIPE TERM

# Copy the source tree to $AUTOPKGTEST_TMP before perparing it for the tests
cp -r . "$WORKDIR" ; cd "$WORKDIR"

# Patching the testsuite's CMakeList.txt to build the tests standalone.
patch -p1 <debian/tests/standalone-tests.patch

# Build it and run tests.
mkdir build ; cd build
cmake ../src/test/
cmake --build . -j $(nproc)

# Run non-flaky tests.
ctest --output-on-failure -E $FLAKY_TESTS -j $(nproc)

# Run remaining (flaky) tests
# They are known to fail randomly, so they are only considered as failed
# if they fail 3 times in a row.
ctest --output-on-failure -R $FLAKY_TESTS  ||
  ctest --rerun-failed --output-on-failure ||
  ctest --rerun-failed --output-on-failure
