#!/bin/sh

set -exu

export ROS_PYTHON_VERSION=3

SOURCEDIR=$(pwd)

cd "$ADTTMP"

cmake "$SOURCEDIR" -DCATKIN_ENABLE_TESTING=ON

make

# this command does not return a non-zero exit status upon failure, so we have
# to manually check for failure later
#
# we have to set ROS_HOME because otherwise it will try using ~/.ros but that
# the user running autopkgtest might not even have a home directory
ROS_HOME="$ADTTMP" make -j1 run_tests

# checking whether the tests succeeded or fails involves checking several files
# for their existence or we print the whole directory content to have it in the
# autopkgtest log file for debugging purposes
find

for t in two_links_moving_joint subclass joint_states_bag two_links_fixed_joint one_link; do
	# make sure that the files indicating failure are *not* present
	[ ! -e "./test_results/robot_state_publisher/MISSING-rostest-test_test_$t.xml" ]
	# make sure that the files indicating success *are* present and not empty
	[ -s "./test_results/robot_state_publisher/rostest-test_test_$t.xml" ]
	# check the XML for any errors reported
	xml2 < "./test_results/robot_state_publisher/rostest-test_test_$t.xml" | grep "/testsuite/@errors=0"
	xml2 < "./test_results/robot_state_publisher/rostest-test_test_$t.xml" | grep "/testsuite/@failures=0"
done
