1// Copyright 2011 Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9//   notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above copyright
11//   notice, this list of conditions and the following disclaimer in the
12//   documentation and/or other materials provided with the distribution.
13// * Neither the name of Google Inc. nor the names of its contributors
14//   may be used to endorse or promote products derived from this software
15//   without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29#include "cli/cmd_config.hpp"
30
31#include <cstdlib>
32
33#include <atf-c++.hpp>
34
35#include "cli/common.ipp"
36#include "engine/config.hpp"
37#include "utils/cmdline/globals.hpp"
38#include "utils/cmdline/ui_mock.hpp"
39#include "utils/config/tree.ipp"
40#include "utils/optional.ipp"
41
42namespace cmdline = utils::cmdline;
43namespace config = utils::config;
44
45using cli::cmd_config;
46using utils::none;
47
48
49namespace {
50
51
52/// Instantiates a fake user configuration for testing purposes.
53///
54/// The user configuration is populated with a collection of test-suite
55/// properties and some hardcoded values for the generic configuration options.
56///
57/// \return A new user configuration object.
58static config::tree
59fake_config(void)
60{
61    config::tree user_config = engine::default_config();
62    user_config.set_string("architecture", "the-architecture");
63    user_config.set_string("platform", "the-platform");
64    //user_config.set_string("unprivileged_user", "");
65    user_config.set_string("test_suites.foo.bar", "first");
66    user_config.set_string("test_suites.foo.baz", "second");
67    return user_config;
68}
69
70
71}  // anonymous namespace
72
73
74ATF_TEST_CASE_WITHOUT_HEAD(all);
75ATF_TEST_CASE_BODY(all)
76{
77    cmdline::args_vector args;
78    args.push_back("config");
79
80    cmd_config cmd;
81    cmdline::ui_mock ui;
82    ATF_REQUIRE_EQ(EXIT_SUCCESS, cmd.main(&ui, args, fake_config()));
83
84    ATF_REQUIRE_EQ(4, ui.out_log().size());
85    ATF_REQUIRE_EQ("architecture = the-architecture", ui.out_log()[0]);
86    ATF_REQUIRE_EQ("platform = the-platform", ui.out_log()[1]);
87    ATF_REQUIRE_EQ("test_suites.foo.bar = first", ui.out_log()[2]);
88    ATF_REQUIRE_EQ("test_suites.foo.baz = second", ui.out_log()[3]);
89    ATF_REQUIRE(ui.err_log().empty());
90}
91
92
93ATF_TEST_CASE_WITHOUT_HEAD(some__ok);
94ATF_TEST_CASE_BODY(some__ok)
95{
96    cmdline::args_vector args;
97    args.push_back("config");
98    args.push_back("platform");
99    args.push_back("test_suites.foo.baz");
100
101    cmd_config cmd;
102    cmdline::ui_mock ui;
103    ATF_REQUIRE_EQ(EXIT_SUCCESS, cmd.main(&ui, args, fake_config()));
104
105    ATF_REQUIRE_EQ(2, ui.out_log().size());
106    ATF_REQUIRE_EQ("platform = the-platform", ui.out_log()[0]);
107    ATF_REQUIRE_EQ("test_suites.foo.baz = second", ui.out_log()[1]);
108    ATF_REQUIRE(ui.err_log().empty());
109}
110
111
112ATF_TEST_CASE_WITHOUT_HEAD(some__fail);
113ATF_TEST_CASE_BODY(some__fail)
114{
115    cmdline::args_vector args;
116    args.push_back("config");
117    args.push_back("platform");
118    args.push_back("unknown");
119    args.push_back("test_suites.foo.baz");
120
121    cmdline::init("progname");
122
123    cmd_config cmd;
124    cmdline::ui_mock ui;
125    ATF_REQUIRE_EQ(EXIT_FAILURE, cmd.main(&ui, args, fake_config()));
126
127    ATF_REQUIRE_EQ(2, ui.out_log().size());
128    ATF_REQUIRE_EQ("platform = the-platform", ui.out_log()[0]);
129    ATF_REQUIRE_EQ("test_suites.foo.baz = second", ui.out_log()[1]);
130    ATF_REQUIRE_EQ(1, ui.err_log().size());
131    ATF_REQUIRE(atf::utils::grep_string("unknown.*not defined",
132                                        ui.err_log()[0]));
133}
134
135
136ATF_INIT_TEST_CASES(tcs)
137{
138    ATF_ADD_TEST_CASE(tcs, all);
139    ATF_ADD_TEST_CASE(tcs, some__ok);
140    ATF_ADD_TEST_CASE(tcs, some__fail);
141}
142