From 70f9dde739ac3a5aebeb55b7c3e331ed3ea88f03 Mon Sep 17 00:00:00 2001 From: MDW Date: Thu, 30 Jan 2025 17:53:56 +0100 Subject: [PATCH] Qual: Improve lang test + message in case of test exception # Qual: Improve lang test + message in case of test exception This fixes the language test enabling the error message itself. I also improved the Exception handling so that a backtrace is shown to help find where it happened (here the error message was empty which was strange and made it harder to find - the (empty(val)?a:b) was lacking parentheses for proper concatenation --- test/phpunit/CommonClassTest.class.php | 12 +++++++++++- test/phpunit/LangTest.php | 12 ++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/test/phpunit/CommonClassTest.class.php b/test/phpunit/CommonClassTest.class.php index d083e842758..4f75b562dcd 100644 --- a/test/phpunit/CommonClassTest.class.php +++ b/test/phpunit/CommonClassTest.class.php @@ -1,7 +1,7 @@ * Copyright (C) 2023 Alexandre Janniaux - * Copyright (C) 2024 MDW + * Copyright (C) 2024-2025 MDW * Copyright (C) 2024 Frédéric France * * This program is free software; you can redistribute it and/or modify @@ -168,6 +168,16 @@ abstract class CommonClassTest extends TestCase print "##[group]$className::$failedTestMethod failed - $argsText.".PHP_EOL; print "## ".get_class($t).": {$t->getMessage()}".PHP_EOL; + // Show some information about where it happened + foreach ($t->getTrace() as $idx => $trace) { + if (isset($trace['file'], $trace['line']) // Only if we have a file name + && !preg_match('/(?:\bphar\b|Framework)/', $trace['file']) // Only if it's not in phpunit + ) { + print "## backtrace($idx): From {$trace['file']}:{$trace['line']}.".PHP_EOL; + } + } + + if ($nbLinesToShow) { $newLines = count($last_lines); if ($newLines > 0) { diff --git a/test/phpunit/LangTest.php b/test/phpunit/LangTest.php index 1cb8c53c532..2442b165848 100644 --- a/test/phpunit/LangTest.php +++ b/test/phpunit/LangTest.php @@ -1,7 +1,7 @@ * Copyright (C) 2023 Alexandre Janniaux - * Copyright (C) 2024 MDW + * Copyright (C) 2024-2025 MDW * Copyright (C) 2024 Frédéric France * * This program is free software; you can redistribute it and/or modify @@ -194,14 +194,14 @@ class LangTest extends CommonClassTest include_once DOL_DOCUMENT_ROOT.'/core/class/translate.class.php'; - $prefix = __METHOD__."($code) "; $tmplangs = new Translate('', $conf); $langcode = $code; $tmplangs->setDefaultLang($langcode); $tmplangs->load("main"); - print PHP_EOL.$prefix."Check language file".PHP_EOL; + print PHP_EOL.$prefix."Check language files".PHP_EOL; + $result = $tmplangs->transnoentitiesnoconv("FONTFORPDF"); print $prefix."FONTFORPDF=".$result.PHP_EOL; $this->assertTrue(in_array($result, array('msungstdlight', 'stsongstdlight', 'helvetica', 'DejaVuSans', 'cid0jp', 'cid0kr', 'freemono', 'freeserif')), 'Error bad value '.$result.' for FONTFORPDF in main.lang file '.$code); @@ -231,14 +231,14 @@ class LangTest extends CommonClassTest unset($tmplangs); - print $prefix."Check some syntax rules in the language file".PHP_EOL; + print $prefix."Check syntax rules in the language files".PHP_EOL; $filesarray2 = scandir(DOL_DOCUMENT_ROOT.'/langs/'.$code); foreach ($filesarray2 as $key => $file) { if (! preg_match('/\.lang$/', $file)) { continue; } - //print 'Check lang file '.$file.PHP_EOL; + //print $prefix.'Check lang file '.$file.PHP_EOL; $filecontent = file_get_contents(DOL_DOCUMENT_ROOT.'/langs/'.$code.'/'.$file); $result = preg_match('/=--$/m', $filecontent); // A special % char we don't want. We want the common one. @@ -260,7 +260,7 @@ class LangTest extends CommonClassTest $reg = array(); $result = preg_match('/(.*)\'notranslate\'/im', $filecontent, $reg); // A sequence of char we don't want //print $prefix."Result for checking we don't have bad percent char = ".$result.PHP_EOL; - $this->assertTrue($result == 0, 'Found a sequence tag \'notranslate\' in the translation file '.$code.'/'.$file.' in line '.empty($reg[1]) ? '' : $reg[1]); + $this->assertTrue($result == 0, 'Found a sequence tag \'notranslate\' in the translation file '.$code.'/'.$file.' in line '.(empty($reg[1]) ? '' : $reg[1])); if (!in_array($code, array('ar_SA'))) { $reg = array();