Line data Source code
1 : // Copyright (c) 2006-2021 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/advgetopt
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 : // Tell catch we want it to add the runner code in this file.
21 : #define CATCH_CONFIG_RUNNER
22 :
23 : // self
24 : //
25 : #include "catch_main.h"
26 :
27 :
28 : // advgetopt lib
29 : //
30 : #include <advgetopt/advgetopt.h>
31 : #include <advgetopt/version.h>
32 :
33 :
34 : // libexcept lib
35 : //
36 : #include <libexcept/exception.h>
37 :
38 :
39 : // snapdev lib
40 : //
41 : #include <snapdev/not_used.h>
42 :
43 :
44 : // C++ lib
45 : //
46 : #include <sstream>
47 :
48 :
49 : // last include
50 : //
51 : #include <snapdev/poison.h>
52 :
53 :
54 :
55 :
56 :
57 : namespace SNAP_CATCH2_NAMESPACE
58 : {
59 :
60 :
61 :
62 2 : std::string g_tmp_dir;
63 :
64 2 : std::string g_config_filename;
65 2 : std::string g_config_project_filename;
66 :
67 :
68 183 : void init_tmp_dir(std::string const & project_name, std::string const & prefname, bool dir)
69 : {
70 366 : std::string tmpdir(SNAP_CATCH2_NAMESPACE::g_tmp_dir);
71 183 : tmpdir += "/.config";
72 366 : std::stringstream ss;
73 183 : if(dir)
74 : {
75 2 : ss << "mkdir -p " << tmpdir << "/" << prefname << "/" << project_name << ".d";
76 : }
77 : else
78 : {
79 181 : ss << "mkdir -p " << tmpdir << "/" << project_name << ".d";
80 : }
81 183 : if(system(ss.str().c_str()) != 0)
82 : {
83 0 : std::cerr << "fatal error: creating sub-temporary directory \"" << ss.str() << "\" failed.\n";
84 0 : exit(1);
85 : }
86 183 : if(dir)
87 : {
88 2 : g_config_filename = tmpdir + "/" + prefname;
89 2 : g_config_project_filename = tmpdir + "/" + prefname + "/" + project_name + ".d";
90 : }
91 : else
92 : {
93 181 : g_config_filename = tmpdir + "/" + prefname + ".config";
94 181 : g_config_project_filename = tmpdir + "/" + project_name + ".d/50-" + prefname + ".config";
95 : }
96 183 : }
97 :
98 :
99 :
100 : }
101 : // SNAP_CATCH2_NAMESPACE namespace
102 :
103 :
104 :
105 :
106 : namespace
107 : {
108 :
109 :
110 :
111 2 : Catch::clara::Parser add_command_line_options(Catch::clara::Parser const & cli)
112 : {
113 : return cli
114 4 : | Catch::clara::Opt(SNAP_CATCH2_NAMESPACE::g_tmp_dir, "tmp")
115 6 : ["-T"]["--tmp"]
116 10 : ("a path to a temporary directory used by the tests.");
117 : }
118 :
119 :
120 1 : int finish_init(Catch::Session & session)
121 : {
122 1 : snap::NOT_USED(session);
123 :
124 1 : if(!SNAP_CATCH2_NAMESPACE::g_tmp_dir.empty())
125 : {
126 1 : if(SNAP_CATCH2_NAMESPACE::g_tmp_dir == "/tmp")
127 : {
128 0 : std::cerr << "fatal error: you must specify a sub-directory for your temporary directory such as /tmp/advgetopt";
129 0 : exit(1);
130 : }
131 : }
132 : else
133 : {
134 0 : SNAP_CATCH2_NAMESPACE::g_tmp_dir = "/tmp/advgetopt";
135 : }
136 :
137 : // delete the existing tmp directory
138 : {
139 2 : std::stringstream ss;
140 1 : ss << "rm -rf \"" << SNAP_CATCH2_NAMESPACE::g_tmp_dir << "\"";
141 1 : if(system(ss.str().c_str()) != 0)
142 : {
143 0 : std::cerr << "fatal error: could not delete temporary directory \"" << SNAP_CATCH2_NAMESPACE::g_tmp_dir << "\".";
144 0 : exit(1);
145 : }
146 : }
147 :
148 : // then re-create the directory
149 : {
150 2 : std::stringstream ss;
151 1 : ss << "mkdir -p \"" << SNAP_CATCH2_NAMESPACE::g_tmp_dir << "\"";
152 1 : if(system(ss.str().c_str()) != 0)
153 : {
154 0 : std::cerr << "fatal error: could not create temporary directory \"" << SNAP_CATCH2_NAMESPACE::g_tmp_dir << "\".";
155 0 : exit(1);
156 : }
157 : }
158 :
159 1 : cppthread::set_log_callback(SNAP_CATCH2_NAMESPACE::log_for_test);
160 :
161 1 : char const * options(getenv("ADVGETOPT_TEST_OPTIONS"));
162 1 : if(options != nullptr
163 0 : && *options != '\0')
164 : {
165 0 : std::cerr << std::endl
166 : << "error:unittest: ADVGETOPT_TEST_OPTIONS already exists,"
167 : " the advgetopt tests would not work as expected with such."
168 0 : " Please unset that environment variable and try again."
169 0 : << std::endl;
170 0 : throw std::runtime_error("ADVGETOPT_TEST_OPTIONS already exists");
171 : }
172 :
173 1 : return 0;
174 : }
175 :
176 :
177 1 : void tests_done()
178 : {
179 1 : SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
180 1 : }
181 :
182 :
183 :
184 : }
185 : // no name namespace
186 :
187 :
188 :
189 :
190 :
191 :
192 2 : int main(int argc, char * argv[])
193 : {
194 4 : return SNAP_CATCH2_NAMESPACE::snap_catch2_main(
195 : "advgetopt"
196 : , LIBADVGETOPT_VERSION_STRING
197 : , argc
198 : , argv
199 6 : , []() { libexcept::set_collect_stack(libexcept::collect_stack_t::COLLECT_STACK_NO); }
200 : , &add_command_line_options
201 : , &finish_init
202 : , &tests_done
203 4 : );
204 6 : }
205 :
206 : // vim: ts=4 sw=4 et
|