Sync libtest.
[m6w6/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();
43 ~Pipe();
44
45 int* fd()
46 {
47 return _fd;
48 }
49
50 enum close_t {
51 READ,
52 WRITE
53 };
54
55 void reset();
56 void close(const close_t& arg);
57 void dup_for_spawn(const close_t& arg,
58 posix_spawn_file_actions_t& file_actions,
59 const int newfildes);
60
61 void nonblock();
62
63 private:
64 int _fd[2];
65 bool _open[2];
66 };
67
68 public:
69 Application(const std::string& arg, const bool _use_libtool_arg= false);
70
71 virtual ~Application();
72
73 void add_option(const std::string&);
74 void add_option(const std::string&, const std::string&);
75 void add_long_option(const std::string& option_name, const std::string& option_value);
76 error_t run(const char *args[]= NULL);
77 error_t wait(bool nohang= true);
78
79 libtest::vchar_t stdout_result() const
80 {
81 return _stdout_buffer;
82 }
83
84 size_t stdout_result_length() const
85 {
86 return _stdout_buffer.size();
87 }
88
89 libtest::vchar_t stderr_result() const
90 {
91 return _stderr_buffer;
92 }
93
94 size_t stderr_result_length() const
95 {
96 return _stderr_buffer.size();
97 }
98
99 std::string print();
100
101 void use_valgrind(bool arg= true)
102 {
103 _use_valgrind= arg;
104 }
105
106 bool check() const;
107
108 bool slurp();
109 void murder();
110
111 void use_gdb(bool arg= true)
112 {
113 _use_gdb= arg;
114 }
115
116 std::string arguments();
117
118 std::string gdb_filename()
119 {
120 return _gdb_filename;
121 }
122
123 pid_t pid() const
124 {
125 return _pid;
126 }
127
128 private:
129 void create_argv(const char *args[]);
130 void delete_argv();
131
132 private:
133 const bool _use_libtool;
134 bool _use_valgrind;
135 bool _use_gdb;
136 size_t _argc;
137 std::string _exectuble_name;
138 std::string _exectuble;
139 std::string _exectuble_with_path;
140 std::string _gdb_filename;
141 Options _options;
142 Pipe stdin_fd;
143 Pipe stdout_fd;
144 Pipe stderr_fd;
145 char * * built_argv;
146 pid_t _pid;
147 libtest::vchar_t _stdout_buffer;
148 libtest::vchar_t _stderr_buffer;
149 };
150
151 static inline std::ostream& operator<<(std::ostream& output, const enum Application::error_t &arg)
152 {
153 switch (arg)
154 {
155 case Application::SUCCESS:
156 output << "EXIT_SUCCESS";
157 break;
158
159 case Application::FAILURE:
160 output << "EXIT_FAILURE";
161 break;
162
163 case Application::INVALID:
164 output << "127";
165 break;
166
167 default:
168 output << "EXIT_UNKNOWN";
169 }
170
171 return output;
172 }
173
174 int exec_cmdline(const std::string& executable, const char *args[], bool use_libtool= false);
175
176 const char *gearmand_binary();
177
178 }