Newer
Older
# vim: set shiftwidth=4 softtabstop=4:

Vincent Hatakeyama
committed
#
# This script is used to run tests on a database with demo data installed.
#
# Either provide the name of the database (first argument) or it will default to <project>_test
# The DB user can be overridden by setting ODOO_SCRIPTS_DB_USER.

Vincent Hatakeyama
committed
# The environment variable “odoo_scripts_run_tests_logfile” can be set to be used to include the output of the tests
#
# COVERAGE_SOURCE can be set to indicate modules or path to check coverage of.
# See https://coverage.readthedocs.io/en/7.6.4/source.html
# If set to empty, no source is given to coverage. (changed in 21.16.1, previous versions considered empty as unset)
# If not set, compute the value from the tested modules
#

Vincent Hatakeyama
committed
here=$(dirname $0)
tested_modules=${ODOO_SCRIPTS_MODULE_LIST_TESTS-$(read_odoo_scripts_expandable_configuration_key module_list_tests)}
if [[ "$tested_modules" != "" ]];
then
if [[ -v COVERAGE_SOURCE ]];

Vincent Hatakeyama
committed
then
# Source already set
else

Vincent Hatakeyama
committed
if type python3 > /dev/null;
then

Vincent Hatakeyama
committed
# With odoo17 or odoo18, using coverage and --source with a package name results in error in psycopg2
COVERAGE_SOURCE="$(python3 -B -c "import os;print(','.join(os.path.join('$ODOO_BASE_PATH', 'addons', name) if os.path.exists(os.path.join('$ODOO_BASE_PATH', 'addons', name)) else f'odoo.addons.{name}' for name in '$tested_modules'.split(',')))")"

Vincent Hatakeyama
committed
else
COVERAGE_SOURCE="$(python2 -B -c "print(','.join('odoo.addons.{}'.format(name) for name in '$tested_modules'.split(',')))")"
fi
fi

Vincent Hatakeyama
committed
dbname=$(basename $(readlink -f $project_home))_test

Vincent Hatakeyama
committed
shift
echo "$INFO Using database $dbname for tests"
if [[ -n "$ODOO_SCRIPTS_DB_USER" ]]
then
echo "$INFO Using db user $ODOO_SCRIPTS_DB_USER"
db_user_param="--db_user $ODOO_SCRIPTS_DB_USER"
fi

Vincent Hatakeyama
committed
if [ "$ODOO_TYPE" = "odoo8" ];
then
loglevel="--log-level=info"
else
loglevel="--log-level=test"
fi

Vincent Hatakeyama
committed
if [[ -z "$odoo_scripts_run_tests_logfile" ]];
then
logfile=$(mktemp --suffix=.log)
function clean_logfile() {
rm $logfile
}
trap clean_logfile INT TERM
else
logfile=$odoo_scripts_run_tests_logfile
fi
if [[ -v COVERAGE_SOURCE ]];
then
if [[ -z "$COVERAGE_SOURCE" ]];
then
__coverage_source_command=""
else
__coverage_source_command="--source=$COVERAGE_SOURCE"
fi
else
__coverage_source_command="--source=$project_home"
fi

Vincent Hatakeyama
committed
if [ "$ODOO_TYPE" = "odoo7" ] || [ "$ODOO_TYPE" = "odoo8" ] || [ "$ODOO_TYPE" = "odoo9" ] || [ "$ODOO_TYPE" = "odoo10" ] ;

Vincent Hatakeyama
committed
# no coverage in those old versions
else
export PRE_ODOO_BIN="coverage run --omit='**/__manifest__.py' $__coverage_source_command --branch "

Vincent Hatakeyama
committed
# Catch both stdout & stderr here as Odoo 10 uses stderr, unlike previous Odoo versions.
env odoo_scripts_start_logfile=$logfile $here/start $update -d $dbname $db_user_param --test-enable $loglevel --max-cron=0 --workers=0 --stop-after-init $* 2>&1 | colorize
start_status=$pipestatus[1]
analyze $logfile && return_code=0 || return_code=$?
echo "$FATAL Tests failed (exit ${start_status})"