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