~ubuntu-branches/ubuntu/trusty/apport/trusty

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/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=./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 --exclude=test_problem_report.py,report.py,is-enabled,apport-bug .  `find bin data -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 -type f -executable ! -name is-enabled ! -name apport-bug ! -name root_info_wrapper`
    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
export LC_MESSAGES=C
unset LANGUAGE

# 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