Line data Source code
1 : /*
2 : * License:
3 : * Copyright (c) 2006-2021 Made to Order Software Corp. All Rights Reserved
4 : *
5 : * https://snapwebsites.org/project/advgetopt
6 : * contact@m2osw.com
7 : *
8 : * This program is free software; you can redistribute it and/or modify
9 : * it under the terms of the GNU General Public License as published by
10 : * the Free Software Foundation; either version 2 of the License, or
11 : * (at your option) any later version.
12 : *
13 : * This program is distributed in the hope that it will be useful,
14 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 : * GNU General Public License for more details.
17 : *
18 : * You should have received a copy of the GNU General Public License along
19 : * with this program; if not, write to the Free Software Foundation, Inc.,
20 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 : *
22 : * Authors:
23 : * Alexis Wilke alexis@m2osw.com
24 : */
25 :
26 : // Tell catch we want it to add the runner code in this file.
27 : #define CATCH_CONFIG_RUNNER
28 :
29 : // self
30 : //
31 : #include "main.h"
32 :
33 : // advgetopt lib
34 : //
35 : #include <advgetopt/advgetopt.h>
36 : #include <advgetopt/version.h>
37 :
38 : // libexcept lib
39 : //
40 : #include <libexcept/exception.h>
41 :
42 : // snapdev lib
43 : //
44 : #include <snapdev/not_used.h>
45 :
46 : // C++ lib
47 : //
48 : #include <sstream>
49 :
50 :
51 : namespace SNAP_CATCH2_NAMESPACE
52 : {
53 :
54 :
55 :
56 2 : std::string g_tmp_dir;
57 :
58 2 : std::string g_config_filename;
59 2 : std::string g_config_project_filename;
60 :
61 :
62 183 : void init_tmp_dir(std::string const & project_name, std::string const & prefname, bool dir)
63 : {
64 366 : std::string tmpdir(SNAP_CATCH2_NAMESPACE::g_tmp_dir);
65 183 : tmpdir += "/.config";
66 366 : std::stringstream ss;
67 183 : if(dir)
68 : {
69 2 : ss << "mkdir -p " << tmpdir << "/" << prefname << "/" << project_name << ".d";
70 : }
71 : else
72 : {
73 181 : ss << "mkdir -p " << tmpdir << "/" << project_name << ".d";
74 : }
75 183 : if(system(ss.str().c_str()) != 0)
76 : {
77 0 : std::cerr << "fatal error: creating sub-temporary directory \"" << ss.str() << "\" failed.\n";
78 0 : exit(1);
79 : }
80 183 : if(dir)
81 : {
82 2 : g_config_filename = tmpdir + "/" + prefname;
83 2 : g_config_project_filename = tmpdir + "/" + prefname + "/" + project_name + ".d";
84 : }
85 : else
86 : {
87 181 : g_config_filename = tmpdir + "/" + prefname + ".config";
88 181 : g_config_project_filename = tmpdir + "/" + project_name + ".d/50-" + prefname + ".config";
89 : }
90 183 : }
91 :
92 :
93 :
94 : }
95 : // SNAP_CATCH2_NAMESPACE namespace
96 :
97 :
98 :
99 :
100 : namespace
101 : {
102 :
103 :
104 :
105 2 : Catch::clara::Parser add_command_line_options(Catch::clara::Parser const & cli)
106 : {
107 : return cli
108 4 : | Catch::clara::Opt(SNAP_CATCH2_NAMESPACE::g_tmp_dir, "tmp")
109 6 : ["-T"]["--tmp"]
110 10 : ("a path to a temporary directory used by the tests.");
111 : }
112 :
113 :
114 1 : int finish_init(Catch::Session & session)
115 : {
116 1 : snap::NOT_USED(session);
117 :
118 1 : if(!SNAP_CATCH2_NAMESPACE::g_tmp_dir.empty())
119 : {
120 1 : if(SNAP_CATCH2_NAMESPACE::g_tmp_dir == "/tmp")
121 : {
122 0 : std::cerr << "fatal error: you must specify a sub-directory for your temporary directory such as /tmp/advgetopt";
123 0 : exit(1);
124 : }
125 : }
126 : else
127 : {
128 0 : SNAP_CATCH2_NAMESPACE::g_tmp_dir = "/tmp/advgetopt";
129 : }
130 :
131 : // delete the existing tmp directory
132 : {
133 2 : std::stringstream ss;
134 1 : ss << "rm -rf \"" << SNAP_CATCH2_NAMESPACE::g_tmp_dir << "\"";
135 1 : if(system(ss.str().c_str()) != 0)
136 : {
137 0 : std::cerr << "fatal error: could not delete temporary directory \"" << SNAP_CATCH2_NAMESPACE::g_tmp_dir << "\".";
138 0 : exit(1);
139 : }
140 : }
141 :
142 : // then re-create the directory
143 : {
144 2 : std::stringstream ss;
145 1 : ss << "mkdir -p \"" << SNAP_CATCH2_NAMESPACE::g_tmp_dir << "\"";
146 1 : if(system(ss.str().c_str()) != 0)
147 : {
148 0 : std::cerr << "fatal error: could not create temporary directory \"" << SNAP_CATCH2_NAMESPACE::g_tmp_dir << "\".";
149 0 : exit(1);
150 : }
151 : }
152 :
153 1 : cppthread::set_log_callback(SNAP_CATCH2_NAMESPACE::log_for_test);
154 :
155 1 : char const * options(getenv("ADVGETOPT_TEST_OPTIONS"));
156 1 : if(options != nullptr
157 0 : && *options != '\0')
158 : {
159 0 : std::cerr << std::endl
160 : << "error:unittest: ADVGETOPT_TEST_OPTIONS already exists,"
161 : " the advgetopt tests would not work as expected with such."
162 0 : " Please unset that environment variable and try again."
163 0 : << std::endl;
164 0 : throw std::runtime_error("ADVGETOPT_TEST_OPTIONS already exists");
165 : }
166 :
167 1 : return 0;
168 : }
169 :
170 :
171 1 : void tests_done()
172 : {
173 1 : SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
174 1 : }
175 :
176 :
177 :
178 : }
179 : // no name namespace
180 :
181 :
182 :
183 :
184 :
185 :
186 2 : int main(int argc, char * argv[])
187 : {
188 4 : return SNAP_CATCH2_NAMESPACE::snap_catch2_main(
189 : "advgetopt"
190 : , LIBADVGETOPT_VERSION_STRING
191 : , argc
192 : , argv
193 6 : , []() { libexcept::set_collect_stack(false); }
194 : , &add_command_line_options
195 : , &finish_init
196 : , &tests_done
197 4 : );
198 6 : }
199 :
200 : // vim: ts=4 sw=4 et
|