Add in better testing for gdb.
[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 private:
62 int _fd[2];
63 bool _open[2];
64 };
65
66 public:
67 Application(const std::string& arg, const bool _use_libtool_arg= false);
68
69 virtual ~Application();
70
71 void add_option(const std::string&);
72 void add_option(const std::string&, const std::string&);
73 error_t run(const char *args[]= NULL);
74 error_t wait();
75
76 libtest::vchar_t stdout_result() const
77 {
78 return _stdout_buffer;
79 }
80
81 size_t stdout_result_length() const
82 {
83 return _stdout_buffer.size();
84 }
85
86 libtest::vchar_t stderr_result() const
87 {
88 return _stderr_buffer;
89 }
90
91 size_t stderr_result_length() const
92 {
93 return _stderr_buffer.size();
94 }
95
96 std::string print();
97
98 void use_valgrind(bool arg= true)
99 {
100 _use_valgrind= arg;
101 }
102
103 void use_gdb(bool arg= true)
104 {
105 _use_gdb= arg;
106 }
107
108 std::string arguments();
109
110 std::string gdb_filename()
111 {
112 return _gdb_filename;
113 }
114
115 private:
116 void create_argv(const char *args[]);
117 void delete_argv();
118
119 private:
120 const bool _use_libtool;
121 bool _use_valgrind;
122 bool _use_gdb;
123 size_t _argc;
124 std::string _exectuble_name;
125 std::string _exectuble;
126 std::string _exectuble_with_path;
127 std::string _gdb_filename;
128 Options _options;
129 Pipe stdin_fd;
130 Pipe stdout_fd;
131 Pipe stderr_fd;
132 char * * built_argv;
133 pid_t _pid;
134 libtest::vchar_t _stdout_buffer;
135 libtest::vchar_t _stderr_buffer;
136 };
137
138 static inline std::ostream& operator<<(std::ostream& output, const enum Application::error_t &arg)
139 {
140 switch (arg)
141 {
142 case Application::SUCCESS:
143 output << "EXIT_SUCCESS";
144 break;
145
146 case Application::FAILURE:
147 output << "EXIT_FAILURE";
148 break;
149
150 case Application::INVALID:
151 output << "127";
152 break;
153
154 default:
155 output << "EXIT_UNKNOWN";
156 }
157
158 return output;
159 }
160
161 int exec_cmdline(const std::string& executable, const char *args[], bool use_libtool= false);
162
163 const char *gearmand_binary();
164
165 }