1*b0d29bc4SBrooks Davis // Copyright 2011 The Kyua Authors.
2*b0d29bc4SBrooks Davis // All rights reserved.
3*b0d29bc4SBrooks Davis //
4*b0d29bc4SBrooks Davis // Redistribution and use in source and binary forms, with or without
5*b0d29bc4SBrooks Davis // modification, are permitted provided that the following conditions are
6*b0d29bc4SBrooks Davis // met:
7*b0d29bc4SBrooks Davis //
8*b0d29bc4SBrooks Davis // * Redistributions of source code must retain the above copyright
9*b0d29bc4SBrooks Davis // notice, this list of conditions and the following disclaimer.
10*b0d29bc4SBrooks Davis // * Redistributions in binary form must reproduce the above copyright
11*b0d29bc4SBrooks Davis // notice, this list of conditions and the following disclaimer in the
12*b0d29bc4SBrooks Davis // documentation and/or other materials provided with the distribution.
13*b0d29bc4SBrooks Davis // * Neither the name of Google Inc. nor the names of its contributors
14*b0d29bc4SBrooks Davis // may be used to endorse or promote products derived from this software
15*b0d29bc4SBrooks Davis // without specific prior written permission.
16*b0d29bc4SBrooks Davis //
17*b0d29bc4SBrooks Davis // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*b0d29bc4SBrooks Davis // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*b0d29bc4SBrooks Davis // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*b0d29bc4SBrooks Davis // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*b0d29bc4SBrooks Davis // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*b0d29bc4SBrooks Davis // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*b0d29bc4SBrooks Davis // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*b0d29bc4SBrooks Davis // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*b0d29bc4SBrooks Davis // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*b0d29bc4SBrooks Davis // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*b0d29bc4SBrooks Davis // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*b0d29bc4SBrooks Davis
29*b0d29bc4SBrooks Davis #include "cli/common.hpp"
30*b0d29bc4SBrooks Davis
31*b0d29bc4SBrooks Davis #include <fstream>
32*b0d29bc4SBrooks Davis
33*b0d29bc4SBrooks Davis #include <atf-c++.hpp>
34*b0d29bc4SBrooks Davis
35*b0d29bc4SBrooks Davis #include "engine/exceptions.hpp"
36*b0d29bc4SBrooks Davis #include "engine/filters.hpp"
37*b0d29bc4SBrooks Davis #include "model/metadata.hpp"
38*b0d29bc4SBrooks Davis #include "model/test_program.hpp"
39*b0d29bc4SBrooks Davis #include "model/test_result.hpp"
40*b0d29bc4SBrooks Davis #include "store/layout.hpp"
41*b0d29bc4SBrooks Davis #include "utils/cmdline/exceptions.hpp"
42*b0d29bc4SBrooks Davis #include "utils/cmdline/globals.hpp"
43*b0d29bc4SBrooks Davis #include "utils/cmdline/options.hpp"
44*b0d29bc4SBrooks Davis #include "utils/cmdline/parser.ipp"
45*b0d29bc4SBrooks Davis #include "utils/cmdline/ui_mock.hpp"
46*b0d29bc4SBrooks Davis #include "utils/datetime.hpp"
47*b0d29bc4SBrooks Davis #include "utils/env.hpp"
48*b0d29bc4SBrooks Davis #include "utils/format/macros.hpp"
49*b0d29bc4SBrooks Davis #include "utils/fs/exceptions.hpp"
50*b0d29bc4SBrooks Davis #include "utils/fs/operations.hpp"
51*b0d29bc4SBrooks Davis #include "utils/fs/path.hpp"
52*b0d29bc4SBrooks Davis #include "utils/optional.ipp"
53*b0d29bc4SBrooks Davis #include "utils/sanity.hpp"
54*b0d29bc4SBrooks Davis
55*b0d29bc4SBrooks Davis namespace cmdline = utils::cmdline;
56*b0d29bc4SBrooks Davis namespace config = utils::config;
57*b0d29bc4SBrooks Davis namespace datetime = utils::datetime;
58*b0d29bc4SBrooks Davis namespace fs = utils::fs;
59*b0d29bc4SBrooks Davis namespace layout = store::layout;
60*b0d29bc4SBrooks Davis
61*b0d29bc4SBrooks Davis using utils::optional;
62*b0d29bc4SBrooks Davis
63*b0d29bc4SBrooks Davis
64*b0d29bc4SBrooks Davis namespace {
65*b0d29bc4SBrooks Davis
66*b0d29bc4SBrooks Davis
67*b0d29bc4SBrooks Davis /// Syntactic sugar to instantiate engine::test_filter objects.
68*b0d29bc4SBrooks Davis ///
69*b0d29bc4SBrooks Davis /// \param test_program Test program.
70*b0d29bc4SBrooks Davis /// \param test_case Test case.
71*b0d29bc4SBrooks Davis ///
72*b0d29bc4SBrooks Davis /// \return A \code test_filter \endcode object, based on \p test_program and
73*b0d29bc4SBrooks Davis /// \p test_case.
74*b0d29bc4SBrooks Davis inline engine::test_filter
mkfilter(const char * test_program,const char * test_case)75*b0d29bc4SBrooks Davis mkfilter(const char* test_program, const char* test_case)
76*b0d29bc4SBrooks Davis {
77*b0d29bc4SBrooks Davis return engine::test_filter(fs::path(test_program), test_case);
78*b0d29bc4SBrooks Davis }
79*b0d29bc4SBrooks Davis
80*b0d29bc4SBrooks Davis
81*b0d29bc4SBrooks Davis } // anonymous namespace
82*b0d29bc4SBrooks Davis
83*b0d29bc4SBrooks Davis
84*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(build_root_path__default);
ATF_TEST_CASE_BODY(build_root_path__default)85*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(build_root_path__default)
86*b0d29bc4SBrooks Davis {
87*b0d29bc4SBrooks Davis std::map< std::string, std::vector< std::string > > options;
88*b0d29bc4SBrooks Davis const cmdline::parsed_cmdline mock_cmdline(options, cmdline::args_vector());
89*b0d29bc4SBrooks Davis
90*b0d29bc4SBrooks Davis ATF_REQUIRE(!cli::build_root_path(mock_cmdline));
91*b0d29bc4SBrooks Davis }
92*b0d29bc4SBrooks Davis
93*b0d29bc4SBrooks Davis
94*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(build_root_path__explicit);
ATF_TEST_CASE_BODY(build_root_path__explicit)95*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(build_root_path__explicit)
96*b0d29bc4SBrooks Davis {
97*b0d29bc4SBrooks Davis std::map< std::string, std::vector< std::string > > options;
98*b0d29bc4SBrooks Davis options["build-root"].push_back("/my//path");
99*b0d29bc4SBrooks Davis const cmdline::parsed_cmdline mock_cmdline(options, cmdline::args_vector());
100*b0d29bc4SBrooks Davis
101*b0d29bc4SBrooks Davis ATF_REQUIRE(cli::build_root_path(mock_cmdline));
102*b0d29bc4SBrooks Davis ATF_REQUIRE_EQ("/my/path", cli::build_root_path(mock_cmdline).get().str());
103*b0d29bc4SBrooks Davis }
104*b0d29bc4SBrooks Davis
105*b0d29bc4SBrooks Davis
106*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(kyuafile_path__default);
ATF_TEST_CASE_BODY(kyuafile_path__default)107*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(kyuafile_path__default)
108*b0d29bc4SBrooks Davis {
109*b0d29bc4SBrooks Davis std::map< std::string, std::vector< std::string > > options;
110*b0d29bc4SBrooks Davis options["kyuafile"].push_back(cli::kyuafile_option.default_value());
111*b0d29bc4SBrooks Davis const cmdline::parsed_cmdline mock_cmdline(options, cmdline::args_vector());
112*b0d29bc4SBrooks Davis
113*b0d29bc4SBrooks Davis ATF_REQUIRE_EQ(cli::kyuafile_option.default_value(),
114*b0d29bc4SBrooks Davis cli::kyuafile_path(mock_cmdline).str());
115*b0d29bc4SBrooks Davis }
116*b0d29bc4SBrooks Davis
117*b0d29bc4SBrooks Davis
118*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(kyuafile_path__explicit);
ATF_TEST_CASE_BODY(kyuafile_path__explicit)119*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(kyuafile_path__explicit)
120*b0d29bc4SBrooks Davis {
121*b0d29bc4SBrooks Davis std::map< std::string, std::vector< std::string > > options;
122*b0d29bc4SBrooks Davis options["kyuafile"].push_back("/my//path");
123*b0d29bc4SBrooks Davis const cmdline::parsed_cmdline mock_cmdline(options, cmdline::args_vector());
124*b0d29bc4SBrooks Davis
125*b0d29bc4SBrooks Davis ATF_REQUIRE_EQ("/my/path", cli::kyuafile_path(mock_cmdline).str());
126*b0d29bc4SBrooks Davis }
127*b0d29bc4SBrooks Davis
128*b0d29bc4SBrooks Davis
129*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(result_types__default);
ATF_TEST_CASE_BODY(result_types__default)130*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(result_types__default)
131*b0d29bc4SBrooks Davis {
132*b0d29bc4SBrooks Davis std::map< std::string, std::vector< std::string > > options;
133*b0d29bc4SBrooks Davis options["results-filter"].push_back(
134*b0d29bc4SBrooks Davis cli::results_filter_option.default_value());
135*b0d29bc4SBrooks Davis const cmdline::parsed_cmdline mock_cmdline(options, cmdline::args_vector());
136*b0d29bc4SBrooks Davis
137*b0d29bc4SBrooks Davis cli::result_types exp_types;
138*b0d29bc4SBrooks Davis exp_types.push_back(model::test_result_skipped);
139*b0d29bc4SBrooks Davis exp_types.push_back(model::test_result_expected_failure);
140*b0d29bc4SBrooks Davis exp_types.push_back(model::test_result_broken);
141*b0d29bc4SBrooks Davis exp_types.push_back(model::test_result_failed);
142*b0d29bc4SBrooks Davis ATF_REQUIRE(exp_types == cli::get_result_types(mock_cmdline));
143*b0d29bc4SBrooks Davis }
144*b0d29bc4SBrooks Davis
145*b0d29bc4SBrooks Davis
146*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(result_types__empty);
ATF_TEST_CASE_BODY(result_types__empty)147*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(result_types__empty)
148*b0d29bc4SBrooks Davis {
149*b0d29bc4SBrooks Davis std::map< std::string, std::vector< std::string > > options;
150*b0d29bc4SBrooks Davis options["results-filter"].push_back("");
151*b0d29bc4SBrooks Davis const cmdline::parsed_cmdline mock_cmdline(options, cmdline::args_vector());
152*b0d29bc4SBrooks Davis
153*b0d29bc4SBrooks Davis cli::result_types exp_types;
154*b0d29bc4SBrooks Davis exp_types.push_back(model::test_result_passed);
155*b0d29bc4SBrooks Davis exp_types.push_back(model::test_result_skipped);
156*b0d29bc4SBrooks Davis exp_types.push_back(model::test_result_expected_failure);
157*b0d29bc4SBrooks Davis exp_types.push_back(model::test_result_broken);
158*b0d29bc4SBrooks Davis exp_types.push_back(model::test_result_failed);
159*b0d29bc4SBrooks Davis ATF_REQUIRE(exp_types == cli::get_result_types(mock_cmdline));
160*b0d29bc4SBrooks Davis }
161*b0d29bc4SBrooks Davis
162*b0d29bc4SBrooks Davis
163*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(result_types__explicit__all);
ATF_TEST_CASE_BODY(result_types__explicit__all)164*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(result_types__explicit__all)
165*b0d29bc4SBrooks Davis {
166*b0d29bc4SBrooks Davis std::map< std::string, std::vector< std::string > > options;
167*b0d29bc4SBrooks Davis options["results-filter"].push_back("passed,skipped,xfail,broken,failed");
168*b0d29bc4SBrooks Davis const cmdline::parsed_cmdline mock_cmdline(options, cmdline::args_vector());
169*b0d29bc4SBrooks Davis
170*b0d29bc4SBrooks Davis cli::result_types exp_types;
171*b0d29bc4SBrooks Davis exp_types.push_back(model::test_result_passed);
172*b0d29bc4SBrooks Davis exp_types.push_back(model::test_result_skipped);
173*b0d29bc4SBrooks Davis exp_types.push_back(model::test_result_expected_failure);
174*b0d29bc4SBrooks Davis exp_types.push_back(model::test_result_broken);
175*b0d29bc4SBrooks Davis exp_types.push_back(model::test_result_failed);
176*b0d29bc4SBrooks Davis ATF_REQUIRE(exp_types == cli::get_result_types(mock_cmdline));
177*b0d29bc4SBrooks Davis }
178*b0d29bc4SBrooks Davis
179*b0d29bc4SBrooks Davis
180*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(result_types__explicit__some);
ATF_TEST_CASE_BODY(result_types__explicit__some)181*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(result_types__explicit__some)
182*b0d29bc4SBrooks Davis {
183*b0d29bc4SBrooks Davis std::map< std::string, std::vector< std::string > > options;
184*b0d29bc4SBrooks Davis options["results-filter"].push_back("skipped,broken");
185*b0d29bc4SBrooks Davis const cmdline::parsed_cmdline mock_cmdline(options, cmdline::args_vector());
186*b0d29bc4SBrooks Davis
187*b0d29bc4SBrooks Davis cli::result_types exp_types;
188*b0d29bc4SBrooks Davis exp_types.push_back(model::test_result_skipped);
189*b0d29bc4SBrooks Davis exp_types.push_back(model::test_result_broken);
190*b0d29bc4SBrooks Davis ATF_REQUIRE(exp_types == cli::get_result_types(mock_cmdline));
191*b0d29bc4SBrooks Davis }
192*b0d29bc4SBrooks Davis
193*b0d29bc4SBrooks Davis
194*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(result_types__explicit__invalid);
ATF_TEST_CASE_BODY(result_types__explicit__invalid)195*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(result_types__explicit__invalid)
196*b0d29bc4SBrooks Davis {
197*b0d29bc4SBrooks Davis std::map< std::string, std::vector< std::string > > options;
198*b0d29bc4SBrooks Davis options["results-filter"].push_back("skipped,foo,broken");
199*b0d29bc4SBrooks Davis const cmdline::parsed_cmdline mock_cmdline(options, cmdline::args_vector());
200*b0d29bc4SBrooks Davis
201*b0d29bc4SBrooks Davis ATF_REQUIRE_THROW_RE(std::runtime_error, "Unknown result type 'foo'",
202*b0d29bc4SBrooks Davis cli::get_result_types(mock_cmdline));
203*b0d29bc4SBrooks Davis }
204*b0d29bc4SBrooks Davis
205*b0d29bc4SBrooks Davis
206*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(results_file_create__default__new);
ATF_TEST_CASE_BODY(results_file_create__default__new)207*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(results_file_create__default__new)
208*b0d29bc4SBrooks Davis {
209*b0d29bc4SBrooks Davis std::map< std::string, std::vector< std::string > > options;
210*b0d29bc4SBrooks Davis options["results-file"].push_back(
211*b0d29bc4SBrooks Davis cli::results_file_create_option.default_value());
212*b0d29bc4SBrooks Davis const cmdline::parsed_cmdline mock_cmdline(options, cmdline::args_vector());
213*b0d29bc4SBrooks Davis
214*b0d29bc4SBrooks Davis const fs::path home("homedir");
215*b0d29bc4SBrooks Davis utils::setenv("HOME", home.str());
216*b0d29bc4SBrooks Davis
217*b0d29bc4SBrooks Davis ATF_REQUIRE_EQ(cli::results_file_create_option.default_value(),
218*b0d29bc4SBrooks Davis cli::results_file_create(mock_cmdline));
219*b0d29bc4SBrooks Davis ATF_REQUIRE(!fs::exists(home / ".kyua"));
220*b0d29bc4SBrooks Davis }
221*b0d29bc4SBrooks Davis
222*b0d29bc4SBrooks Davis
223*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(results_file_create__default__historical);
ATF_TEST_CASE_BODY(results_file_create__default__historical)224*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(results_file_create__default__historical)
225*b0d29bc4SBrooks Davis {
226*b0d29bc4SBrooks Davis std::map< std::string, std::vector< std::string > > options;
227*b0d29bc4SBrooks Davis options["results-file"].push_back(
228*b0d29bc4SBrooks Davis cli::results_file_create_option.default_value());
229*b0d29bc4SBrooks Davis const cmdline::parsed_cmdline mock_cmdline(options, cmdline::args_vector());
230*b0d29bc4SBrooks Davis
231*b0d29bc4SBrooks Davis const fs::path home("homedir");
232*b0d29bc4SBrooks Davis utils::setenv("HOME", home.str());
233*b0d29bc4SBrooks Davis fs::mkdir_p(fs::path("homedir/.kyua"), 0755);
234*b0d29bc4SBrooks Davis atf::utils::create_file("homedir/.kyua/store.db", "fake store");
235*b0d29bc4SBrooks Davis
236*b0d29bc4SBrooks Davis ATF_REQUIRE_EQ(fs::path("homedir/.kyua/store.db").to_absolute(),
237*b0d29bc4SBrooks Davis fs::path(cli::results_file_create(mock_cmdline)));
238*b0d29bc4SBrooks Davis }
239*b0d29bc4SBrooks Davis
240*b0d29bc4SBrooks Davis
241*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(results_file_create__explicit);
ATF_TEST_CASE_BODY(results_file_create__explicit)242*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(results_file_create__explicit)
243*b0d29bc4SBrooks Davis {
244*b0d29bc4SBrooks Davis std::map< std::string, std::vector< std::string > > options;
245*b0d29bc4SBrooks Davis options["results-file"].push_back("/my//path/f.db");
246*b0d29bc4SBrooks Davis const cmdline::parsed_cmdline mock_cmdline(options, cmdline::args_vector());
247*b0d29bc4SBrooks Davis
248*b0d29bc4SBrooks Davis ATF_REQUIRE_EQ("/my//path/f.db",
249*b0d29bc4SBrooks Davis cli::results_file_create(mock_cmdline));
250*b0d29bc4SBrooks Davis }
251*b0d29bc4SBrooks Davis
252*b0d29bc4SBrooks Davis
253*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(results_file_open__default__latest);
ATF_TEST_CASE_BODY(results_file_open__default__latest)254*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(results_file_open__default__latest)
255*b0d29bc4SBrooks Davis {
256*b0d29bc4SBrooks Davis std::map< std::string, std::vector< std::string > > options;
257*b0d29bc4SBrooks Davis options["results-file"].push_back(
258*b0d29bc4SBrooks Davis cli::results_file_open_option.default_value());
259*b0d29bc4SBrooks Davis const cmdline::parsed_cmdline mock_cmdline(options, cmdline::args_vector());
260*b0d29bc4SBrooks Davis
261*b0d29bc4SBrooks Davis const fs::path home("homedir");
262*b0d29bc4SBrooks Davis utils::setenv("HOME", home.str());
263*b0d29bc4SBrooks Davis
264*b0d29bc4SBrooks Davis ATF_REQUIRE_EQ(cli::results_file_open_option.default_value(),
265*b0d29bc4SBrooks Davis cli::results_file_open(mock_cmdline));
266*b0d29bc4SBrooks Davis ATF_REQUIRE(!fs::exists(home / ".kyua"));
267*b0d29bc4SBrooks Davis }
268*b0d29bc4SBrooks Davis
269*b0d29bc4SBrooks Davis
270*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(results_file_open__default__historical);
ATF_TEST_CASE_BODY(results_file_open__default__historical)271*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(results_file_open__default__historical)
272*b0d29bc4SBrooks Davis {
273*b0d29bc4SBrooks Davis std::map< std::string, std::vector< std::string > > options;
274*b0d29bc4SBrooks Davis options["results-file"].push_back(
275*b0d29bc4SBrooks Davis cli::results_file_open_option.default_value());
276*b0d29bc4SBrooks Davis const cmdline::parsed_cmdline mock_cmdline(options, cmdline::args_vector());
277*b0d29bc4SBrooks Davis
278*b0d29bc4SBrooks Davis const fs::path home("homedir");
279*b0d29bc4SBrooks Davis utils::setenv("HOME", home.str());
280*b0d29bc4SBrooks Davis fs::mkdir_p(fs::path("homedir/.kyua"), 0755);
281*b0d29bc4SBrooks Davis atf::utils::create_file("homedir/.kyua/store.db", "fake store");
282*b0d29bc4SBrooks Davis
283*b0d29bc4SBrooks Davis ATF_REQUIRE_EQ(fs::path("homedir/.kyua/store.db").to_absolute(),
284*b0d29bc4SBrooks Davis fs::path(cli::results_file_open(mock_cmdline)));
285*b0d29bc4SBrooks Davis }
286*b0d29bc4SBrooks Davis
287*b0d29bc4SBrooks Davis
288*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(results_file_open__explicit);
ATF_TEST_CASE_BODY(results_file_open__explicit)289*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(results_file_open__explicit)
290*b0d29bc4SBrooks Davis {
291*b0d29bc4SBrooks Davis std::map< std::string, std::vector< std::string > > options;
292*b0d29bc4SBrooks Davis options["results-file"].push_back("/my//path/f.db");
293*b0d29bc4SBrooks Davis const cmdline::parsed_cmdline mock_cmdline(options, cmdline::args_vector());
294*b0d29bc4SBrooks Davis
295*b0d29bc4SBrooks Davis ATF_REQUIRE_EQ("/my//path/f.db", cli::results_file_open(mock_cmdline));
296*b0d29bc4SBrooks Davis }
297*b0d29bc4SBrooks Davis
298*b0d29bc4SBrooks Davis
299*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(parse_filters__none);
ATF_TEST_CASE_BODY(parse_filters__none)300*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(parse_filters__none)
301*b0d29bc4SBrooks Davis {
302*b0d29bc4SBrooks Davis const cmdline::args_vector args;
303*b0d29bc4SBrooks Davis const std::set< engine::test_filter > filters = cli::parse_filters(args);
304*b0d29bc4SBrooks Davis ATF_REQUIRE(filters.empty());
305*b0d29bc4SBrooks Davis }
306*b0d29bc4SBrooks Davis
307*b0d29bc4SBrooks Davis
308*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(parse_filters__ok);
ATF_TEST_CASE_BODY(parse_filters__ok)309*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(parse_filters__ok)
310*b0d29bc4SBrooks Davis {
311*b0d29bc4SBrooks Davis cmdline::args_vector args;
312*b0d29bc4SBrooks Davis args.push_back("foo");
313*b0d29bc4SBrooks Davis args.push_back("bar/baz");
314*b0d29bc4SBrooks Davis args.push_back("other:abc");
315*b0d29bc4SBrooks Davis args.push_back("other:bcd");
316*b0d29bc4SBrooks Davis const std::set< engine::test_filter > filters = cli::parse_filters(args);
317*b0d29bc4SBrooks Davis
318*b0d29bc4SBrooks Davis std::set< engine::test_filter > exp_filters;
319*b0d29bc4SBrooks Davis exp_filters.insert(mkfilter("foo", ""));
320*b0d29bc4SBrooks Davis exp_filters.insert(mkfilter("bar/baz", ""));
321*b0d29bc4SBrooks Davis exp_filters.insert(mkfilter("other", "abc"));
322*b0d29bc4SBrooks Davis exp_filters.insert(mkfilter("other", "bcd"));
323*b0d29bc4SBrooks Davis
324*b0d29bc4SBrooks Davis ATF_REQUIRE(exp_filters == filters);
325*b0d29bc4SBrooks Davis }
326*b0d29bc4SBrooks Davis
327*b0d29bc4SBrooks Davis
328*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(parse_filters__duplicate);
ATF_TEST_CASE_BODY(parse_filters__duplicate)329*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(parse_filters__duplicate)
330*b0d29bc4SBrooks Davis {
331*b0d29bc4SBrooks Davis cmdline::args_vector args;
332*b0d29bc4SBrooks Davis args.push_back("foo/bar//baz");
333*b0d29bc4SBrooks Davis args.push_back("hello/world:yes");
334*b0d29bc4SBrooks Davis args.push_back("foo//bar/baz");
335*b0d29bc4SBrooks Davis ATF_REQUIRE_THROW_RE(cmdline::error, "Duplicate.*'foo/bar/baz'",
336*b0d29bc4SBrooks Davis cli::parse_filters(args));
337*b0d29bc4SBrooks Davis }
338*b0d29bc4SBrooks Davis
339*b0d29bc4SBrooks Davis
340*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(parse_filters__nondisjoint);
ATF_TEST_CASE_BODY(parse_filters__nondisjoint)341*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(parse_filters__nondisjoint)
342*b0d29bc4SBrooks Davis {
343*b0d29bc4SBrooks Davis cmdline::args_vector args;
344*b0d29bc4SBrooks Davis args.push_back("foo/bar");
345*b0d29bc4SBrooks Davis args.push_back("hello/world:yes");
346*b0d29bc4SBrooks Davis args.push_back("foo/bar:baz");
347*b0d29bc4SBrooks Davis ATF_REQUIRE_THROW_RE(cmdline::error, "'foo/bar'.*'foo/bar:baz'.*disjoint",
348*b0d29bc4SBrooks Davis cli::parse_filters(args));
349*b0d29bc4SBrooks Davis }
350*b0d29bc4SBrooks Davis
351*b0d29bc4SBrooks Davis
352*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(report_unused_filters__none);
ATF_TEST_CASE_BODY(report_unused_filters__none)353*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(report_unused_filters__none)
354*b0d29bc4SBrooks Davis {
355*b0d29bc4SBrooks Davis std::set< engine::test_filter > unused;
356*b0d29bc4SBrooks Davis
357*b0d29bc4SBrooks Davis cmdline::ui_mock ui;
358*b0d29bc4SBrooks Davis ATF_REQUIRE(!cli::report_unused_filters(unused, &ui));
359*b0d29bc4SBrooks Davis ATF_REQUIRE(ui.out_log().empty());
360*b0d29bc4SBrooks Davis ATF_REQUIRE(ui.err_log().empty());
361*b0d29bc4SBrooks Davis }
362*b0d29bc4SBrooks Davis
363*b0d29bc4SBrooks Davis
364*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(report_unused_filters__some);
ATF_TEST_CASE_BODY(report_unused_filters__some)365*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(report_unused_filters__some)
366*b0d29bc4SBrooks Davis {
367*b0d29bc4SBrooks Davis std::set< engine::test_filter > unused;
368*b0d29bc4SBrooks Davis unused.insert(mkfilter("a/b", ""));
369*b0d29bc4SBrooks Davis unused.insert(mkfilter("hey/d", "yes"));
370*b0d29bc4SBrooks Davis
371*b0d29bc4SBrooks Davis cmdline::ui_mock ui;
372*b0d29bc4SBrooks Davis cmdline::init("progname");
373*b0d29bc4SBrooks Davis ATF_REQUIRE(cli::report_unused_filters(unused, &ui));
374*b0d29bc4SBrooks Davis ATF_REQUIRE(ui.out_log().empty());
375*b0d29bc4SBrooks Davis ATF_REQUIRE_EQ(2, ui.err_log().size());
376*b0d29bc4SBrooks Davis ATF_REQUIRE( atf::utils::grep_collection("No.*matched.*'a/b'",
377*b0d29bc4SBrooks Davis ui.err_log()));
378*b0d29bc4SBrooks Davis ATF_REQUIRE( atf::utils::grep_collection("No.*matched.*'hey/d:yes'",
379*b0d29bc4SBrooks Davis ui.err_log()));
380*b0d29bc4SBrooks Davis }
381*b0d29bc4SBrooks Davis
382*b0d29bc4SBrooks Davis
383*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(format_delta);
ATF_TEST_CASE_BODY(format_delta)384*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(format_delta)
385*b0d29bc4SBrooks Davis {
386*b0d29bc4SBrooks Davis ATF_REQUIRE_EQ("0.000s", cli::format_delta(datetime::delta()));
387*b0d29bc4SBrooks Davis ATF_REQUIRE_EQ("0.012s", cli::format_delta(datetime::delta(0, 12300)));
388*b0d29bc4SBrooks Davis ATF_REQUIRE_EQ("0.999s", cli::format_delta(datetime::delta(0, 999000)));
389*b0d29bc4SBrooks Davis ATF_REQUIRE_EQ("51.321s", cli::format_delta(datetime::delta(51, 321000)));
390*b0d29bc4SBrooks Davis }
391*b0d29bc4SBrooks Davis
392*b0d29bc4SBrooks Davis
393*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(format_result__no_reason);
ATF_TEST_CASE_BODY(format_result__no_reason)394*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(format_result__no_reason)
395*b0d29bc4SBrooks Davis {
396*b0d29bc4SBrooks Davis ATF_REQUIRE_EQ("passed", cli::format_result(
397*b0d29bc4SBrooks Davis model::test_result(model::test_result_passed)));
398*b0d29bc4SBrooks Davis ATF_REQUIRE_EQ("failed", cli::format_result(
399*b0d29bc4SBrooks Davis model::test_result(model::test_result_failed)));
400*b0d29bc4SBrooks Davis }
401*b0d29bc4SBrooks Davis
402*b0d29bc4SBrooks Davis
403*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(format_result__with_reason);
ATF_TEST_CASE_BODY(format_result__with_reason)404*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(format_result__with_reason)
405*b0d29bc4SBrooks Davis {
406*b0d29bc4SBrooks Davis ATF_REQUIRE_EQ("broken: Something", cli::format_result(
407*b0d29bc4SBrooks Davis model::test_result(model::test_result_broken, "Something")));
408*b0d29bc4SBrooks Davis ATF_REQUIRE_EQ("expected_failure: A B C", cli::format_result(
409*b0d29bc4SBrooks Davis model::test_result(model::test_result_expected_failure, "A B C")));
410*b0d29bc4SBrooks Davis ATF_REQUIRE_EQ("failed: More text", cli::format_result(
411*b0d29bc4SBrooks Davis model::test_result(model::test_result_failed, "More text")));
412*b0d29bc4SBrooks Davis ATF_REQUIRE_EQ("skipped: Bye", cli::format_result(
413*b0d29bc4SBrooks Davis model::test_result(model::test_result_skipped, "Bye")));
414*b0d29bc4SBrooks Davis }
415*b0d29bc4SBrooks Davis
416*b0d29bc4SBrooks Davis
417*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(format_test_case_id__test_case);
ATF_TEST_CASE_BODY(format_test_case_id__test_case)418*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(format_test_case_id__test_case)
419*b0d29bc4SBrooks Davis {
420*b0d29bc4SBrooks Davis const model::test_program test_program = model::test_program_builder(
421*b0d29bc4SBrooks Davis "mock", fs::path("foo/bar/baz"), fs::path("unused-root"),
422*b0d29bc4SBrooks Davis "unused-suite-name")
423*b0d29bc4SBrooks Davis .add_test_case("abc")
424*b0d29bc4SBrooks Davis .build();
425*b0d29bc4SBrooks Davis ATF_REQUIRE_EQ("foo/bar/baz:abc",
426*b0d29bc4SBrooks Davis cli::format_test_case_id(test_program, "abc"));
427*b0d29bc4SBrooks Davis }
428*b0d29bc4SBrooks Davis
429*b0d29bc4SBrooks Davis
430*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(format_test_case_id__test_filter);
ATF_TEST_CASE_BODY(format_test_case_id__test_filter)431*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(format_test_case_id__test_filter)
432*b0d29bc4SBrooks Davis {
433*b0d29bc4SBrooks Davis const engine::test_filter filter(fs::path("foo/bar"), "baz");
434*b0d29bc4SBrooks Davis ATF_REQUIRE_EQ("foo/bar:baz", cli::format_test_case_id(filter));
435*b0d29bc4SBrooks Davis }
436*b0d29bc4SBrooks Davis
437*b0d29bc4SBrooks Davis
438*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(write_version_header);
ATF_TEST_CASE_BODY(write_version_header)439*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(write_version_header)
440*b0d29bc4SBrooks Davis {
441*b0d29bc4SBrooks Davis cmdline::ui_mock ui;
442*b0d29bc4SBrooks Davis cli::write_version_header(&ui);
443*b0d29bc4SBrooks Davis ATF_REQUIRE_EQ(1, ui.out_log().size());
444*b0d29bc4SBrooks Davis ATF_REQUIRE_MATCH("^kyua .*[0-9]+\\.[0-9]+$", ui.out_log()[0]);
445*b0d29bc4SBrooks Davis ATF_REQUIRE(ui.err_log().empty());
446*b0d29bc4SBrooks Davis }
447*b0d29bc4SBrooks Davis
448*b0d29bc4SBrooks Davis
ATF_INIT_TEST_CASES(tcs)449*b0d29bc4SBrooks Davis ATF_INIT_TEST_CASES(tcs)
450*b0d29bc4SBrooks Davis {
451*b0d29bc4SBrooks Davis ATF_ADD_TEST_CASE(tcs, build_root_path__default);
452*b0d29bc4SBrooks Davis ATF_ADD_TEST_CASE(tcs, build_root_path__explicit);
453*b0d29bc4SBrooks Davis
454*b0d29bc4SBrooks Davis ATF_ADD_TEST_CASE(tcs, kyuafile_path__default);
455*b0d29bc4SBrooks Davis ATF_ADD_TEST_CASE(tcs, kyuafile_path__explicit);
456*b0d29bc4SBrooks Davis
457*b0d29bc4SBrooks Davis ATF_ADD_TEST_CASE(tcs, result_types__default);
458*b0d29bc4SBrooks Davis ATF_ADD_TEST_CASE(tcs, result_types__empty);
459*b0d29bc4SBrooks Davis ATF_ADD_TEST_CASE(tcs, result_types__explicit__all);
460*b0d29bc4SBrooks Davis ATF_ADD_TEST_CASE(tcs, result_types__explicit__some);
461*b0d29bc4SBrooks Davis ATF_ADD_TEST_CASE(tcs, result_types__explicit__invalid);
462*b0d29bc4SBrooks Davis
463*b0d29bc4SBrooks Davis ATF_ADD_TEST_CASE(tcs, results_file_create__default__new);
464*b0d29bc4SBrooks Davis ATF_ADD_TEST_CASE(tcs, results_file_create__default__historical);
465*b0d29bc4SBrooks Davis ATF_ADD_TEST_CASE(tcs, results_file_create__explicit);
466*b0d29bc4SBrooks Davis
467*b0d29bc4SBrooks Davis ATF_ADD_TEST_CASE(tcs, results_file_open__default__latest);
468*b0d29bc4SBrooks Davis ATF_ADD_TEST_CASE(tcs, results_file_open__default__historical);
469*b0d29bc4SBrooks Davis ATF_ADD_TEST_CASE(tcs, results_file_open__explicit);
470*b0d29bc4SBrooks Davis
471*b0d29bc4SBrooks Davis ATF_ADD_TEST_CASE(tcs, parse_filters__none);
472*b0d29bc4SBrooks Davis ATF_ADD_TEST_CASE(tcs, parse_filters__ok);
473*b0d29bc4SBrooks Davis ATF_ADD_TEST_CASE(tcs, parse_filters__duplicate);
474*b0d29bc4SBrooks Davis ATF_ADD_TEST_CASE(tcs, parse_filters__nondisjoint);
475*b0d29bc4SBrooks Davis
476*b0d29bc4SBrooks Davis ATF_ADD_TEST_CASE(tcs, report_unused_filters__none);
477*b0d29bc4SBrooks Davis ATF_ADD_TEST_CASE(tcs, report_unused_filters__some);
478*b0d29bc4SBrooks Davis
479*b0d29bc4SBrooks Davis ATF_ADD_TEST_CASE(tcs, format_delta);
480*b0d29bc4SBrooks Davis
481*b0d29bc4SBrooks Davis ATF_ADD_TEST_CASE(tcs, format_result__no_reason);
482*b0d29bc4SBrooks Davis ATF_ADD_TEST_CASE(tcs, format_result__with_reason);
483*b0d29bc4SBrooks Davis
484*b0d29bc4SBrooks Davis ATF_ADD_TEST_CASE(tcs, format_test_case_id__test_case);
485*b0d29bc4SBrooks Davis ATF_ADD_TEST_CASE(tcs, format_test_case_id__test_filter);
486*b0d29bc4SBrooks Davis
487*b0d29bc4SBrooks Davis ATF_ADD_TEST_CASE(tcs, write_version_header);
488*b0d29bc4SBrooks Davis }
489