Skip to content
Snippets Groups Projects
run_tests 3.24 KiB
Newer Older
Vincent Hatakeyama's avatar
Vincent Hatakeyama committed
#!/bin/zsh
# vim: set shiftwidth=4 softtabstop=4:
#
# 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.
# 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
Vincent Hatakeyama's avatar
Vincent Hatakeyama committed
#   If set to empty, no source is given to coverage. (changed in 22.0.0, previous versions considered empty as unset)
#   If not set, compute the value from the tested modules
#
Vincent Hatakeyama's avatar
Vincent Hatakeyama committed
# Version 22.0.0
source $here/odoo_scripts_common

tested_modules=${ODOO_SCRIPTS_MODULE_LIST_TESTS-$(read_odoo_scripts_expandable_configuration_key module_list_tests)}

if [[ "$tested_modules" != "" ]];
then 
    update="-i $tested_modules"
    if [[ -v COVERAGE_SOURCE ]];
      # Source already set
    else
          # 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(',')))")"
      else
          COVERAGE_SOURCE="$(python2 -B -c "print(','.join('odoo.addons.{}'.format(name) for name in '$tested_modules'.split(',')))")"
      fi
    fi
Vincent Hatakeyama's avatar
Vincent Hatakeyama committed
dbname=$1
if [[ -z "$dbname" ]];
then
Vincent Hatakeyama's avatar
Vincent Hatakeyama committed
else
Vincent Hatakeyama's avatar
Vincent Hatakeyama committed
fi
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

if [ "$ODOO_TYPE" = "odoo8" ];
then
    loglevel="--log-level=info"
else
    loglevel="--log-level=test"
fi
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
if [ "$ODOO_TYPE" = "odoo7" ] || [ "$ODOO_TYPE" = "odoo8" ] || [ "$ODOO_TYPE" = "odoo9" ] || [ "$ODOO_TYPE" = "odoo10" ] ;
    export PRE_ODOO_BIN="coverage run --omit='**/__manifest__.py' $__coverage_source_command --branch "
# 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=$?
if [[ $start_status -ne 0 ]];
then
    echo "$FATAL Tests failed (exit ${start_status})"
    exit $start_status
fi