#!/bin/bash
set -ex
target="${1:?"Specify a makefile target"}"
# Set to 1 to temporarily ignore warnings
export PERL_TEST_WARNINGS_ONLY_REPORT_WARNINGS=0
export COVERAGE=1
export COVERDB_SUFFIX="_${target}"
# Enable JUnit-compatible test results
export JUNIT_PACKAGE=openQA
export JUNIT_NAME_MANGLE=perl
export JUNIT_OUTPUT_FILE=test-results/junit/$target.xml
# Be aware that this option saves all verbose output caught by "prove"
# into test artifact files whereas the error output is still shows up
# where prove is executed to show initial context in case of failed
# tests or other problems. The "--merge" option of prove would loose
# that context in the prove calling context.
export PERL_TEST_HARNESS_DUMP_TAP=test-results
export HARNESS='--harness TAP::Harness::JUnit --timer'
# Activate Test::CheckGitStatus
export CHECK_GIT_STATUS=1
mkdir -p test-results/junit
# CircleCI can be particularly slow sometimes since some time around 2021-06
export OPENQA_TEST_TIMEOUT_SCALE_CI=3
export EXTRA_PROVE_ARGS="-v"
export OPENQA_FULLSTACK_TEMP_DIR=$PWD/test-results/fullstack
# Set consistent language
export LANGUAGE= LANG=C.utf8

# run make as different user to avoid "initdb: error: cannot be run as root"
chmod 777 "$PWD/.." "$PWD"
user=testuser
id -u "$user" &>/dev/null || useradd "$user"
chown -R "$user:$user" "$PWD"

# avoid running into "The authenticity of host 'github.com (…)' can't be established." in `test-gitlint` target
cp -vr "$HOME/.ssh" "/home/$user/.ssh"
chown -R "$user:$user" "/home/$user/.ssh"

sudo --preserve-env -u "$user" make test-"$target"
