Line data Source code
1 : // Copyright (c) 2018-2026 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/snapdev
4 : // contact@m2osw.com
5 : //
6 : // This program is free software: you can redistribute it and/or modify
7 : // it under the terms of the GNU General Public License as published by
8 : // the Free Software Foundation, either version 3 of the License, or
9 : // (at your option) any later version.
10 : //
11 : // This program is distributed in the hope that it will be useful,
12 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : // GNU General Public License for more details.
15 : //
16 : // You should have received a copy of the GNU General Public License
17 : // along with this program. If not, see <https://www.gnu.org/licenses/>.
18 :
19 : /** \file
20 : * \brief Verify the string_replace_variables() function.
21 : *
22 : * Make sure that it does replace variables as expected.
23 : */
24 :
25 : // self
26 : //
27 : #include <snapdev/string_replace_variables.h>
28 :
29 : #include "catch_main.h"
30 :
31 :
32 : // C++
33 : //
34 : #include <deque>
35 : #include <list>
36 :
37 :
38 : // last include
39 : //
40 : #include <snapdev/poison.h>
41 :
42 :
43 :
44 7 : CATCH_TEST_CASE("string_replace_variables", "[string_replace_variables][string]")
45 : {
46 7 : CATCH_START_SECTION("string_replace_variables: empty input string")
47 : {
48 1 : std::string const empty;
49 1 : std::string result(snapdev::string_replace_variables(empty));
50 1 : CATCH_REQUIRE(empty == result);
51 1 : }
52 7 : CATCH_END_SECTION()
53 :
54 7 : CATCH_START_SECTION("string_replace_variables: replace $HOME or ${HOME}")
55 : {
56 : // we expect HOME to be set but if not use "/root"
57 : //
58 1 : char const * home(getenv("HOME"));
59 1 : if(home == nullptr)
60 : {
61 0 : setenv("HOME", "/root", 1);
62 0 : home = getenv("HOME");
63 0 : CATCH_REQUIRE(home != nullptr);
64 : }
65 :
66 : // try at the start of the string
67 : //
68 3 : std::string result(snapdev::string_replace_variables(std::string("$HOME/.cache/snapdev/memory.txt")));
69 3 : CATCH_REQUIRE(std::string(home) + "/.cache/snapdev/memory.txt" == result);
70 :
71 3 : result = snapdev::string_replace_variables(std::string("${HOME}/snapdev/intro"));
72 3 : CATCH_REQUIRE(std::string(home) + "/snapdev/intro" == result);
73 :
74 : // try in the middle of the string
75 : //
76 3 : result = snapdev::string_replace_variables(std::string("/magic:$HOME/snapdev"));
77 3 : CATCH_REQUIRE("/magic:" + std::string(home) + "/snapdev" == result);
78 :
79 3 : result = snapdev::string_replace_variables(std::string("/magic:${HOME}/snapdev"));
80 3 : CATCH_REQUIRE("/magic:" + std::string(home) + "/snapdev" == result);
81 :
82 : // try at the end of the string
83 : //
84 3 : result = snapdev::string_replace_variables(std::string("/var/lib/snapdev/$HOME"));
85 3 : CATCH_REQUIRE("/var/lib/snapdev/" + std::string(home) == result);
86 :
87 3 : result = snapdev::string_replace_variables(std::string("/run/${HOME}"));
88 3 : CATCH_REQUIRE("/run/" + std::string(home) == result);
89 1 : }
90 7 : CATCH_END_SECTION()
91 :
92 7 : CATCH_START_SECTION("string_replace_variables: test _SNAPDEV and snapdev123 (underscore, lowercase, digits)")
93 : {
94 : // test with an underscore
95 : //
96 1 : setenv("_SNAPDEV", "special_name", 1);
97 3 : std::string result(snapdev::string_replace_variables(std::string("Name = $_SNAPDEV {working?}")));
98 1 : CATCH_REQUIRE("Name = special_name {working?}" == result);
99 :
100 3 : result = snapdev::string_replace_variables(std::string("Name between braces = ${_SNAPDEV} {working?}"));
101 1 : CATCH_REQUIRE("Name between braces = special_name {working?}" == result);
102 :
103 : // test with lowercase and digits
104 : //
105 1 : setenv("snapdev123", "Lowercase & Digits", 1);
106 3 : result = snapdev::string_replace_variables(std::string("Another variable {$snapdev123}"));
107 1 : CATCH_REQUIRE("Another variable {Lowercase & Digits}" == result);
108 :
109 3 : result = snapdev::string_replace_variables(std::string("Double braces: {${snapdev123}}"));
110 1 : CATCH_REQUIRE("Double braces: {Lowercase & Digits}" == result);
111 1 : }
112 7 : CATCH_END_SECTION()
113 :
114 7 : CATCH_START_SECTION("string_replace_variables: replace $$ with pid")
115 : {
116 3 : std::string result(snapdev::string_replace_variables(std::string("/proc/$$/status")));
117 1 : CATCH_REQUIRE("/proc/" + std::to_string(getpid()) + "/status" == result);
118 1 : }
119 7 : CATCH_END_SECTION()
120 :
121 7 : CATCH_START_SECTION("string_replace_variables: dollar at the end of the string")
122 : {
123 3 : std::string result(snapdev::string_replace_variables(std::string("/ignore/this$")));
124 1 : CATCH_REQUIRE("/ignore/this$" == result);
125 1 : }
126 7 : CATCH_END_SECTION()
127 :
128 7 : CATCH_START_SECTION("string_replace_variables: no name")
129 : {
130 3 : std::string result(snapdev::string_replace_variables(std::string("/var/$/snapdev")));
131 1 : CATCH_REQUIRE("/var/$/snapdev" == result);
132 :
133 3 : result = snapdev::string_replace_variables(std::string("/var/${}/snapdev"));
134 1 : CATCH_REQUIRE("/var/${}/snapdev" == result);
135 1 : }
136 7 : CATCH_END_SECTION()
137 :
138 7 : CATCH_START_SECTION("string_replace_variables: invalid name")
139 : {
140 3 : std::string result(snapdev::string_replace_variables(std::string("/var/${bad/snapdev")));
141 1 : CATCH_REQUIRE("/var/${bad/snapdev" == result);
142 :
143 3 : result = snapdev::string_replace_variables(std::string("/var/${not_closing"));
144 1 : CATCH_REQUIRE("/var/${not_closing" == result);
145 1 : }
146 7 : CATCH_END_SECTION()
147 7 : }
148 :
149 :
150 :
151 : // vim: ts=4 sw=4 et
|