Update libtest/associated tests.
[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 private:
99 void create_argv(const char *args[]);
100 void delete_argv();
101
102 private:
103 const bool _use_libtool;
104 size_t _argc;
105 std::string _exectuble;
106 std::string _exectuble_with_path;
107 Options _options;
108 Pipe stdin_fd;
109 Pipe stdout_fd;
110 Pipe stderr_fd;
111 char * * built_argv;
112 pid_t _pid;
113 libtest::vchar_t _stdout_buffer;
114 libtest::vchar_t _stderr_buffer;
115 };
116
117 static inline std::ostream& operator<<(std::ostream& output, const enum Application::error_t &arg)
118 {
119 switch (arg)
120 {
121 case Application::SUCCESS:
122 output << "EXIT_SUCCESS";
123 break;
124
125 case Application::FAILURE:
126 output << "EXIT_FAILURE";
127 break;
128
129 case Application::INVALID:
130 output << "127";
131 break;
132 }
133
134 return output;
135 }
136
137 int exec_cmdline(const std::string& executable, const char *args[], bool use_libtool= false);
138
139 const char *gearmand_binary();
140
141 }