gdb_tilde_expand-selftests.c 3.0 KB

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