]> Git Repo - binutils.git/blob - gdb/unittests/gdb_tilde_expand-selftests.c
Automatic date update in version.in
[binutils.git] / gdb / unittests / gdb_tilde_expand-selftests.c
1 /* Self tests for gdb_tilde_expand
2
3    Copyright (C) 2021-2022 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
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.
11
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.
16
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/>.  */
19
20 #include "gdbsupport/common-defs.h"
21 #include "gdbsupport/selftest.h"
22 #include <cstdlib>
23
24 #include "gdbsupport/gdb_tilde_expand.h"
25
26 namespace selftests {
27 namespace gdb_tilde_expand_tests {
28
29 static void
30 do_test ()
31 {
32   /* Home directory of the user running the test.  */
33   const char *c_home = std::getenv ("HOME");
34
35   /* Skip the test if $HOME is not available in the environment.  */
36   if (c_home == nullptr)
37     return;
38
39   const std::string home (c_home);
40
41   /* Basic situation.  */
42   SELF_CHECK (home == gdb_tilde_expand ("~"));
43
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
46      expansion.  */
47   SELF_CHECK (gdb_tilde_expand ("~/non/existent/directory")
48               == home + "/non/existent/directory");
49
50   /* gdb_tilde_expand only expands tilde and does not try to do any other
51      substitution.  */
52   SELF_CHECK (gdb_tilde_expand ("~/*/a.out") == home + "/*/a.out");
53
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");
57
58   /* If $USER is available in the env variables, check the '~user'
59      expansion.  */
60   const char *c_user = std::getenv ("USER");
61   if (c_user != nullptr)
62     {
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 ())
66                   == home + "/a/b");
67     }
68
69   /* Check that an error is thrown when trying to expand home of a unknown
70      user.  */
71   try
72     {
73       gdb_tilde_expand ("~no_one_should_have_that_login/a");
74       SELF_CHECK (false);
75     }
76   catch (const gdb_exception_error &e)
77     {
78       SELF_CHECK (e.error == GENERIC_ERROR);
79       SELF_CHECK
80         (*e.message
81          == "Could not find a match for '~no_one_should_have_that_login'.");
82     }
83 }
84
85 } /* namespace gdb_tilde_expand_tests */
86 } /* namespace selftests */
87
88 void _initialize_gdb_tilde_expand_selftests ();
89 void
90 _initialize_gdb_tilde_expand_selftests ()
91 {
92   selftests::register_test
93     ("gdb_tilde_expand", selftests::gdb_tilde_expand_tests::do_test);
94 }
This page took 0.031359 seconds and 4 git commands to generate.