1 /* Self tests for gdb_tilde_expand
3 Copyright (C) 2021-2023 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "gdbsupport/common-defs.h"
21 #include "gdbsupport/selftest.h"
24 #include "gdbsupport/gdb_tilde_expand.h"
27 namespace gdb_tilde_expand_tests
{
32 /* Home directory of the user running the test. */
33 const char *c_home
= std::getenv ("HOME");
35 /* Skip the test if $HOME is not available in the environment. */
36 if (c_home
== nullptr)
39 const std::string
home (c_home
);
41 /* Basic situation. */
42 SELF_CHECK (home
== gdb_tilde_expand ("~"));
44 /* When given a path that begins by a tilde and refers to a file that
45 does not exist, gdb_tilde expand must still be able to do the tilde
47 SELF_CHECK (gdb_tilde_expand ("~/non/existent/directory")
48 == home
+ "/non/existent/directory");
50 /* gdb_tilde_expand only expands tilde and does not try to do any other
52 SELF_CHECK (gdb_tilde_expand ("~/*/a.out") == home
+ "/*/a.out");
54 /* gdb_tilde_expand does no modification to a non tilde leading path. */
55 SELF_CHECK (gdb_tilde_expand ("/some/abs/path") == "/some/abs/path");
56 SELF_CHECK (gdb_tilde_expand ("relative/path") == "relative/path");
58 /* If $USER is available in the env variables, check the '~user'
60 const char *c_user
= std::getenv ("USER");
61 if (c_user
!= nullptr)
63 const std::string
user (c_user
);
64 SELF_CHECK (gdb_tilde_expand (("~" + user
).c_str ()) == home
);
65 SELF_CHECK (gdb_tilde_expand (("~" + user
+ "/a/b").c_str ())
69 /* Check that an error is thrown when trying to expand home of a unknown
73 gdb_tilde_expand ("~no_one_should_have_that_login/a");
76 catch (const gdb_exception_error
&e
)
78 SELF_CHECK (e
.error
== GENERIC_ERROR
);
81 == "Could not find a match for '~no_one_should_have_that_login'.");
85 } /* namespace gdb_tilde_expand_tests */
86 } /* namespace selftests */
88 void _initialize_gdb_tilde_expand_selftests ();
90 _initialize_gdb_tilde_expand_selftests ()
92 selftests::register_test
93 ("gdb_tilde_expand", selftests::gdb_tilde_expand_tests::do_test
);