#!/bin/sh
# Run Apport self tests.
#
# Test against the source tree when run in the source tree root. Test against
# the system libraries/programs when run from anywhere else.
# You can specify test names as arguments to only run a subset of tests.
#
# Run all tests: test/run
# Run tests of one module: test/run crashdb
# Run one test in one module:  test/run report.test_add_os_info

# Copyright (C) 2007 - 2012 Canonical Ltd.
# Author: Martin Pitt <martin.pitt@ubuntu.com>
# 
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.  See http://www.gnu.org/copyleft/gpl.html for
# the full text of the license.

set -e

mydir=`dirname "$0"`

export LC_MESSAGES=C

# Run against source tree when we are in the source directory
if [ -d test -a -e setup.py ]; then
    echo "Testing local source tree."
    export PATH=`pwd`/bin:$PATH
    export PYTHONPATH=`pwd`
    export APPORT_CRASHDB_CONF=./etc/apport/crashdb.conf
    export APPORT_DATA_DIR=./data
    export APPORT_TEST_LOCAL=1

    # PEP8 tests, if pep8 is available
    if type pep8 >/dev/null 2>&1; then
        echo "Running pep8..."
        # . catches all *.py modules; we explicitly need to specify the programs
        pep8 -r --ignore=E401,E501,E124 --exclude=test_problem_report.py,report.py,is-enabled,apport-bug,apport-collect .  `find bin data gtk kde -type f -executable`
        # those tests deliberately have trailing whitespace in test files, ignore
        pep8 -r --ignore=E401,E501,W291,W293 test/test_problem_report.py
        # has_key is minidom API, not the dict operator here
        pep8 -r --ignore=E401,E501,W601 apport/report.py
    else
        echo "Skipping PEP 8 tests, pep8 is not installed"
    fi

    # pyflakes, if available
    if type pyflakes >/dev/null 2>&1; then
        echo "Running pyflakes..."
        pyflakes `find -name '*.py'` `find bin data gtk -type f -executable ! -name is-enabled ! -name apport-bug ! -name root_info_wrapper ! -name apport-collect`
    else
        echo "Skipping pyflakes tests, pyflakes is not installed"
    fi

    # assert that there are no hardcoded "Ubuntu" names
    out=$(grep -rw Ubuntu test/*.py apport/*.py gtk/apport-gtk* kde/* bin/* | grep -v Debian | grep -v X-Ubuntu-Gettext-Domain | grep -v '#.*Ubuntu') || :
    if [ -n "$out" ]; then
        echo "Found hardcoded 'Ubuntu' names, use DistroRelease: field or lsb_release instead:\n\n$out" >&2
        exit 1
    fi

    if [ ! -e apport/packaging_impl.py ]; then
	echo "You need to copy an appropriate packaging implementation from backends/ to apport/packaging_impl.py; run './setup.py build' for auto-detection." >&2
	exit 1
    fi
else
    echo "Testing installed libraries/program."
fi

# avoid breaking the UI tests due to obsolete packages
export APPORT_IGNORE_OBSOLETE_PACKAGES=1

# avoid breaking tests due to translated strings or assuming a particular
# locale
export LC_MESSAGES=C
export LC_CTYPE=C
unset LANGUAGE LANG

# do not assume/disturb session bus
unset DBUS_SESSION_BUS_ADDRESS

xvfb=
if type xvfb-run >/dev/null && [ -z "$APPORT_TEST_NOXVFB" ]; then
    xvfb=xvfb-run
fi

failed=0

# check command line whether to only run a subset of tests
if [ -z "$1" ]; then
    TESTS=$(ls $mydir/*.py | cut -f2- -d_ | cut -f1 -d.)
else
    while [ -n "$1" ]; do
        if [ -e "$mydir/test_${1%%.*}.py" ]; then
            TESTS="$TESTS $1"
        else
            echo "Test $1 does not exist" >&2
            exit 1
        fi
        shift
    done 
fi

for t in $TESTS; do
    echo "--- Testing $t ---"
    if [ "${t#ui_}" != $t ]; then
        while [ -e /tmp/.X99-lock ]; do
           echo "Waiting for previous xvfb to finish..."
           sleep 0.5
        done
        prefix=$xvfb
    else
        # ensure that non-UI modules do not require X
        prefix="env -u DISPLAY"
    fi
    if [ "$t" = "python_crashes" ]; then
        prefix="dbus-launch $prefix"
    fi
    test_file="$mydir/test_${t%%.*}.py"
    if [ "$t" = "${t#*.}" ]; then
        test_name=""
    else
        test_name="T.${t#*.}"
    fi
    $prefix ${PYTHON:=python3} -tt $test_file -v $test_name || failed=$((failed+1))
done

exit $failed
