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
42 namespace cmdline = utils::cmdline;
43 namespace config = utils::config;
44
45 using cli::cmd_config;
46 using utils::none;
47
48
49 namespace {
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.
58 static config::tree
fake_config(void)59 fake_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
74 ATF_TEST_CASE_WITHOUT_HEAD(all);
ATF_TEST_CASE_BODY(all)75 ATF_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
93 ATF_TEST_CASE_WITHOUT_HEAD(some__ok);
ATF_TEST_CASE_BODY(some__ok)94 ATF_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
112 ATF_TEST_CASE_WITHOUT_HEAD(some__fail);
ATF_TEST_CASE_BODY(some__fail)113 ATF_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
ATF_INIT_TEST_CASES(tcs)136 ATF_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