The new woob repository is here: https://gitlab.com/woob/woob. This gitlab will be removed soon.

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

# Mai available environment variables
#   * RSYNC_TARGET: target on which to rsync the xunit output.
#   * XUNIT_OUT: file in which xunit output should be saved.
6 7 8
#   * WOOB_BACKENDS: path to the Woob backends file to use.
#   * WOOB_CI_TARGET: URL of your Woob-CI instance.
#   * WOOB_CI_ORIGIN: origin for the Woob-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
# path to sources
45
WOOB_DIR=$(cd $(dirname $0)/.. && pwd -P)
46

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

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

# 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="${WOOB_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

Roger Philibert's avatar
Roger Philibert committed
78
# Handle Woob-CI variables
79 80
if [ -n "${WOOB_CI_TARGET}" ]; then
    if [ ! -n "${WOOB_CI_ORIGIN}" ]; then
Roger Philibert's avatar
Roger Philibert committed
81
        WOOB_CI_ORIGIN="Woob unittests run"
82 83
    fi
    # Set up xunit reporting
84
    XUNIT_OUT="${WOOB_TMPDIR}/xunit.xml"
85
else
86
    WOOB_CI_TARGET=""
87 88
fi

89 90
# do not allow undefined variables anymore
set -u
91 92
if [ -f "${WOOB_BACKENDS}" ]; then
    cp "${WOOB_BACKENDS}" "${WOOB_TMPDIR}/backends"
93
else
94 95
    touch "${WOOB_TMPDIR}/backends"
    chmod go-r "${WOOB_TMPDIR}/backends"
96
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
[ $VER -eq 2 ] && $PYTHON "$(dirname $0)/stale_pyc.py"
106

107
echo "file://${WOOB_MODULES}" > "${WOOB_TMPDIR}/sources.list"
108

109 110 111
export WOOB_WORKDIR="${WOOB_TMPDIR}"
export WOOB_DATADIR="${WOOB_TMPDIR}"
export PYTHONPATH="${WOOB_DIR}:${PYTHONPATH}"
112
export NOSE_NOPATH="1"
113 114

if [[ ($TEST_MODULES = 1) || (-n "${BACKEND}") ]]; then
115
    # TODO can we require woob to be installed before being able to run run_tests.sh?
116
    # if we can, then woob config is present in PATH (virtualenv or whatever)
117
    ${PYTHON} -c "import sys; sys.argv='woob-config update'.split(); from woob.applications.config import AppConfig; AppConfig.run()"
118
fi
119 120 121

# allow failing commands past this point
set +e
122
set -o pipefail
123 124
STATUS_CORE=0
STATUS=0
125
if [ -n "${BACKEND}" ]; then
126
    ${PYTHON} -m nose -c /dev/null --logging-level=DEBUG -sv "${WOOB_MODULES}/${BACKEND}/test.py" ${XUNIT_ARGS}
127
    STATUS=$?
128
else
129
    if [ $TEST_CORE = 1 ]; then
Roger Philibert's avatar
Roger Philibert committed
130
        echo "=== Woob ==="
131
        CORE_TESTS=$(mktemp)
132
        ${PYTHON} -m nose --cover-package woob -c ${WOOB_DIR}/setup.cfg --logging-level=DEBUG -sv 2>&1 | tee "${CORE_TESTS}"
133 134 135 136 137 138 139 140 141 142
        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)
143
        MODULES_TO_TEST=$(find "${WOOB_MODULES}" -name "test.py" | sort | xargs echo)
144 145 146 147 148 149 150
        ${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
151 152 153

    # Compute total coverage
    echo "=== Total coverage ==="
154 155 156 157 158 159 160 161 162 163 164 165 166
    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
167
fi
168

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

175 176 177 178
# Woob-CI upload
if [ -n "${WOOB_CI_TARGET}" ]; then
    JSON_MODULE_MATRIX=$(${PYTHON} "${WOOB_DIR}/tools/modules_testing_grid.py" "${XUNIT_OUT}" "${WOOB_CI_ORIGIN}")
    curl -H "Content-Type: application/json" --data "${JSON_MODULE_MATRIX}" "${WOOB_CI_TARGET}/api/v1/modules"
179
    rm "${XUNIT_OUT}"
180 181
fi

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

189
[ $STATUS_CORE -gt 0 ] && exit $STATUS_CORE
190
exit $STATUS