Line data Source code
1 : // Copyright (c) 2019 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/snapdatabase
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 :
28 : // snapdatabase lib
29 : //
30 : #include <snapdatabase/version.h>
31 :
32 :
33 : // libexcept lib
34 : //
35 : #include <libexcept/exception.h>
36 :
37 :
38 : // snaplogger lib
39 : //
40 : #include <snaplogger/logger.h>
41 :
42 :
43 : // snapdev lib
44 : //
45 : #include <snapdev/not_used.h>
46 :
47 :
48 : // C++ lib
49 : //
50 : #include <fstream>
51 :
52 :
53 : // C lib
54 : //
55 : #include <sys/stat.h>
56 : #include <sys/types.h>
57 :
58 :
59 :
60 : namespace SNAP_CATCH2_NAMESPACE
61 : {
62 :
63 :
64 :
65 2 : std::string g_tmp_dir;
66 :
67 :
68 1 : std::string setup_context(std::string const & sub_path, std::vector<std::string> const & xmls)
69 : {
70 2 : std::string path(g_tmp_dir + "/" + sub_path);
71 :
72 1 : if(mkdir(path.c_str(), 0700) != 0)
73 : {
74 0 : if(errno != EEXIST)
75 : {
76 0 : CATCH_REQUIRE(!"could not create context path");
77 0 : return std::string();
78 : }
79 : }
80 :
81 1 : if(mkdir((path + "/tables").c_str(), 0700) != 0)
82 : {
83 0 : if(errno != EEXIST)
84 : {
85 0 : CATCH_REQUIRE(!"could not create table path");
86 0 : return std::string();
87 : }
88 : }
89 :
90 1 : if(mkdir((path + "/database").c_str(), 0700) != 0)
91 : {
92 0 : if(errno != EEXIST)
93 : {
94 0 : CATCH_REQUIRE(!"could not create database path");
95 0 : return std::string();
96 : }
97 : }
98 :
99 2 : for(auto const & x : xmls)
100 : {
101 1 : char const * s(x.c_str());
102 1 : CATCH_REQUIRE((s[0] == '<'
103 : && s[1] == '!'
104 : && s[2] == '-'
105 : && s[3] == '-'
106 : && s[4] == ' '
107 : && s[5] == 'n'
108 : && s[6] == 'a'
109 : && s[7] == 'm'
110 : && s[8] == 'e'
111 : && s[9] == '='));
112 1 : s += 10;
113 1 : char const * e(s);
114 29 : while(*e != ' ')
115 : {
116 14 : ++e;
117 : }
118 2 : std::string const name(s, e - s);
119 2 : std::ofstream o(path + "/tables/" + name + ".xml");
120 1 : o << x;
121 : }
122 :
123 1 : return path;
124 : }
125 :
126 :
127 2 : Catch::clara::Parser add_command_line_options(Catch::clara::Parser const & cli)
128 : {
129 : return cli
130 4 : | Catch::clara::Opt(SNAP_CATCH2_NAMESPACE::g_tmp_dir, "tmp")
131 4 : ["-T"]["--tmp"]
132 4 : ("a path to a temporary directory used by the tests.");
133 : }
134 :
135 :
136 2 : void init_callback()
137 : {
138 2 : libexcept::set_collect_stack(false);
139 2 : }
140 :
141 :
142 1 : int init_tests(Catch::Session & session)
143 : {
144 1 : snap::NOTUSED(session);
145 :
146 1 : if(g_tmp_dir.find_first_of("'\"") != std::string::npos)
147 : {
148 0 : std::cerr << "error: the path to the temporary directory cannot include single (') or double (\") quotes."
149 0 : << std::endl;
150 0 : return 1;
151 : }
152 :
153 2 : std::string cmd("rm -rf \"");
154 1 : cmd += g_tmp_dir;
155 1 : cmd += '"';
156 1 : snap::NOTUSED(system(cmd.c_str()));
157 :
158 1 : if(mkdir(g_tmp_dir.c_str(), 0700) != 0)
159 : {
160 0 : perror(("could not create directory \"" + g_tmp_dir + "\"").c_str());
161 0 : return 1;
162 : }
163 :
164 2 : snaplogger::logger::pointer_t l(snaplogger::logger::get_instance());
165 1 : l->add_console_appender();
166 1 : l->set_severity(snaplogger::severity_t::SEVERITY_ALL);
167 :
168 2 : SNAP_LOG_ERROR
169 1 : << "This is an error through the logger..."
170 : << SNAP_LOG_SEND;
171 :
172 1 : return 0;
173 : }
174 :
175 :
176 : }
177 :
178 :
179 :
180 2 : int main(int argc, char * argv[])
181 : {
182 : return SNAP_CATCH2_NAMESPACE::snap_catch2_main(
183 : "snapdatabase"
184 : , SNAPDATABASE_VERSION_STRING
185 : , argc
186 : , argv
187 : , SNAP_CATCH2_NAMESPACE::init_callback
188 : , SNAP_CATCH2_NAMESPACE::add_command_line_options
189 : , SNAP_CATCH2_NAMESPACE::init_tests
190 2 : );
191 6 : }
192 :
193 :
194 : // vim: ts=4 sw=4 et
|