Update
[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();
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();
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 private:
124 void create_argv(const char *args[]);
125 void delete_argv();
126
127 private:
128 const bool _use_libtool;
129 bool _use_valgrind;
130 bool _use_gdb;
131 size_t _argc;
132 std::string _exectuble_name;
133 std::string _exectuble;
134 std::string _exectuble_with_path;
135 std::string _gdb_filename;
136 Options _options;
137 Pipe stdin_fd;
138 Pipe stdout_fd;
139 Pipe stderr_fd;
140 char * * built_argv;
141 pid_t _pid;
142 libtest::vchar_t _stdout_buffer;
143 libtest::vchar_t _stderr_buffer;
144 };
145
146 static inline std::ostream& operator<<(std::ostream& output, const enum Application::error_t &arg)
147 {
148 switch (arg)
149 {
150 case Application::SUCCESS:
151 output << "EXIT_SUCCESS";
152 break;
153
154 case Application::FAILURE:
155 output << "EXIT_FAILURE";
156 break;
157
158 case Application::INVALID:
159 output << "127";
160 break;
161
162 default:
163 output << "EXIT_UNKNOWN";
164 }
165
166 return output;
167 }
168
169 int exec_cmdline(const std::string& executable, const char *args[], bool use_libtool= false);
170
171 const char *gearmand_binary();
172
173 }