run_tests.sh 5.61 KB
Newer Older
1
#!/usr/bin/env bash
2 3 4 5 6

# Mai available environment variables
#   * RSYNC_TARGET: target on which to rsync the xunit output.
#   * XUNIT_OUT: file in which xunit output should be saved.
#   * WEBOOB_BACKENDS: path to the Weboob backends file to use.
7 8
#   * WEBOOB_CI_TARGET: URL of your Weboob-CI instance.
#   * WEBOOB_CI_ORIGIN: origin for the Weboob-CI data.
9

10 11
# stop on failure
set -e
12

13 14 15 16 17
. "$(dirname $0)/common.sh"

if [ -z "${PYTHON}" ]; then
    echo "Python required"
    exit 1
18 19
fi

20 21 22 23 24
if ! $PYTHON -c "import nose" 2>/dev/null; then
    echo "python-nose required"
    exit 1
fi

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
TEST_CORE=1
TEST_MODULES=1

for i in "$@"
do
case $i in
    --no-modules)
        TEST_MODULES=0
        shift
        ;;
    --no-core)
        TEST_CORE=0
        shift
        ;;
    *)
    ;;
esac
done
43

44 45 46
# path to sources
WEBOOB_DIR=$(cd $(dirname $0)/.. && pwd -P)

47
BACKEND="${1}"
48 49 50 51 52 53 54 55
if [ -z "${WEBOOB_WORKDIR}" ]; then
    # use the old workdir by default
    WEBOOB_WORKDIR="${HOME}/.weboob"
    # but if we can find a valid xdg workdir, switch to it
    [ "${XDG_CONFIG_HOME}" != "" ] || XDG_CONFIG_HOME="${HOME}/.config"
    [ -d "${XDG_CONFIG_HOME}/weboob" ] && WEBOOB_WORKDIR="${XDG_CONFIG_HOME}/weboob"
fi
[ -z "${TMPDIR}" ] && TMPDIR="/tmp"
56
WEBOOB_TMPDIR=$(mktemp -d "${TMPDIR}/weboob_test.XXXXXX")
57
[ -z "${WEBOOB_BACKENDS}" ] && WEBOOB_BACKENDS="${WEBOOB_WORKDIR}/backends"
58 59
[ -z "${WEBOOB_MODULES}" ] && WEBOOB_MODULES="${WEBOOB_DIR}/modules"
[ -z "${PYTHONPATH}" ] && PYTHONPATH=""
60

61 62 63 64 65 66
# allow private environment setup
[ -f "${WEBOOB_WORKDIR}/pre-test.sh" ] && source "${WEBOOB_WORKDIR}/pre-test.sh"

# setup xunit reporting (buildbot slaves only)
if [ -n "${RSYNC_TARGET}" ]; then
    # by default, builder name is containing directory name
67
    [ -z "${BUILDER_NAME}" ] && BUILDER_NAME=$(basename $(readlink -e $(dirname $0)/../..))
68
    XUNIT_OUT="${WEBOOB_TMPDIR}/xunit.xml"
69 70 71 72
else
    RSYNC_TARGET=""
fi

73
# Avoid undefined variables
74 75 76 77
if [ ! -n "${XUNIT_OUT}" ]; then
    XUNIT_OUT=""
fi

78 79 80 81 82 83 84 85 86 87 88
# Handle Weboob-CI variables
if [ -n "${WEBOOB_CI_TARGET}" ]; then
    if [ ! -n "${WEBOOB_CI_ORIGIN}" ]; then
        WEBOOB_CI_ORIGIN="Weboob unittests run"
    fi
    # Set up xunit reporting
    XUNIT_OUT="${WEBOOB_TMPDIR}/xunit.xml"
else
    WEBOOB_CI_TARGET=""
fi

89 90
# do not allow undefined variables anymore
set -u
91 92 93 94 95 96
if [ -f "${WEBOOB_BACKENDS}" ]; then
    cp "${WEBOOB_BACKENDS}" "${WEBOOB_TMPDIR}/backends"
else
    touch "${WEBOOB_TMPDIR}/backends"
    chmod go-r "${WEBOOB_TMPDIR}/backends"
fi
97

98
# xunit nose setup
99
if [ -n "${XUNIT_OUT}" ]; then
100
    XUNIT_ARGS="--with-xunit --xunit-file=${XUNIT_OUT}"
101 102 103 104
else
    XUNIT_ARGS=""
fi

105
${PYTHON} "$(dirname $0)/stale_pyc.py"
106 107

echo "file://${WEBOOB_MODULES}" > "${WEBOOB_TMPDIR}/sources.list"
108

109
export WEBOOB_WORKDIR="${WEBOOB_TMPDIR}"
110
export WEBOOB_DATADIR="${WEBOOB_TMPDIR}"
111
export PYTHONPATH="${WEBOOB_DIR}:${PYTHONPATH}"
112
export NOSE_NOPATH="1"
113 114 115 116

