Commit 4b2c413e authored by Simon Glass's avatar Simon Glass Committed by Carles Cufi
Browse files

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: default avatarSimon Glass <sjg@chromium.org>
parent 1a163673
Loading
Loading
Loading
Loading
+4 −3
Original line number Diff line number Diff line
@@ -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
}
+29 −0
Original line number Diff line number Diff line
@@ -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;