-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessTests.cpp
More file actions
313 lines (264 loc) · 8.95 KB
/
Copy pathProcessTests.cpp
File metadata and controls
313 lines (264 loc) · 8.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include <vix/engine/Process.hpp>
#include <cstdlib>
#include <exception>
#include <filesystem>
#include <future>
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
#ifndef _WIN32
#include <csignal>
#endif
using namespace vix::engine::process;
namespace
{
namespace fs = std::filesystem;
static fs::path selfPath;
struct TempDir
{
fs::path path;
TempDir()
{
path = fs::temp_directory_path() /
("vix-engine-process-test-" + std::to_string(std::rand()));
fs::remove_all(path);
fs::create_directories(path);
}
~TempDir()
{
std::error_code ec;
fs::remove_all(path, ec);
}
};
static void require(bool condition, const std::string &message)
{
if (!condition)
throw std::runtime_error(message);
}
static Command self_command(std::vector<std::string> args)
{
Command command;
command.argv.push_back(selfPath.string());
command.argv.insert(command.argv.end(), args.begin(), args.end());
return command;
}
static void test_empty_command()
{
const Result result = execute(Command{});
require(!result.success(), "empty command fails");
require(!result.started, "empty command not started");
require(result.exitCode == 127, "empty command exit");
require(!result.errorMessage.empty(), "empty command error");
}
static void test_success_and_nonzero()
{
Result ok = execute(self_command({"--child-exit", "0"}));
require(ok.started, "successful command started");
require(ok.success(), "successful command success");
Result failed = execute(self_command({"--child-exit", "7"}));
require(failed.started, "failed command started");
require(!failed.success(), "failed command not success");
require(failed.exitCode == 7, "failed command exit code");
}
static void test_missing_executable()
{
Command command;
command.argv = {"definitely-missing-vix-engine-process-test-executable"};
const Result result = execute(command);
require(!result.success(), "missing executable fails");
require(result.exitCode == 127, "missing executable exit");
require(!result.errorMessage.empty(), "missing executable error");
}
static void test_stdout_stderr_and_output_flag()
{
Result stdoutResult = execute(self_command({"--child-stdout", "hello"}));
require(stdoutResult.success(), "stdout command success");
require(stdoutResult.output == "hello", "stdout captured");
require(stdoutResult.producedOutput, "stdout produced output");
Result stderrResult = execute(self_command({"--child-stderr", "err"}));
require(stderrResult.success(), "stderr command success");
require(stderrResult.output == "err", "stderr captured");
require(stderrResult.producedOutput, "stderr produced output");
Result noOutput = execute(self_command({"--child-exit", "0"}));
require(noOutput.success(), "no output success");
require(!noOutput.producedOutput, "no output flag");
}
static void test_merged_output_order()
{
const Result result = execute(self_command({"--child-mixed-output"}));
require(result.success(), "mixed output success");
require(result.output == "out1\nerr1\nout2\nerr2\n", "merged output order");
}
static void test_working_directory()
{
TempDir temp;
Command command = self_command({"--child-pwd"});
command.workingDirectory = temp.path;
const Result result = execute(command);
require(result.success(), "working directory success");
require(result.output.find(temp.path.string()) != std::string::npos, "working directory propagated");
}
static void test_arguments_with_spaces_and_quotes()
{
const Result result =
execute(self_command({"--child-print-args", "hello world", "quote\"value"}));
require(result.success(), "argument command success");
require(result.output.find("[hello world]") != std::string::npos, "space argument preserved");
require(result.output.find("[quote\"value]") != std::string::npos, "quote argument preserved");
}
static void test_environment()
{
Command inherited = self_command({"--child-print-env", "PATH"});
const Result inheritedResult = execute(inherited);
require(inheritedResult.success(), "inherited env success");
require(!inheritedResult.output.empty(), "inherited PATH visible");
Command override = self_command({"--child-print-env", "VIX_ENGINE_PROCESS_TEST"});
override.environment.emplace_back("VIX_ENGINE_PROCESS_TEST", "override value");
const Result overrideResult = execute(override);
require(overrideResult.success(), "env override success");
require(overrideResult.output == "override value", "env override visible");
Command disabled = self_command({"--child-print-env", "PATH"});
disabled.inheritEnvironment = false;
const Result disabledResult = execute(disabled);
require(disabledResult.success(), "disabled env success");
require(disabledResult.output.empty(), "disabled env omits parent PATH");
}
static void test_large_output()
{
const Result result = execute(self_command({"--child-large-output"}));
require(result.success(), "large output success");
require(result.output.size() >= 1024 * 1024, "large output captured");
}
static void test_display_command()
{
Command command = self_command({"--child-print-args", "hello world"});
command.workingDirectory = "/tmp/some path";
const std::string display = display_command(command);
require(display.find("hello world") != std::string::npos, "display includes spaced arg");
require(display.find("cd") != std::string::npos, "display includes cwd");
}
static void test_normalize_exit_code()
{
#ifdef _WIN32
require(normalize_exit_code(12) == 12, "windows normalize");
#else
int status = 0;
require(normalize_exit_code(-1) == 127, "normalize wait failure");
require(normalize_exit_code(status) == 0, "normalize zero status");
#endif
}
static void test_signal_termination()
{
#ifndef _WIN32
const Result result = execute(self_command({"--child-signal"}));
require(!result.success(), "signal command fails");
require(result.exitCode == 128 + SIGTERM, "signal normalized");
#endif
}
static void test_repeated_and_concurrent()
{
for (int i = 0; i < 3; ++i)
{
const Result result = execute(self_command({"--child-stdout", "repeat"}));
require(result.success(), "repeated success");
require(result.output == "repeat", "repeated output");
}
auto first = std::async(std::launch::async, []()
{ return execute(self_command({"--child-stdout", "a"})); });
auto second = std::async(std::launch::async, []()
{ return execute(self_command({"--child-stdout", "b"})); });
require(first.get().success(), "concurrent first");
require(second.get().success(), "concurrent second");
}
static int child_main(int argc, char **argv)
{
const std::string mode = argc > 1 ? argv[1] : "";
if (mode == "--child-exit")
return argc > 2 ? std::atoi(argv[2]) : 0;
if (mode == "--child-stdout")
{
if (argc > 2)
std::cout << argv[2];
return 0;
}
if (mode == "--child-stderr")
{
if (argc > 2)
std::cerr << argv[2];
return 0;
}
if (mode == "--child-mixed-output")
{
std::cout << "out1\n" << std::flush;
std::cerr << "err1\n" << std::flush;
std::cout << "out2\n" << std::flush;
std::cerr << "err2\n" << std::flush;
return 0;
}
if (mode == "--child-pwd")
{
std::cout << fs::current_path().string();
return 0;
}
if (mode == "--child-print-args")
{
for (int i = 2; i < argc; ++i)
std::cout << "[" << argv[i] << "]";
return 0;
}
if (mode == "--child-print-env")
{
if (argc > 2)
{
const char *value = std::getenv(argv[2]);
if (value)
std::cout << value;
}
return 0;
}
if (mode == "--child-large-output")
{
std::string chunk(4096, 'x');
for (int i = 0; i < 256; ++i)
std::cout << chunk;
return 0;
}
if (mode == "--child-signal")
{
#ifndef _WIN32
std::raise(SIGTERM);
#endif
return 1;
}
return 2;
}
} // namespace
int main(int argc, char **argv)
{
if (argc > 1 && std::string(argv[1]).rfind("--child-", 0) == 0)
return child_main(argc, argv);
try
{
selfPath = fs::absolute(argv[0]).lexically_normal();
test_empty_command();
test_success_and_nonzero();
test_missing_executable();
test_stdout_stderr_and_output_flag();
test_merged_output_order();
test_working_directory();
test_arguments_with_spaces_and_quotes();
test_environment();
test_large_output();
test_display_command();
test_normalize_exit_code();
test_signal_termination();
test_repeated_and_concurrent();
}
catch (const std::exception &ex)
{
std::cerr << "ProcessTests failed: " << ex.what() << "\n";
return 1;
}
return 0;
}