# HG changeset patch
# User Vincent Hatakeyama <vincent.hatakeyama@xcg-consulting.fr>
# Date 1643901362 -3600
#      Thu Feb 03 16:16:02 2022 +0100
# Node ID 8a307edc7ff9c634df3d6aeb6d4c4f86c9060129
# Parent  299bb41cc0a2b2b0dcd3c31672c27677d5988515
📝 improve tests color output and make it compatible with odoo 15

diff --git a/NEWS.rst b/NEWS.rst
--- a/NEWS.rst
+++ b/NEWS.rst
@@ -7,6 +7,8 @@
 
 Also do branch coverage when doing coverage.
 
+Better output of odoo 15 tests with run_tests shell script.
+
 15.0.0
 ------
 
diff --git a/run_tests b/run_tests
--- a/run_tests
+++ b/run_tests
@@ -43,28 +43,39 @@
 then
     export PRE_ODOO_BIN="python3-coverage run --omit=__manifest__.py --source=$project_home "
 fi
+# colorize also remove database name and thread number
+alias colorize="sed -e 's/^\(\w*-..-.. ..:..:..,\w*\) \w*/\o033[2m\1\o033[22m/' -e 's/ INFO \w* \(odoo\.\(tests\.runner\|modules\.module\): \)\(.*\)/ \o033[32m\o033[7mINFO\o033[0m \1\o033[34m\3\o033[0m/' -e 's/ ERROR \w* \(odoo\.\(tests\.runner\|modules.module\): \)\(.*\)/ \o033[31m\o033[7mERROR\o033[0m \1\o033[34m\3\o033[0m/' -e 's/ \(INFO\) \(\w*\|?\) \([a-Z._-]*\):\(.*\)/ \o033[32m\o033[7m\1\o033[0m \3:\o033[32m\4\o033[39m/' -e 's/ \(DEBUG\) \w* \([a-Z\._-]*\):\(.*\)/ \o033[33m\o033[7m\1\o033[0m \2:\o033[33m\3\o033[39m/' -e 's/ \(WARNING\) \w* \([a-Z._-]*\):\(.*\)/ \o033[33m\o033[7m\1\o033[0m \2:\o033[33m\3\o033[0m/' -e 's/ \(ERROR\) \w* \([a-Z\._-]*\):\(.*\)/ \o033[31m\o033[7m\1\o033[0m \2:\o033[31m\3\o033[0m/' -e 's/ FAILED/ \o033[41m\o033[97m\o033[1mFAILED\o033[0m/' -e 's/ FAIL\(.*\)/ \o033[41m\o033[97m\o033[1mFAIL\o033[49m\o033[31m\1\o033[0m/' -e 's/ \(CRITICAL\) \w* \([a-Z\._-]*\):\(.*\)/ \o033[33m\o033[7m\1\o033[0m \2:\o033[33m\3\o033[39m/' -e 's/ \(TEST\) \w* \(.*\) \(ERROR\)\(.*\)/ \o033[34m\o033[7m\1\o033[27m \2\o033[0m \o033[41m\o033[97m\o033[1m\3\o033[49m\o033[0m/' -e 's/ \(TEST\) \w* \(.*\) \(OK\)/ \o033[34m\o033[7m\1\o033[27m \2\o033[0m \o033[32m\o033[7m\3\o033[27m\o033[39m/' -e 's/ \(TEST\) \w* \([a-Z\._-]*\):\(.*\)/ \o033[34m\o033[7m\1\o033[0m \2:\o033[34m\3\o033[0m/'"
 # Catch both stdout & stderr here as Odoo 10 uses stderr, unlike previous Odoo versions.
-$here/start $update -d $dbname $db_user_param  --test-enable $loglevel --max-cron=0 --stop-after-init $* 2>&1 | tee $logfile | sed -e 's/\(.*\) \(INFO\) \(.*\)/\o033[2m\1\o033[22m \o033[32m\o033[7m\2\o033[27m \3\o033[39m/' -e 's/\(.*\) \(DEBUG\) \(.*\)/\o033[2m\1\o033[22m \o033[33m\o033[7m\2\o033[27m \2\o033[39m/' -e 's/\(.*\) \(WARNING\) \(.*\)/\o033[2m\1\o033[22m \o033[33m\o033[7m\2\o033[27m \3\o033[39m/' -e 's/\(.*\) \(ERROR\) \(.*\)/\o033[2m\1\o033[22m \o033[31m\o033[7m\2\o033[27m \3\o033[0m/' -e 's/ FAILED/ \o033[41m\o033[97m\o033[1mFAILED\o033[0m/' -e 's/ FAIL\(.*\)/ \o033[41m\o033[97m\o033[1mFAIL\o033[49m\o033[31m\1\o033[0m/' -e 's/\(.*\) \(CRITICAL\) \(.*\)/\o033[2m\1\o033[22m \o033[33m\o033[7m\2\o033[27m \3\o033[39m/' -e 's/\(.*\) \(TEST\) \(.*\) \(ERROR\)\(.*\)/\o033[2m\1\o033[22m \o033[34m\o033[7m\2\o033[27m \3\o033[0m \o033[41m\o033[97m\o033[1m\4\o033[49m\o033[31m\5\o033[0m/' -e 's/\(.*\) \(TEST\) \(.*\) \(OK\)/\o033[2m\1\o033[22m \o033[34m\o033[7m\2\o033[27m \3\o033[0m \o033[32m\o033[7m\4\o033[27m\o033[39m/' -e 's/\(.*\) \(TEST\) \(.*\)/\o033[2m\1\o033[22m \o033[34m\o033[7m\2\o033[27m \3 \o033[0m/'
+$here/start $update -d $dbname $db_user_param  --test-enable $loglevel --max-cron=0 --stop-after-init $* 2>&1 | tee $logfile | colorize
 start_status=$pipestatus[1]
