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
|
//! ref: composer/tests/Composer/Test/Util/ProcessExecutorTest.php
// These run real subprocesses (capturing output/stderr/timeout) and assert ProcessExecutor's
// password hiding, line splitting and argument escaping. A few data points remain unportable —
// see the individual `// TODO(phase-d)` comments below.
use shirabe::io::ConsoleIO;
use shirabe::io::IOInterface;
use shirabe::io::buffer_io::BufferIO;
use shirabe::util::process_executor::ProcessExecutor;
use shirabe_external_packages::symfony::console::helper::QuestionHelper;
use shirabe_external_packages::symfony::console::input::array_input::ArrayInput;
use shirabe_external_packages::symfony::console::input::input_interface::InputInterface;
use shirabe_external_packages::symfony::console::output::buffered_output::BufferedOutput;
use shirabe_external_packages::symfony::console::output::output_interface::{
OutputInterface, VERBOSITY_DEBUG, VERBOSITY_NORMAL,
};
use shirabe_php_shim::{PHP_EOL, ob_get_clean, ob_start, trim};
#[test]
fn test_execute_captures_output() {
let mut process = ProcessExecutor::new(None);
let mut output = String::new();
process.execute("echo foo", &mut output, None).unwrap();
assert_eq!(format!("foo{}", PHP_EOL), output);
}
#[ignore = "shirabe_php_shim::ob_start/ob_get_clean are todo!(): the shim has no echo-to-buffer routing, and ProcessExecutor::execute with FORWARD_OUTPUT and io=None writes straight to the real process stdout"]
#[test]
fn test_execute_outputs_if_not_captured() {
let mut process = ProcessExecutor::new(None);
ob_start();
process
.execute("echo foo", ProcessExecutor::FORWARD_OUTPUT, None)
.unwrap();
let output = ob_get_clean();
assert_eq!(Some(format!("foo{}", PHP_EOL)), output);
}
#[test]
fn test_use_io_is_not_null_and_if_not_captured() {
use crate::io_stub::IOStub;
let io = std::rc::Rc::new(std::cell::RefCell::new(IOStub::new()));
let mut process = ProcessExecutor::new(Some(
io.clone() as std::rc::Rc<std::cell::RefCell<dyn IOInterface>>
));
process
.execute("echo foo", ProcessExecutor::FORWARD_OUTPUT, None)
.unwrap();
assert_eq!(
vec![(format!("foo{}", PHP_EOL), false)],
io.borrow().write_raw_calls()
);
}
#[test]
fn test_execute_captures_stderr() {
let mut process = ProcessExecutor::new(None);
let mut output = String::new();
process.execute("cat foo", &mut output, None).unwrap();
assert!(
process
.get_error_output()
.contains("foo: No such file or directory")
);
}
#[test]
fn test_timeout() {
ProcessExecutor::set_timeout(1_i64);
let process = ProcessExecutor::new(None);
assert_eq!(1, ProcessExecutor::get_timeout());
let _ = &process;
ProcessExecutor::set_timeout(60_i64);
}
fn hide_password_provider() -> Vec<(&'static str, &'static str)> {
vec![
(
"echo https://foo:bar@example.org/",
"echo https://foo:***@example.org/",
),
("echo http://foo@example.org", "echo http://foo@example.org"),
(
"echo http://abcdef1234567890234578:x-oauth-token@github.com/",
"echo http://***:***@github.com/",
),
(
"echo http://github_pat_1234567890abcdefghijkl_1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW:x-oauth-token@github.com/",
"echo http://***:***@github.com/",
),
(
"svn ls --verbose --non-interactive --username 'foo' --password 'bar' 'https://foo.example.org/svn/'",
"svn ls --verbose --non-interactive --username 'foo' --password '***' 'https://foo.example.org/svn/'",
),
(
"svn ls --verbose --non-interactive --username 'foo' --password 'bar 'bar' 'https://foo.example.org/svn/'",
"svn ls --verbose --non-interactive --username 'foo' --password '***' 'https://foo.example.org/svn/'",
),
]
}
#[test]
fn test_hide_passwords() {
for (command, expected_command_output) in hide_password_provider() {
let buffer = std::rc::Rc::new(std::cell::RefCell::new(
BufferIO::new(String::new(), VERBOSITY_DEBUG, None).unwrap(),
));
let mut process = ProcessExecutor::new(Some(
buffer.clone() as std::rc::Rc<std::cell::RefCell<dyn IOInterface>>
));
let mut output = String::new();
process.execute(command, &mut output, None).unwrap();
assert_eq!(
format!("Executing command (CWD): {}", expected_command_output),
trim(&buffer.borrow().get_output(), None)
);
}
}
#[test]
fn test_doesnt_hide_ports() {
let buffer = std::rc::Rc::new(std::cell::RefCell::new(
BufferIO::new(String::new(), VERBOSITY_DEBUG, None).unwrap(),
));
let mut process = ProcessExecutor::new(Some(
buffer.clone() as std::rc::Rc<std::cell::RefCell<dyn IOInterface>>
));
let mut output = String::new();
process
.execute("echo https://localhost:1234/", &mut output, None)
.unwrap();
assert_eq!(
"Executing command (CWD): echo https://localhost:1234/",
trim(&buffer.borrow().get_output(), None)
);
}
#[test]
fn test_split_lines() {
let process = ProcessExecutor::new(None);
assert!(process.split_lines("").is_empty());
// PHP: $process->splitLines(null). `split_lines` takes `&str` where PHP takes `?string`, and
// its body opens with `trim((string) $output)`, so the caller performs the null-to-"" cast.
assert!(process.split_lines("").is_empty());
assert_eq!(vec!["foo"], process.split_lines("foo"));
assert_eq!(vec!["foo", "bar"], process.split_lines("foo\nbar"));
assert_eq!(vec!["foo", "bar"], process.split_lines("foo\r\nbar"));
assert_eq!(vec!["foo", "bar"], process.split_lines("foo\r\nbar\n"));
}
#[test]
fn test_console_io_does_not_format_symfony_console_style() {
let output = std::rc::Rc::new(std::cell::RefCell::new(BufferedOutput::new(
Some(VERBOSITY_NORMAL),
true,
None,
)));
let input: std::rc::Rc<std::cell::RefCell<dyn InputInterface>> = std::rc::Rc::new(
std::cell::RefCell::new(ArrayInput::new(vec![], None).unwrap()),
);
let console_io = ConsoleIO::new(
input,
output.clone() as std::rc::Rc<std::cell::RefCell<dyn OutputInterface>>,
QuestionHelper::default(),
);
let mut process =
ProcessExecutor::new(Some(std::rc::Rc::new(std::cell::RefCell::new(console_io))
as std::rc::Rc<std::cell::RefCell<dyn IOInterface>>));
process
.execute(
r#"php -ddisplay_errors=0 -derror_reporting=0 -r "echo '<error>foo</error>'.PHP_EOL;""#,
ProcessExecutor::FORWARD_OUTPUT,
None,
)
.unwrap();
assert_eq!(
format!("<error>foo</error>{}", PHP_EOL),
output.borrow().fetch()
);
}
#[ignore = "none of the three symbols this test drives exist: execute_async returns a plain future with no cancel(), and ProcessExecutor has no count_active_jobs or wait (PHP's $jobs/$maxJobs queue is a tokio semaphore here)"]
#[test]
fn test_execute_async_cancel() {
// TODO(phase-d): PHP's executeAsync returns a React\Promise\PromiseInterface with cancel(),
// and the test reads countActiveJobs() around it and then calls wait(). execute_async here
// returns a plain future with no cancel(), and ProcessExecutor has neither count_active_jobs
// nor wait: the PHP job queue those methods expose is a tokio semaphore in this port.
todo!()
}
fn data_escape_arguments() -> Vec<(&'static str, &'static str)> {
// (argument, unix-expected). Not on Windows, so the unix column is used.
// null and false arguments are coerced to the empty string.
vec![
("", "''"),
("", "''"),
("", "''"),
("a'bc", "'a'\\''bc'"),
("a\nb\nc", "'a\nb\nc'"),
("a b c", "'a b c'"),
("a\tb\tc", "'a\tb\tc'"),
("abc", "'abc'"),
("a,bc", "'a,bc'"),
("a\"bc", "'a\"bc'"),
("a\\\"bc", "'a\\\"bc'"),
("ab\\\\c\\", "'ab\\\\c\\'"),
("a b c\\\\", "'a b c\\\\'"),
("a \"b\" c", "'a \"b\" c'"),
("%path%", "'%path%'"),
("%path", "'%path'"),
("%%path", "'%%path'"),
("!path!", "'!path!'"),
("!path", "'!path'"),
("!!path", "'!!path'"),
("<>\"&|()^", "'<>\"&|()^'"),
("<> &| ()^", "'<> &| ()^'"),
("<>&|()^", "'<>&|()^'"),
]
}
#[test]
fn test_escape_argument() {
for (argument, unix) in data_escape_arguments() {
assert_eq!(unix, ProcessExecutor::escape(argument));
}
}
// Exercises the ProcessExecutorMock infrastructure (cf.
// composer/tests/Composer/Test/Mock/ProcessExecutorMock.php): expectations are matched in order,
// stdout is captured back, stderr surfaces via get_error_output, and assert_complete (via the
// guard) verifies the queue was fully consumed.
#[test]
fn test_mock_consumes_expectations_in_order() {
use crate::process_executor_mock::{cmd_full, get_process_executor_mock};
use shirabe::util::process_executor::MockHandler;
let (process, _guard) = get_process_executor_mock(
vec![
cmd_full("git command", 1, "out one", ""),
cmd_full(["git", "--version"], 0, "git version 2.0.0", "warn"),
],
true,
MockHandler::default(),
);
let mut output = String::new();
let rc = process
.borrow_mut()
.execute("git command", &mut output, None)
.unwrap();
assert_eq!(1, rc);
assert_eq!("out one", output);
let mut output2 = String::new();
let rc2 = process
.borrow_mut()
.execute(&["git", "--version"], &mut output2, None)
.unwrap();
assert_eq!(0, rc2);
assert_eq!("git version 2.0.0", output2);
assert_eq!("warn", process.borrow().get_error_output());
}
// Non-strict mode falls back to the default handler for unexpected commands.
#[test]
fn test_mock_default_handler_for_unexpected_command() {
use crate::process_executor_mock::get_process_executor_mock;
use shirabe::util::process_executor::MockHandler;
let (process, _guard) = get_process_executor_mock(
vec![],
false,
MockHandler {
r#return: 7,
stdout: "fallback".to_string(),
stderr: String::new(),
},
);
let mut output = String::new();
let rc = process
.borrow_mut()
.execute("anything goes", &mut output, None)
.unwrap();
assert_eq!(7, rc);
assert_eq!("fallback", output);
}
|