{{ message }}
Repository navigation
Expand file tree
/
Copy pathrun_shell_test.rs
More file actions
573 lines (503 loc) · 19.7 KB
/
Copy pathrun_shell_test.rs
File metadata and controls
573 lines (503 loc) · 19.7 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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
mod common;
#[cfg(unix)]
mod unix {
use std::process::Command;
fn rtk() -> Command {
crate::common::rtk_command()
}
#[test]
fn positional_arguments_are_not_interpreted_by_a_shell() {
let output = rtk()
.args([
"run",
"/usr/bin/printf",
"[%s]\\n",
"a b",
"*",
"$HOME",
";",
"&&",
"|",
"$(printf injected)",
"`id`",
"line1\nline2",
])
.output()
.expect("run rtk");
assert!(output.status.success());
assert_eq!(
String::from_utf8_lossy(&output.stdout),
"[a b]\n[*]\n[$HOME]\n[;]\n[&&]\n[|]\n[$(printf injected)]\n[`id`]\n[line1\nline2]\n"
);
}
#[test]
fn direct_execution_preserves_child_exit_code() {
let status = rtk()
.args(["run", "/bin/sh", "-c", "exit 42"])
.status()
.expect("run rtk");
assert_eq!(status.code(), Some(42));
}
#[test]
fn command_string_keeps_posix_shell_default() {
let output = rtk()
.args([
"run",
"-c",
"value=$(printf posix_ok); printf '%s\\n' \"$value\"",
])
.output()
.expect("run rtk");
assert!(output.status.success());
assert_eq!(String::from_utf8_lossy(&output.stdout), "posix_ok\n");
}
#[test]
fn explicit_fish_shell_runs_fish_syntax_when_available() {
let Ok(fish) = which::which("fish") else {
return;
};
let output = rtk()
.args([
"run",
"--shell",
fish.to_str().expect("fish path is UTF-8"),
"-c",
"set value (printf fish_ok); printf '%s\\n' $value",
])
.output()
.expect("run rtk");
assert!(output.status.success());
assert_eq!(String::from_utf8_lossy(&output.stdout), "fish_ok\n");
}
#[test]
fn explicit_missing_shell_reports_the_shell_contract() {
let output = rtk()
.args([
"run",
"--shell",
"rtk-missing-shell-for-test",
"-c",
"echo ok",
])
.output()
.expect("run rtk");
// A shell that is not there answers like a program that is not there:
// the shell's own line and exit 127, not an RTK error chain.
assert_eq!(output.status.code(), Some(127));
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains("rtk-missing-shell-for-test: command not found"),
"{stderr}"
);
}
#[test]
fn summary_arguments_are_not_interpreted_by_a_shell() {
let output = rtk()
.args(["summary", "/bin/echo", "*"])
.output()
.expect("run rtk summary");
assert!(output.status.success());
assert_eq!(String::from_utf8_lossy(&output.stdout), "*\n\n\n");
}
#[test]
fn filtered_wrapper_accepts_one_explicit_shell_script() {
let Ok(fish) = which::which("fish") else {
return;
};
let output = rtk()
.args([
"err",
"--shell",
fish.to_str().expect("fish path is UTF-8"),
"printf 'error: fish_ok\\n'",
])
.output()
.expect("run rtk err");
assert!(output.status.success());
assert!(String::from_utf8_lossy(&output.stdout).contains("error: fish_ok"));
}
#[test]
fn filtered_wrapper_rejects_reconstructed_shell_arguments() {
let output = rtk()
.args(["err", "--shell", "sh", "printf", "error: split"])
.output()
.expect("run rtk err");
assert!(!output.status.success());
assert!(
String::from_utf8_lossy(&output.stderr)
.contains("--shell takes the complete command as one quoted argument")
);
}
#[test]
fn err_reports_an_unresolvable_program_as_exit_127() {
// The `sh -c` RTK no longer interposes returned 127 here; CI steps and
// the `[FAIL]` line both key on it.
let output = rtk()
.args(["err", "rtk-no-such-binary-4c1f"])
.output()
.expect("run rtk err");
assert_eq!(output.status.code(), Some(127));
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(
stdout.contains("[FAIL] Command failed (exit code: 127)"),
"{stdout}"
);
assert!(
stdout.contains("rtk-no-such-binary-4c1f: command not found"),
"{stdout}"
);
}
#[test]
fn test_and_summary_report_an_unresolvable_program_as_exit_127() {
for subcommand in ["test", "summary"] {
let output = rtk()
.args([subcommand, "rtk-no-such-binary-4c1f"])
.output()
.unwrap_or_else(|e| panic!("run rtk {subcommand}: {e}"));
assert_eq!(output.status.code(), Some(127), "{subcommand}");
assert!(
String::from_utf8_lossy(&output.stdout)
.contains("rtk-no-such-binary-4c1f: command not found"),
"{subcommand}"
);
}
}
#[test]
fn run_reports_an_unresolvable_program_as_exit_127() {
let output = rtk()
.args(["run", "rtk-no-such-binary-4c1f"])
.output()
.expect("run rtk run");
assert_eq!(output.status.code(), Some(127));
assert!(
String::from_utf8_lossy(&output.stderr)
.contains("rtk-no-such-binary-4c1f: command not found")
);
}
/// 126 is the other half of the contract: `sh`, `dash` and `bash` all
/// answer 126 for something that exists and cannot be executed, and 127
/// only for a program that is not there at all.
#[test]
fn an_unexecutable_path_reports_exit_126() {
let dir = tempfile::tempdir().expect("create tempdir");
let file = dir.path().join("noexec");
std::fs::write(&file, b"not executable\n").expect("write file");
let path = file.to_string_lossy().into_owned();
for subcommand in ["err", "test", "summary"] {
let output = rtk()
.args([subcommand, &path])
.output()
.unwrap_or_else(|e| panic!("run rtk {subcommand}: {e}"));
assert_eq!(output.status.code(), Some(126), "{subcommand}");
assert!(
String::from_utf8_lossy(&output.stdout).contains("Permission denied"),
"{subcommand}"
);
}
let output = rtk().args(["run", &path]).output().expect("run rtk run");
assert_eq!(output.status.code(), Some(126));
assert!(String::from_utf8_lossy(&output.stderr).contains("Permission denied"));
}
#[test]
fn a_directory_reports_exit_126() {
let dir = tempfile::tempdir().expect("create tempdir");
let path = dir.path().to_string_lossy().into_owned();
let output = rtk().args(["run", &path]).output().expect("run rtk run");
assert_eq!(output.status.code(), Some(126));
}
/// Resolution proves the name resolves; `execve` still refuses a CRLF
/// shebang (its interpreter is `/bin/sh\r`) and a file that is not a valid
/// executable. Both used to surface as an anyhow chain and exit 1.
#[test]
fn spawn_failures_keep_the_shell_contract() {
use std::os::unix::fs::PermissionsExt;
let dir = tempfile::tempdir().expect("create tempdir");
let crlf = dir.path().join("crlf.sh");
std::fs::write(&crlf, b"#!/bin/sh\r\necho hi\r\n").expect("write script");
std::fs::set_permissions(&crlf, std::fs::Permissions::from_mode(0o755))
.expect("chmod script");
let output = rtk()
.args(["err", &crlf.to_string_lossy()])
.output()
.expect("run rtk err");
assert_eq!(output.status.code(), Some(127));
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(
stdout.contains("[FAIL] Command failed (exit code: 127)"),
"{stdout}"
);
// An `+x` file the kernel cannot exec: on Linux `execvp` reports
// ENOEXEC, while on macOS it falls back to `sh`, which answers 127
// itself. Either way the outcome is a shell's, never an RTK error.
let binary = dir.path().join("not-an-executable");
std::fs::write(&binary, b"\x7fELF-but-not-really").expect("write file");
std::fs::set_permissions(&binary, std::fs::Permissions::from_mode(0o755))
.expect("chmod file");
let output = rtk()
.args(["run", &binary.to_string_lossy()])
.output()
.expect("run rtk run");
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
matches!(output.status.code(), Some(126) | Some(127)),
"{:?} / {stderr}",
output.status.code()
);
assert!(!stderr.contains("Failed to spawn process"), "{stderr}");
}
/// `--shell` is the first flag these surfaces have, and a missing value is
/// the likeliest way to get it wrong. It must report the flag error, not
/// exec a program named after the subcommand.
#[test]
fn a_missing_shell_value_reports_a_flag_error() {
for subcommand in ["err", "test", "summary", "run"] {
let output = rtk()
.args([subcommand, "--shell"])
.output()
.unwrap_or_else(|e| panic!("run rtk {subcommand}: {e}"));
assert_eq!(output.status.code(), Some(2), "{subcommand}");
assert!(
String::from_utf8_lossy(&output.stderr).contains("--shell <SHELL>"),
"{subcommand}: {}",
String::from_utf8_lossy(&output.stderr)
);
}
}
/// The two named-shell tests above skip wherever `fish` is absent, which is
/// every CI runner — this one names a shell that always exists, so the
/// explicit-shell branch is actually exercised somewhere.
#[test]
fn explicit_shell_by_name_runs_the_named_shell() {
let output = rtk()
.args(["run", "--shell", "sh", "-c", "printf 'shell_ok'"])
.output()
.expect("run rtk run");
assert!(output.status.success());
assert_eq!(String::from_utf8_lossy(&output.stdout), "shell_ok");
}
#[test]
fn an_unresolvable_shell_reports_the_shell_contract() {
let output = rtk()
.args(["run", "--shell", "rtk-no-such-shell-4c1f", "-c", "echo hi"])
.output()
.expect("run rtk run");
assert_eq!(output.status.code(), Some(127));
assert!(
String::from_utf8_lossy(&output.stderr).contains("rtk-no-such-shell-4c1f"),
"{}",
String::from_utf8_lossy(&output.stderr)
);
}
/// A `( … )` pair only wraps the whole command when it balances across it.
/// `( a ) b ( c )` closes its first group before the end, so peeling the
/// outer tokens would hand `b` an argument list it never had.
#[test]
fn a_group_that_closes_early_is_not_peeled() {
let output = rtk()
.args(["test", "(", "/bin/true", ")", "x", "(", "/bin/false", ")"])
.output()
.expect("run rtk test");
assert_eq!(output.status.code(), Some(127));
assert!(
String::from_utf8_lossy(&output.stdout).contains("command not found"),
"{}",
String::from_utf8_lossy(&output.stdout)
);
}
/// `--shell` is RTK's flag on `rtk test`, and a flag other tools carry too.
/// The guard that answers a misused `rtk test --shell` must not read argv
/// belonging to a command RTK is only passing through.
#[test]
fn a_passthrough_keeps_its_own_shell_flag() {
let output = rtk()
.args(["/bin/echo", "--shell", "bash", "hi"])
.output()
.expect("run rtk passthrough");
assert!(
output.status.success(),
"{}",
String::from_utf8_lossy(&output.stderr)
);
assert_eq!(
String::from_utf8_lossy(&output.stdout).trim_end(),
"--shell bash hi"
);
}
/// `metadata` answers EACCES rather than yes-or-no when a directory on the
/// way to the program is not searchable, so its existence is unknown from
/// there; 127 would assert that it is not there.
#[test]
fn an_unsearchable_parent_reports_exit_126() {
use std::os::unix::fs::PermissionsExt;
let dir = tempfile::tempdir().expect("create tempdir");
let closed = dir.path().join("closed");
std::fs::create_dir(&closed).expect("create dir");
let program = closed.join("prog");
std::fs::write(&program, b"#!/bin/sh\nexit 0\n").expect("write program");
std::fs::set_permissions(&program, std::fs::Permissions::from_mode(0o755))
.expect("chmod program");
std::fs::set_permissions(&closed, std::fs::Permissions::from_mode(0o000))
.expect("close dir");
// Root ignores the search bit, so `metadata` answers there instead of
// failing and the premise does not hold. Leave rather than assert an
// outcome the environment cannot produce.
if std::fs::read_dir(&closed).is_ok() {
std::fs::set_permissions(&closed, std::fs::Permissions::from_mode(0o755)).ok();
return;
}
let output = rtk()
.arg("run")
.arg(&program)
.output()
.expect("run rtk run");
std::fs::set_permissions(&closed, std::fs::Permissions::from_mode(0o755)).ok();
assert_eq!(output.status.code(), Some(126));
assert!(
String::from_utf8_lossy(&output.stderr).contains("Permission denied"),
"{}",
String::from_utf8_lossy(&output.stderr)
);
}
#[test]
fn grouped_and_negated_commands_keep_their_exit_codes() {
// `!` and `( … )` are `test`'s syntax as much as the shell's, and they
// nest: the joined `sh -c` string used to apply both. Stripping one of
// each in a single pass left `(` as the program, so a negation turned
// that 127 into a reported *pass* for a command that never ran.
for (args, expected) in [
(vec!["test", "!", "false"], 0),
(vec!["test", "!", "true"], 1),
(vec!["test", "(", "false", ")"], 1),
(vec!["test", "(", "!", "false", ")"], 0),
(vec!["test", "!", "(", "true", ")"], 1),
(vec!["test", "!", "(", "false", ")"], 0),
(vec!["test", "(", "(", "false", ")", ")"], 1),
(vec!["test", "!", "!", "(", "true", ")"], 0),
] {
let output = rtk()
.args(&args)
.output()
.unwrap_or_else(|e| panic!("run rtk {args:?}: {e}"));
assert_eq!(output.status.code(), Some(expected), "{args:?}");
assert!(
!String::from_utf8_lossy(&output.stdout).contains("command not found"),
"{args:?} must run the command, not report it missing"
);
}
}
#[test]
fn a_single_shell_phrase_falls_back_to_the_platform_shell() {
let output = rtk()
.args(["run", "echo one two"])
.output()
.expect("run rtk run");
assert!(output.status.success());
assert_eq!(String::from_utf8_lossy(&output.stdout), "one two\n");
assert!(
String::from_utf8_lossy(&output.stderr).contains("pass arguments separately"),
"{}",
String::from_utf8_lossy(&output.stderr)
);
}
#[test]
fn the_fallback_propagates_the_script_exit_code() {
let output = rtk()
.args(["err", "true && false"])
.output()
.expect("run rtk err");
assert_eq!(output.status.code(), Some(1));
}
#[test]
fn single_string_shell_syntax_reaches_the_shell() {
for (args, expected_code) in [
(["run", "echo x | grep x"], 0),
(["summary", "echo a && echo b"], 0),
(["test", "cd /tmp && pwd"], 0),
(["err", "cargo --version 2>&1"], 0),
] {
let output = rtk()
.args(args)
.output()
.unwrap_or_else(|e| panic!("run rtk {args:?}: {e}"));
assert_eq!(output.status.code(), Some(expected_code), "{args:?}");
assert!(
!String::from_utf8_lossy(&output.stdout).contains("command not found"),
"{args:?} must run through the shell, not report it missing"
);
}
}
#[test]
fn a_single_bare_word_still_answers_127_without_the_shell() {
let output = rtk()
.args(["run", "rtk-no-such-binary-4c1f"])
.output()
.expect("run rtk run");
assert_eq!(output.status.code(), Some(127));
assert!(
!String::from_utf8_lossy(&output.stderr).contains("pass arguments separately"),
"a bare word must not trigger the compatibility fallback"
);
}
#[test]
fn the_fallback_requires_a_single_argument() {
let output = rtk()
.args(["run", "echo one two", "x"])
.output()
.expect("run rtk run");
assert_eq!(output.status.code(), Some(127));
}
}
#[cfg(windows)]
mod windows {
use std::process::Command;
fn rtk() -> Command {
crate::common::rtk_command()
}
/// Direct execution resolves through `%PATH%` (and `PATHEXT`), where the
/// `cmd /C` string it replaced also searched the working directory and
/// carried builtins. Anything `cmd`-specific now needs `rtk run -c`.
#[test]
fn direct_execution_resolves_through_path() {
let output = rtk()
.args(["run", "cmd", "/C", "echo windows_ok"])
.output()
.expect("run rtk run");
assert!(output.status.success());
assert!(String::from_utf8_lossy(&output.stdout).contains("windows_ok"));
}
/// An argument carrying a `"` reaches a non-batch child with the encoding
/// MSYS/Cygwin and libuv children expect (`child_args`, #3728).
///
/// `cmd.exe` is the wrong witness for this — it parses its own way and
/// echoes the encoding back verbatim — so the child here is `rtk` itself:
/// `rtk rewrite` prints the command string it received, which is only the
/// one that was sent if the quote survived re-encoding on both sides.
#[test]
fn quoted_arguments_reach_the_child_intact() {
let home = tempfile::tempdir().expect("create isolated home");
let child = rtk();
let output = rtk()
.arg("run")
.arg(child.get_program())
.args(["rewrite", "git status \"a b\""])
.env("HOME", home.path())
.env("USERPROFILE", home.path())
.env("XDG_CONFIG_HOME", home.path())
.output()
.expect("run rtk run");
let stdout = String::from_utf8_lossy(&output.stdout);
assert_eq!(stdout.trim_end(), "rtk git status \"a b\"", "{stdout}");
}
#[test]
fn missing_program_reports_exit_127() {
let output = rtk()
.args(["err", "rtk-no-such-binary-4c1f"])
.output()
.expect("run rtk err");
assert_eq!(output.status.code(), Some(127));
assert!(
String::from_utf8_lossy(&output.stdout)
.contains("rtk-no-such-binary-4c1f: command not found")
);
}
}
You can’t perform that action at this time.
