Add none support for testing.
[awesomized/libmemcached] / libtest / cmdline.h
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * libtest
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 3 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #pragma once
23
24 #include <spawn.h>
25
26 namespace libtest {
27
28 class Application {
29 private:
30 typedef std::vector< std::pair<std::string, std::string> > Options;
31
32 public:
33
34 enum error_t {
35 SUCCESS= EXIT_SUCCESS,
36 FAILURE= EXIT_FAILURE,
37 INVALID= 127
38 };
39
40 class Pipe {
41 public:
42 Pipe(int);
43 ~Pipe();
44
45 int fd();
46
47 enum close_t {
48 READ= 0,
49 WRITE= 1
50 };
51
52 void reset();
53 void close(const close_t& arg);
54 void dup_for_spawn(const close_t& arg,
55 posix_spawn_file_actions_t& file_actions);
56
57 void nonblock();
58 void cloexec();
59 bool read(libtest::vchar_t&);
60
61 private:
62 const int _std_fd;
63 int _pipe_fd[2];
64 bool _open[2];
65 };
66
67 public:
68 Application(const std::string& arg, const bool _use_libtool_arg= false);
69
70 virtual ~Application();
71
72 void add_option(const std::string&);
73 void add_option(const std::string&, const std::string&);
74 void add_long_option(const std::string& option_name, const std::string& option_value);
75 error_t run(const char *args[]= NULL);
76 error_t wait(bool nohang= true);
77
78 libtest::vchar_t stdout_result() const
79 {
80 return _stdout_buffer;
81 }
82
83 size_t stdout_result_length() const
84 {
85 return _stdout_buffer.size();
86 }
87
88 libtest::vchar_t stderr_result() const
89 {
90 return _stderr_buffer;
91 }
92
93 const char* stderr_c_str() const
94 {
95 return &_stderr_buffer[0];
96 }
97
98 size_t stderr_result_length() const
99 {
100 return _stderr_buffer.size();
101 }
102
103 std::string print();
104
105 void use_valgrind(bool arg= true)
106 {
107 _use_valgrind= arg;
108 }
109
110 bool check() const;
111
112 bool slurp();
113 void murder();
114
115 void use_gdb(bool arg= true)
116 {
117 _use_gdb= arg;
118 }
119
120 void use_ptrcheck(bool arg= true)
121 {
122 _use_ptrcheck= arg;
123 }
124
125 std::string arguments();
126
127 std::string gdb_filename()
128 {
129 return _gdb_filename;
130 }
131
132 pid_t pid() const
133 {
134 return _pid;
135 }
136
137 private:
138 void create_argv(const char *args[]);
139 void delete_argv();
140
141 private:
142 const bool _use_libtool;
143 bool _use_valgrind;
144 bool _use_gdb;
145 bool _use_ptrcheck;
146 size_t _argc;
147 std::string _exectuble_name;
148 std::string _exectuble;
149 std::string _exectuble_with_path;
150 std::string _gdb_filename;
151 Options _options;
152 Pipe stdin_fd;
153 Pipe stdout_fd;
154 Pipe stderr_fd;
155 char * * built_argv;
156 pid_t _pid;
157 libtest::vchar_t _stdout_buffer;
158 libtest::vchar_t _stderr_buffer;
159 };
160
161 static inline std::ostream& operator<<(std::ostream& output, const enum Application::error_t &arg)
162 {
163 switch (arg)
164 {
165 case Application::SUCCESS:
166 output << "EXIT_SUCCESS";
167 break;
168
169 case Application::FAILURE:
170 output << "EXIT_FAILURE";
171 break;
172
173 case Application::INVALID:
174 output << "127";
175 break;
176
177 default:
178 output << "EXIT_UNKNOWN";
179 }
180
181 return output;
182 }
183
184 int exec_cmdline(const std::string& executable, const char *args[], bool use_libtool= false);
185
186 const char *gearmand_binary();
187
188 }