Line data Source code
1 : // Copyright (c) 2021-2022 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 2 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 along
17 : // with this program; if not, write to the Free Software Foundation, Inc.,
18 : // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 :
20 : /** \file
21 : * \brief Verify that the path functions work.
22 : *
23 : * This file implements tests for the pathinfo function.
24 : */
25 :
26 : // self
27 : //
28 : #include <snapdev/glob_to_list.h>
29 :
30 : #include "catch_main.h"
31 :
32 :
33 : // C++ lib
34 : //
35 : #include <fstream>
36 : #include <set>
37 :
38 :
39 : // last include
40 : //
41 : #include <snapdev/poison.h>
42 :
43 :
44 :
45 :
46 3 : CATCH_TEST_CASE("glob_to_list", "[glob_to_list]")
47 : {
48 2 : CATCH_START_SECTION("pathinfo: replace existing suffix")
49 : {
50 2 : std::string top_dir(SNAP_CATCH2_NAMESPACE::g_tmp_dir() + "/top");
51 1 : int r(system(("rm -rf " + top_dir).c_str()));
52 1 : CATCH_REQUIRE(r == 0);
53 1 : CATCH_REQUIRE(mkdir(top_dir.c_str(), 0777) == 0);
54 :
55 2 : std::string foo_dir(top_dir + "/foo");
56 1 : CATCH_REQUIRE(mkdir(foo_dir.c_str(), 0777) == 0);
57 :
58 2 : std::string bar_dir(top_dir + "/bar");
59 1 : CATCH_REQUIRE(mkdir(bar_dir.c_str(), 0777) == 0);
60 :
61 2 : std::string sub_bar_dir(bar_dir + "/sub");
62 1 : CATCH_REQUIRE(mkdir(sub_bar_dir.c_str(), 0777) == 0);
63 :
64 2 : std::string hidden_bar_dir(sub_bar_dir + "/hidden");
65 1 : CATCH_REQUIRE(mkdir(hidden_bar_dir.c_str(), 0777) == 0);
66 :
67 2 : std::string foo_txt(foo_dir + "/foo.txt");
68 2 : std::ofstream foo_file;
69 1 : foo_file.open(foo_txt);
70 1 : foo_file << "foo file\n";
71 1 : CATCH_REQUIRE(foo_file.good());
72 :
73 2 : std::string foo2_txt(foo_dir + "/foo-2.txt");
74 2 : std::ofstream foo2_file;
75 1 : foo2_file.open(foo2_txt);
76 1 : foo2_file << "foo #2 file\n";
77 1 : CATCH_REQUIRE(foo2_file.good());
78 :
79 2 : std::string bar_txt(bar_dir + "/bar.txt");
80 2 : std::ofstream bar_file;
81 1 : bar_file.open(bar_txt);
82 1 : bar_file << "bar file\n";
83 1 : CATCH_REQUIRE(bar_file.good());
84 :
85 2 : std::string bar2_txt(bar_dir + "/bar_2.txt");
86 2 : std::ofstream bar2_file;
87 1 : bar2_file.open(bar2_txt);
88 1 : bar2_file << "bar two file\n";
89 1 : CATCH_REQUIRE(bar2_file.good());
90 :
91 2 : std::string sub_bar3_txt(sub_bar_dir + "/sub-bar3.txt");
92 2 : std::ofstream sub_bar3_file;
93 1 : sub_bar3_file.open(sub_bar3_txt);
94 1 : sub_bar3_file << "no. 3 -- sub-bar file\n";
95 1 : CATCH_REQUIRE(sub_bar3_file.good());
96 :
97 2 : std::string hidden_settings_conf(hidden_bar_dir + "/settings.conf");
98 2 : std::ofstream hidden_settings_file;
99 1 : hidden_settings_file.open(hidden_settings_conf);
100 1 : hidden_settings_file << "hidden=\"config file\"\n";
101 1 : CATCH_REQUIRE(hidden_settings_file.good());
102 :
103 : // soft links to a file are kept
104 : //
105 2 : std::string soft_link_txt(sub_bar_dir + "/soft-link.txt");
106 1 : CATCH_REQUIRE(symlink("../../foo/foo-2.txt", soft_link_txt.c_str()) == 0);
107 :
108 : // soft links to a directory are ignored by default
109 : //
110 2 : std::string soft_dir(sub_bar_dir + "/soft-dir");
111 1 : CATCH_REQUIRE(symlink("../../bar", soft_dir.c_str()) == 0);
112 :
113 : // soft links which loops forever
114 : //
115 2 : std::string loop_dir(hidden_bar_dir + "/loop.lnk");
116 1 : CATCH_REQUIRE(symlink("../hidden", loop_dir.c_str()) == 0);
117 :
118 : // read all the files (*)
119 : //
120 : typedef std::set<std::string> strings_t;
121 2 : snapdev::glob_to_list<strings_t> all_glob;
122 1 : CATCH_REQUIRE(all_glob.read_path<
123 : snapdev::glob_to_list_flag_t::GLOB_FLAG_RECURSIVE>(top_dir + "/*"));
124 :
125 : // soft-links are followed and the realpath() function returns
126 : // the target; here we "computed" what the glob() is expected to
127 : // return (i.e. not the target)
128 : //
129 2 : std::string const soft_link_link(all_glob.get_real_path(sub_bar_dir) + "/soft-link.txt");
130 2 : std::string const soft_dir_link(all_glob.get_real_path(sub_bar_dir) + "/soft-dir");
131 2 : std::string const loop_dir_link(all_glob.get_real_path(hidden_bar_dir) + "/loop.lnk");
132 :
133 1 : CATCH_REQUIRE(all_glob.size() == 13);
134 1 : CATCH_REQUIRE(all_glob.count(all_glob.get_real_path(bar_dir)) == 1);
135 1 : CATCH_REQUIRE(all_glob.count(all_glob.get_real_path(bar_txt)) == 1);
136 1 : CATCH_REQUIRE(all_glob.count(all_glob.get_real_path(bar2_txt)) == 1);
137 1 : CATCH_REQUIRE(all_glob.count(all_glob.get_real_path(sub_bar_dir)) == 1);
138 1 : CATCH_REQUIRE(all_glob.count(all_glob.get_real_path(hidden_bar_dir)) == 1);
139 1 : CATCH_REQUIRE(all_glob.count(loop_dir_link) == 1);
140 1 : CATCH_REQUIRE(all_glob.count(all_glob.get_real_path(hidden_settings_conf)) == 1);
141 1 : CATCH_REQUIRE(all_glob.count(soft_dir_link) == 1);
142 1 : CATCH_REQUIRE(all_glob.count(soft_link_link) == 1);
143 1 : CATCH_REQUIRE(all_glob.count(all_glob.get_real_path(sub_bar3_txt)) == 1);
144 1 : CATCH_REQUIRE(all_glob.count(all_glob.get_real_path(foo_dir)) == 1);
145 1 : CATCH_REQUIRE(all_glob.count(all_glob.get_real_path(foo2_txt)) == 1);
146 1 : CATCH_REQUIRE(all_glob.count(all_glob.get_real_path(foo_txt)) == 1);
147 :
148 : // read all text files (*.txt)
149 : //
150 : typedef std::set<std::string> strings_t;
151 2 : snapdev::glob_to_list<strings_t> txt_glob;
152 1 : CATCH_REQUIRE(txt_glob.read_path<
153 : snapdev::glob_to_list_flag_t::GLOB_FLAG_RECURSIVE>(top_dir + "/*.txt"));
154 :
155 1 : CATCH_REQUIRE(txt_glob.size() == 6);
156 1 : CATCH_REQUIRE(txt_glob.count(txt_glob.get_real_path(bar_txt)) == 1);
157 1 : CATCH_REQUIRE(txt_glob.count(txt_glob.get_real_path(bar2_txt)) == 1);
158 1 : CATCH_REQUIRE(txt_glob.count(txt_glob.get_real_path(soft_link_link)) == 1);
159 1 : CATCH_REQUIRE(txt_glob.count(txt_glob.get_real_path(sub_bar3_txt)) == 1);
160 1 : CATCH_REQUIRE(txt_glob.count(txt_glob.get_real_path(foo2_txt)) == 1);
161 1 : CATCH_REQUIRE(txt_glob.count(txt_glob.get_real_path(foo_txt)) == 1);
162 :
163 : // read text file within the bar sub-directory
164 : //
165 : typedef std::set<std::string> strings_t;
166 2 : snapdev::glob_to_list<strings_t> sub_bar_glob;
167 1 : CATCH_REQUIRE(sub_bar_glob.read_path<
168 : snapdev::glob_to_list_flag_t::GLOB_FLAG_RECURSIVE>(sub_bar_dir + "/*.txt"));
169 :
170 1 : CATCH_REQUIRE(sub_bar_glob.size() == 2);
171 1 : CATCH_REQUIRE(sub_bar_glob.count(soft_link_link) == 1);
172 1 : CATCH_REQUIRE(sub_bar_glob.count(sub_bar_glob.get_real_path(sub_bar3_txt)) == 1);
173 :
174 : // read text files within the bar sub-directory and follow symlinks
175 : //
176 : typedef std::set<std::string> strings_t;
177 2 : snapdev::glob_to_list<strings_t> symlink_glob;
178 1 : CATCH_REQUIRE(symlink_glob.read_path<
179 : snapdev::glob_to_list_flag_t::GLOB_FLAG_FOLLOW_SYMLINK
180 : , snapdev::glob_to_list_flag_t::GLOB_FLAG_RECURSIVE>(sub_bar_dir + "/*.txt"));
181 :
182 1 : CATCH_REQUIRE(symlink_glob.size() == 4);
183 1 : CATCH_REQUIRE(symlink_glob.count(symlink_glob.get_real_path(bar_txt)) == 1);
184 1 : CATCH_REQUIRE(symlink_glob.count(symlink_glob.get_real_path(bar2_txt)) == 1);
185 1 : CATCH_REQUIRE(symlink_glob.count(soft_link_link) == 1);
186 1 : CATCH_REQUIRE(symlink_glob.count(symlink_glob.get_real_path(sub_bar3_txt)) == 1);
187 :
188 : }
189 : CATCH_END_SECTION()
190 7 : }
191 :
192 :
193 :
194 : // vim: ts=4 sw=4 et
|