subsys/testsuite: Shorten the assertion messages

At present these can be very long since they include the full path of
the filename with the error.

    Assertion failed at /home/sglass/cosarm/zephry/zephyrproject/
	zephyr/tests/drivers/flash_simulator/src/main.c:102:
	test_write_read: (0 not equal to rc)

Reduce the length by omitting the current directory (where the tests
are being run) from the output:

    Assertion failed at tests/drivers/flash_simulator/src/main.c:102:
	test_write_read: (0 not equal to rc)

This improves usability for people running tests locally.

Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
Simon Glass 2020-05-07 15:17:14 -06:00 committed by Carles Cufí
commit 4b2c413e13
2 changed files with 33 additions and 3 deletions

View file

@ -23,6 +23,7 @@
extern "C" {
#endif
const char *ztest_relative_filename(const char *file);
void ztest_test_fail(void);
#if CONFIG_ZTEST_ASSERT_VERBOSE == 0
@ -30,7 +31,7 @@ static inline void z_zassert_(bool cond, const char *file, int line)
{
if (cond == false) {
PRINT("\n Assertion failed at %s:%d\n",
file, line);
ztest_relative_filename(file), line);
ztest_test_fail();
}
}
@ -51,7 +52,7 @@ static inline void z_zassert(bool cond,
va_start(vargs, msg);
PRINT("\n Assertion failed at %s:%d: %s: %s\n",
file, line, func, default_msg);
ztest_relative_filename(file), line, func, default_msg);
vprintk(msg, vargs);
printk("\n");
va_end(vargs);
@ -60,7 +61,7 @@ static inline void z_zassert(bool cond,
#if CONFIG_ZTEST_ASSERT_VERBOSE == 2
else {
PRINT("\n Assertion succeeded at %s:%d (%s)\n",
file, line, func);
ztest_relative_filename(file), line, func);
}
#endif
}

View file

@ -16,6 +16,10 @@
static struct k_thread ztest_thread;
#endif
#ifdef CONFIG_ARCH_POSIX
#include <unistd.h>
#endif
/* ZTEST_DMEM and ZTEST_BMEM are used for the application shared memory test */
ZTEST_DMEM enum {
@ -27,6 +31,31 @@ ZTEST_DMEM enum {
static ZTEST_BMEM int test_status;
/**
* @brief Try to shorten a filename by removing the current directory
*
* This helps to reduce the very long filenames in assertion failures. It
* removes the current directory from the filename and returns the rest.
* This makes assertions a lot more readable, and sometimes they fit on one
* line.
*
* @param file Filename to check
* @returns Shortened filename, or @file if it could not be shortened
*/
const char *ztest_relative_filename(const char *file)
{
#ifdef CONFIG_ARCH_POSIX
const char *cwd;
char buf[200];
cwd = getcwd(buf, sizeof(buf));
if (cwd && strlen(file) > strlen(cwd) &&
!strncmp(file, cwd, strlen(cwd)))
return file + strlen(cwd) + 1; /* move past the trailing '/' */
#endif
return file;
}
static int cleanup_test(struct unit_test *test)
{
int ret = TC_PASS;