if [[ ($TEST_MODULES = 1) || (-n "${BACKEND}") ]]; then
    ${PYTHON} "${WEBOOB_DIR}/scripts/weboob-config" update
fi
117 118 119

# allow failing commands past this point
set +e
120
set -o pipefail
121 122
STATUS_CORE=0
STATUS=0
123
if [ -n "${BACKEND}" ]; then
124
    ${PYTHON} -m nose -c /dev/null --logging-level=DEBUG -sv "${WEBOOB_MODULES}/${BACKEND}/test.py" ${XUNIT_ARGS}
125
    STATUS=$?
126
else
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
    if [ $TEST_CORE = 1 ]; then
        echo "=== Weboob ==="
        CORE_TESTS=$(mktemp)
        ${PYTHON} -m nose --cover-package weboob -c ${WEBOOB_DIR}/setup.cfg --logging-level=DEBUG -sv 2>&1 | tee "${CORE_TESTS}"
        STATUS_CORE=$?
        CORE_STMTS=$(grep "TOTAL" ${CORE_TESTS} | awk '{ print $2; }')
        CORE_MISS=$(grep "TOTAL" ${CORE_TESTS} | awk '{ print $3; }')
        CORE_COVERAGE=$(grep "TOTAL" ${CORE_TESTS} | awk '{ print $4; }')
        rm ${CORE_TESTS}
    fi

    if [ $TEST_MODULES = 1 ]; then
        echo "=== Modules ==="
        MODULES_TESTS=$(mktemp)
        MODULES_TO_TEST=$(find "${WEBOOB_MODULES}" -name "test.py" | sort | xargs echo)
        ${PYTHON} -m nose --with-coverage --cover-package modules -c /dev/null --logging-level=DEBUG -sv ${XUNIT_ARGS} ${MODULES_TO_TEST} 2>&1 | tee ${MODULES_TESTS}
        STATUS=$?
        MODULES_STMTS=$(grep "TOTAL" ${MODULES_TESTS} | awk '{ print $2; }')
        MODULES_MISS=$(grep "TOTAL" ${MODULES_TESTS} | awk '{ print $3; }')
        MODULES_COVERAGE=$(grep "TOTAL" ${MODULES_TESTS} | awk '{ print $4; }')
        rm ${MODULES_TESTS}
    fi
149 150 151

    # Compute total coverage
    echo "=== Total coverage ==="
152 153 154 155 156 157 158 159 160 161 162 163 164
    if [ $TEST_CORE = 1 ]; then
        echo "CORE COVERAGE: ${CORE_COVERAGE}"
    fi
    if [ $TEST_MODULES = 1 ]; then
        echo "MODULES COVERAGE: ${MODULES_COVERAGE}"
    fi

    if [[ ($TEST_CORE = 1) && ($TEST_MODULES = 1) ]]; then
        TOTAL_STMTS=$((${CORE_STMTS} + ${MODULES_STMTS}))
        TOTAL_MISS=$((${CORE_MISS} + ${MODULES_MISS}))
        TOTAL_COVERAGE=$((100 * (${TOTAL_STMTS} - ${TOTAL_MISS}) / ${TOTAL_STMTS}))
        echo "TOTAL: ${TOTAL_COVERAGE}%"
    fi
165
fi
166

167
# Rsync xunit transfer
168
if [ -n "${RSYNC_TARGET}" ]; then
169 170 171 172 173 174 175 176 177
    rsync -iz "${XUNIT_OUT}" "${RSYNC_TARGET}/${BUILDER_NAME}-$(date +%s).xml"
    rm "${XUNIT_OUT}"
fi

# Weboob-CI upload
if [ -n "${WEBOOB_CI_TARGET}" ]; then
    JSON_MODULE_MATRIX=$(${PYTHON} "${WEBOOB_DIR}/tools/modules_testing_grid.py" "${XUNIT_OUT}" "${WEBOOB_CI_ORIGIN}")
    curl -H "Content-Type: application/json" --data "${JSON_MODULE_MATRIX}" "${WEBOOB_CI_TARGET}/api/v1/modules"
    rm "${XUNIT_OUT}"
178 179
fi

180
# safe removal
181 182 183
if [[ ($TEST_MODULES = 1) || (-n "${BACKEND}") ]]; then
    rm -r "${WEBOOB_TMPDIR}/icons" "${WEBOOB_TMPDIR}/repositories" "${WEBOOB_TMPDIR}/modules" "${WEBOOB_TMPDIR}/keyrings"
fi
184
rm "${WEBOOB_TMPDIR}/backends" "${WEBOOB_TMPDIR}/sources.list"
185
rmdir "${WEBOOB_TMPDIR}"
186

187
[ $STATUS_CORE -gt 0 ] && exit $STATUS_CORE
188
exit $STATUS