[PATCH] Improve gdb_tilde_expand logic

Simon Marchi simon.marchi@polymtl.ca
Sat Jan 9 02:17:26 GMT 2021


On 2021-01-08 8:33 p.m., Lancelot SIX wrote:
> The option I have in mind is inspired by gdb.gdb/selftest.exp (if I understand it correctly): once I have a gdb session debugging gdb, I can have something like:
> 
>     gdb_test {print gdb_tilde_expand ("~/most/probably/non/existing").c_str ()} \
>              {= .*"/.*/most/probably/non/existing"}
> 
> or something similar. If I go for something like that, can it be added directly in selftest.expr (under gdb.gdb) or do it require an entire new testcase (probably in gdb.base)?  It might be an overkill approach to unit-testing, but it should work.

Well, we now have a "selftest" framework integrated in the GDB binary
itself, which makes it much more convenient to unit test functions.
It's also called selftest, but it's not related to gdb.gdb/selftest.exp.

I mislead you in my previous message when I said you could place the
test in gdbsupport.  Well, the test could be in gdbsupport, but there
needs to be something that registers the test, which is usually done
in an _initialize_* function in GDB.  So there are already some tests
for gdbsupport stuff placed in gdb/unittest.

You can use the patch below as a starting point.

You can run it like this:

(gdb) maintenance selftest tilde
Running selftest gdb_tilde_expand.
Self test failed: Could not find a match for '~/non/existent/directory'.
Ran 1 unit tests, 1 failed

With your fix, there shouldn't be an exception thrown.  And it would be
a good idea to add a SELF_CHECK (i.e. an assert) to check the result,
for example we expect it to contain "/non/existent/directory" somewhere.

Simon

>From 65013bf368ca127ff51d96c6efb053209afc0ad5 Mon Sep 17 00:00:00 2001
From: Simon Marchi <simon.marchi@polymtl.ca>
Date: Fri, 8 Jan 2021 21:12:19 -0500
Subject: [PATCH] gdb: add selftest for gdb_tilde_expand

Change-Id: Icfcbc761a9b39c9b66634985a23b92e9a65ca761
---
 gdb/Makefile.in                            |  1 +
 gdb/unittests/gdb_tilde_expand-selftests.c | 44 ++++++++++++++++++++++
 2 files changed, 45 insertions(+)
 create mode 100644 gdb/unittests/gdb_tilde_expand-selftests.c

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index 9267bea7beb6..c8979fe2760b 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -446,6 +446,7 @@ SELFTESTS_SRCS = \
 	unittests/filtered_iterator-selftests.c \
 	unittests/format_pieces-selftests.c \
 	unittests/function-view-selftests.c \
+	unittests/gdb_tilde_expand-selftests.c \
 	unittests/gmp-utils-selftests.c \
 	unittests/lookup_name_info-selftests.c \
 	unittests/memory-map-selftests.c \
diff --git a/gdb/unittests/gdb_tilde_expand-selftests.c b/gdb/unittests/gdb_tilde_expand-selftests.c
new file mode 100644
index 000000000000..c68ea99dac83
--- /dev/null
+++ b/gdb/unittests/gdb_tilde_expand-selftests.c
@@ -0,0 +1,44 @@
+/* Self tests for gdb_tilde_expand
+
+   Copyright (C) 2021 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include "gdbsupport/common-defs.h"
+#include "gdbsupport/selftest.h"
+
+#include "gdbsupport/gdb_tilde_expand.h"
+
+namespace selftests {
+namespace gdb_tilde_expand_tests {
+
+static void
+do_test ()
+{
+  std::string ret = gdb_tilde_expand ("~");
+  ret = gdb_tilde_expand ("~/non/existent/directory");
+}
+
+} /* namespace gdb_tilde_expand_tests */
+} /* namespace selftests */
+
+void _initialize_gdb_tilde_expand_selftests ();
+void
+_initialize_gdb_tilde_expand_selftests ()
+{
+  selftests::register_test
+    ("gdb_tilde_expand", selftests::gdb_tilde_expand_tests::do_test);
+}
-- 
2.29.2




More information about the Gdb-patches mailing list