-# Valid with odoo 7, 11, 13 and probably the rest too
-tests_ran=$(grep -P 'o(penerp|doo).(modules.module|[^ ]+): Ran \K([[:digit:]]+)' $logfile -o | perl -ne '$sum += $_ } { print $sum')
-tests_failures=$(grep -P 'o(penerp|doo).(modules.module|[^ ]+): Module [a-z0-9_]+: \K([[:digit:]]+)' $logfile -o | perl -ne '$sum += $_ } { print $sum')
-# that might be doable in the perl above
-if [ -z "$tests_failures" ];
+# Odoo 15 detection
+if [ $(grep -P 'odoo\.tests\.runner: ([[:digit:]]+) failed, [[:digit:]]+ error\(s\) of [[:digit:]]+ tests when loading database' $logfile --count) -gt 0 ];
 then
-    tests_failures=0
-fi
-tests_errors=$(grep -P 'o(penerp|doo).(modules.module|[^ ]+): Module [a-z0-9_]+: [[:digit:]]+ failures, \K([[:digit:]]+)' $logfile -o | perl -ne '$sum += $_ } { print $sum')
-if [ -z "$tests_errors" ];
-then
-    tests_errors=0
+  # Valid with odoo 15
+  tests_ran=$(grep -P 'odoo\.tests\.runner: ([[:digit:]]+) failed, [[:digit:]]+ error\(s\) of \K[[:digit:]]+' $logfile -o)
+  tests_failures=$(grep -P 'odoo\.tests\.runner: \K([[:digit:]]+)' $logfile -o)
+  test_errors=$(grep -P 'odoo\.tests\.runner: ([[:digit:]]+) failed, \K[[:digit:]]+' $logfile -o)
+else
+  # Valid with odoo 7, 11, 13
+  tests_ran=$(grep -P 'o(penerp|doo).(modules.module|[^ ]+): Ran \K([[:digit:]]+)' $logfile -o | perl -ne '$sum += $_ } { print $sum')
+  tests_failures=$(grep -P 'o(penerp|doo).(modules.module|[^ ]+): Module [a-z0-9_]+: \K([[:digit:]]+)' $logfile -o | perl -ne '$sum += $_ } { print $sum')
+  # that might be doable in the perl above
+  if [ -z "$tests_failures" ];
+  then
+      tests_failures=0
+  fi
+  tests_errors=$(grep -P 'o(penerp|doo).(modules.module|[^ ]+): Module [a-z0-9_]+: [[:digit:]]+ failures, \K([[:digit:]]+)' $logfile -o | perl -ne '$sum += $_ } { print $sum')
+  if [ -z "$tests_errors" ];
+  then
+      tests_errors=0
+  fi
 fi
 # create a OpenMetrics metrics file
 if [ "$CI" = "true" ];
 then
     echo -e "odoo_scripts_tests_ran $tests_ran\nodoo_scripts_tests_failures $tests_failures\nodoo_scripts_tests_error $tests_error\n# EOF\n" > metrics
 fi
-if [ $ODOO_TYPE != 'odoo13' ];
+if [ "$ODOO_TYPE" = 'odoo7' ] || [ "$ODOO_TYPE" = 'odoo8' ] || [ "$ODOO_TYPE" = 'odoo9' ] || [ "$ODOO_TYPE" = 'odoo10' ] || [ "$ODOO_TYPE" = 'odoo11' ];
 then
     ok=$(grep ' OK' $logfile -c)
 fi
@@ -79,8 +90,8 @@
 fi
 echo ''
 echo "${esc}[2m*****${esc}[22m ${esc}[1mTest results${esc}[21m ${esc}[2m*****${esc}[22m"
-echo "Ran $tests_ran tests, $tests_failures failures, $tests_errors errors"
-if [ $ODOO_TYPE != 'odoo13' ];
+echo "$tests_failures failed, $tests_error error(s) of $tests_ran tests"
+if [ "$ODOO_TYPE" = 'odoo7' ] || [ "$ODOO_TYPE" = 'odoo8' ] || [ "$ODOO_TYPE" = 'odoo9' ] || [ "$ODOO_TYPE" = 'odoo10' ] || [ "$ODOO_TYPE" = 'odoo11' ];
 then
     echo "$OK $ok modules passing"
 fi