Skip to content

Commit 7cb0908

Browse files
committed
test(env): Use new_ucmd!() when possible
1 parent 62030b5 commit 7cb0908

1 file changed

Lines changed: 89 additions & 134 deletions

File tree

tests/by-util/test_env.rs

Lines changed: 89 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -112,97 +112,94 @@ fn test_if_windows_batch_files_can_be_executed() {
112112
#[test]
113113
fn test_debug_1() {
114114
let ts = TestScenario::new(util_name!());
115-
let result = ts
116-
.ucmd()
115+
116+
ts.ucmd()
117117
.arg("-v")
118118
.arg(&ts.bin_path)
119119
.args(&["echo", "hello"])
120-
.succeeds();
121-
result.stderr_matches(
122-
&Regex::new(concat!(
123-
r"executing: [^\n]+(\/|\\)coreutils(\.exe)?\n",
124-
r" arg\[0\]= '[^\n]+(\/|\\)coreutils(\.exe)?'\n",
125-
r" arg\[1\]= 'echo'\n",
126-
r" arg\[2\]= 'hello'"
127-
))
128-
.unwrap(),
129-
);
120+
.succeeds()
121+
.stderr_matches(
122+
&Regex::new(concat!(
123+
r"executing: [^\n]+(\/|\\)coreutils(\.exe)?\n",
124+
r" arg\[0\]= '[^\n]+(\/|\\)coreutils(\.exe)?'\n",
125+
r" arg\[1\]= 'echo'\n",
126+
r" arg\[2\]= 'hello'"
127+
))
128+
.unwrap(),
129+
);
130130
}
131131

132132
#[cfg(feature = "echo")]
133133
#[test]
134134
fn test_debug_2() {
135135
let ts = TestScenario::new(util_name!());
136-
let result = ts
137-
.ucmd()
136+
ts.ucmd()
138137
.arg("-vv")
139138
.arg(&ts.bin_path)
140139
.args(&["echo", "hello2"])
141-
.succeeds();
142-
result.stderr_matches(
143-
&Regex::new(concat!(
144-
r"input args:\n",
145-
r"arg\[0\]: 'env'\n",
146-
r"arg\[1\]: '-vv'\n",
147-
r"arg\[2\]: '[^\n]+(\/|\\)coreutils(.exe)?'\n",
148-
r"arg\[3\]: 'echo'\n",
149-
r"arg\[4\]: 'hello2'\n",
150-
r"executing: [^\n]+(\/|\\)coreutils(.exe)?\n",
151-
r" arg\[0\]= '[^\n]+(\/|\\)coreutils(.exe)?'\n",
152-
r" arg\[1\]= 'echo'\n",
153-
r" arg\[2\]= 'hello2'"
154-
))
155-
.unwrap(),
156-
);
140+
.succeeds()
141+
.stderr_matches(
142+
&Regex::new(concat!(
143+
r"input args:\n",
144+
r"arg\[0\]: 'env'\n",
145+
r"arg\[1\]: '-vv'\n",
146+
r"arg\[2\]: '[^\n]+(\/|\\)coreutils(.exe)?'\n",
147+
r"arg\[3\]: 'echo'\n",
148+
r"arg\[4\]: 'hello2'\n",
149+
r"executing: [^\n]+(\/|\\)coreutils(.exe)?\n",
150+
r" arg\[0\]= '[^\n]+(\/|\\)coreutils(.exe)?'\n",
151+
r" arg\[1\]= 'echo'\n",
152+
r" arg\[2\]= 'hello2'"
153+
))
154+
.unwrap(),
155+
);
157156
}
158157

159158
#[cfg(feature = "echo")]
160159
#[test]
161160
fn test_debug1_part_of_string_arg() {
162161
let ts = TestScenario::new(util_name!());
163162

164-
let result = ts
165-
.ucmd()
163+
ts.ucmd()
166164
.arg("-vS FOO=BAR")
167165
.arg(&ts.bin_path)
168166
.args(&["echo", "hello1"])
169-
.succeeds();
170-
result.stderr_matches(
171-
&Regex::new(concat!(
172-
r"executing: [^\n]+(\/|\\)coreutils(\.exe)?\n",
173-
r" arg\[0\]= '[^\n]+(\/|\\)coreutils(\.exe)?'\n",
174-
r" arg\[1\]= 'echo'\n",
175-
r" arg\[2\]= 'hello1'"
176-
))
177-
.unwrap(),
178-
);
167+
.succeeds()
168+
.stderr_matches(
169+
&Regex::new(concat!(
170+
r"executing: [^\n]+(\/|\\)coreutils(\.exe)?\n",
171+
r" arg\[0\]= '[^\n]+(\/|\\)coreutils(\.exe)?'\n",
172+
r" arg\[1\]= 'echo'\n",
173+
r" arg\[2\]= 'hello1'"
174+
))
175+
.unwrap(),
176+
);
179177
}
180178

181179
#[cfg(feature = "echo")]
182180
#[test]
183181
fn test_debug2_part_of_string_arg() {
184182
let ts = TestScenario::new(util_name!());
185-
let result = ts
186-
.ucmd()
183+
ts.ucmd()
187184
.arg("-vvS FOO=BAR")
188185
.arg(&ts.bin_path)
189186
.args(&["echo", "hello2"])
190-
.succeeds();
191-
result.stderr_matches(
192-
&Regex::new(concat!(
193-
r"input args:\n",
194-
r"arg\[0\]: 'env'\n",
195-
r"arg\[1\]: '-vvS FOO=BAR'\n",
196-
r"arg\[2\]: '[^\n]+(\/|\\)coreutils(.exe)?'\n",
197-
r"arg\[3\]: 'echo'\n",
198-
r"arg\[4\]: 'hello2'\n",
199-
r"executing: [^\n]+(\/|\\)coreutils(.exe)?\n",
200-
r" arg\[0\]= '[^\n]+(\/|\\)coreutils(.exe)?'\n",
201-
r" arg\[1\]= 'echo'\n",
202-
r" arg\[2\]= 'hello2'"
203-
))
204-
.unwrap(),
205-
);
187+
.succeeds()
188+
.stderr_matches(
189+
&Regex::new(concat!(
190+
r"input args:\n",
191+
r"arg\[0\]: 'env'\n",
192+
r"arg\[1\]: '-vvS FOO=BAR'\n",
193+
r"arg\[2\]: '[^\n]+(\/|\\)coreutils(.exe)?'\n",
194+
r"arg\[3\]: 'echo'\n",
195+
r"arg\[4\]: 'hello2'\n",
196+
r"executing: [^\n]+(\/|\\)coreutils(.exe)?\n",
197+
r" arg\[0\]= '[^\n]+(\/|\\)coreutils(.exe)?'\n",
198+
r" arg\[1\]= 'echo'\n",
199+
r" arg\[2\]= 'hello2'"
200+
))
201+
.unwrap(),
202+
);
206203
}
207204

208205
#[test]
@@ -291,10 +288,8 @@ fn test_multiple_name_value_pairs() {
291288

292289
#[test]
293290
fn test_ignore_environment() {
294-
let scene = TestScenario::new(util_name!());
295-
296-
scene.ucmd().arg("-i").succeeds().no_stdout();
297-
scene.ucmd().arg("-").succeeds().no_stdout();
291+
new_ucmd!().arg("-i").succeeds().no_stdout();
292+
new_ucmd!().arg("-").succeeds().no_stdout();
298293
}
299294

300295
#[test]
@@ -326,8 +321,7 @@ fn test_null_delimiter() {
326321

327322
#[test]
328323
fn test_unset_variable() {
329-
let out = TestScenario::new(util_name!())
330-
.ucmd()
324+
let out = new_ucmd!()
331325
.env("HOME", "FOO")
332326
.arg("-u")
333327
.arg("HOME")
@@ -349,16 +343,15 @@ fn test_fail_null_with_program() {
349343
#[cfg(not(windows))]
350344
#[test]
351345
fn test_change_directory() {
352-
let scene = TestScenario::new(util_name!());
353346
let temporary_directory = tempdir().unwrap();
354347
let temporary_path = std::fs::canonicalize(temporary_directory.path()).unwrap();
348+
355349
assert_ne!(env::current_dir().unwrap(), temporary_path);
356350

357351
// command to print out current working directory
358352
let pwd = "pwd";
359353

360-
let out = scene
361-
.ucmd()
354+
let out = new_ucmd!()
362355
.arg("--chdir")
363356
.arg(&temporary_path)
364357
.arg(pwd)
@@ -370,7 +363,6 @@ fn test_change_directory() {
370363
#[cfg(windows)]
371364
#[test]
372365
fn test_change_directory() {
373-
let scene = TestScenario::new(util_name!());
374366
let temporary_directory = tempdir().unwrap();
375367

376368
let temporary_path = temporary_directory.path();
@@ -389,8 +381,7 @@ fn test_change_directory() {
389381
// command to print out current working directory
390382
let pwd = [&*cmd_path, "/C", "cd"];
391383

392-
let out = scene
393-
.ucmd()
384+
let out = new_ucmd!()
394385
.arg("--chdir")
395386
.arg(temporary_path)
396387
.args(&pwd)
@@ -401,12 +392,10 @@ fn test_change_directory() {
401392

402393
#[test]
403394
fn test_fail_change_directory() {
404-
let scene = TestScenario::new(util_name!());
405395
let some_non_existing_path = "some_nonexistent_path";
406396
assert!(!Path::new(some_non_existing_path).is_dir());
407397

408-
let out = scene
409-
.ucmd()
398+
let out = new_ucmd!()
410399
.arg("--chdir")
411400
.arg(some_non_existing_path)
412401
.arg("pwd")
@@ -418,95 +407,68 @@ fn test_fail_change_directory() {
418407
#[cfg(not(target_os = "windows"))] // windows has no executable "echo", its only supported as part of a batch-file
419408
#[test]
420409
fn test_split_string_into_args_one_argument_no_quotes() {
421-
let scene = TestScenario::new(util_name!());
422-
423-
let out = scene
424-
.ucmd()
410+
new_ucmd!()
425411
.arg("-S echo hello world")
426412
.succeeds()
427-
.stdout_move_str();
428-
assert_eq!(out, "hello world\n");
413+
.stdout_is("hello world\n");
429414
}
430415

431416
#[cfg(not(target_os = "windows"))] // windows has no executable "echo", its only supported as part of a batch-file
432417
#[test]
433418
fn test_split_string_into_args_one_argument() {
434-
let scene = TestScenario::new(util_name!());
435-
436-
let out = scene
437-
.ucmd()
419+
new_ucmd!()
438420
.arg("-S echo \"hello world\"")
439421
.succeeds()
440-
.stdout_move_str();
441-
assert_eq!(out, "hello world\n");
422+
.stdout_is("hello world\n");
442423
}
443424

444425
#[cfg(not(target_os = "windows"))] // windows has no executable "echo", its only supported as part of a batch-file
445426
#[test]
446427
fn test_split_string_into_args_s_escaping_challenge() {
447-
let scene = TestScenario::new(util_name!());
448-
449-
let out = scene
450-
.ucmd()
428+
new_ucmd!()
451429
.args(&[r#"-S echo "hello \"great\" world""#])
452430
.succeeds()
453-
.stdout_move_str();
454-
assert_eq!(out, "hello \"great\" world\n");
431+
.stdout_is("hello \"great\" world\n");
455432
}
456433

457434
#[test]
458435
fn test_split_string_into_args_s_escaped_c_not_allowed() {
459-
let scene = TestScenario::new(util_name!());
460-
461-
let out = scene.ucmd().args(&[r#"-S"\c""#]).fails().stderr_move_str();
462-
assert_eq!(
463-
out,
464-
"env: '\\c' must not appear in double-quoted -S string\n"
465-
);
436+
new_ucmd!()
437+
.args(&[r#"-S"\c""#])
438+
.fails()
439+
.stderr_is("env: '\\c' must not appear in double-quoted -S string\n");
466440
}
467441

468442
#[cfg(not(target_os = "windows"))] // no printf available
469443
#[test]
470444
fn test_split_string_into_args_s_whitespace_handling() {
471-
let scene = TestScenario::new(util_name!());
472-
473-
let out = scene
474-
.ucmd()
445+
new_ucmd!()
475446
.args(&["-Sprintf x%sx\\n A \t B \x0B\x0C\r\n"])
476447
.succeeds()
477-
.stdout_move_str();
478-
assert_eq!(out, "xAx\nxBx\n");
448+
.stdout_is("xAx\nxBx\n");
479449
}
480450

481451
#[cfg(not(target_os = "windows"))] // no printf available
482452
#[test]
483453
fn test_split_string_into_args_long_option_whitespace_handling() {
484-
let scene = TestScenario::new(util_name!());
485-
486-
let out = scene
487-
.ucmd()
454+
new_ucmd!()
488455
.args(&["--split-string printf x%sx\\n A \t B \x0B\x0C\r\n"])
489456
.succeeds()
490-
.stdout_move_str();
491-
assert_eq!(out, "xAx\nxBx\n");
457+
.stdout_is("xAx\nxBx\n");
492458
}
493459

494460
#[cfg(not(target_os = "windows"))] // no printf available
495461
#[test]
496462
fn test_split_string_into_args_debug_output_whitespace_handling() {
497-
let scene = TestScenario::new(util_name!());
498-
499-
let out = scene
500-
.ucmd()
463+
new_ucmd!()
501464
.args(&["-vvS printf x%sx\\n A \t B \x0B\x0C\r\n"])
502-
.succeeds();
503-
assert_eq!(out.stdout_str(), "xAx\nxBx\n");
504-
assert_eq!(
505-
out.stderr_str(),
506-
"input args:\narg[0]: 'env'\narg[1]: $\
465+
.succeeds()
466+
.stdout_is("xAx\nxBx\n")
467+
.stderr_is(
468+
"input args:\narg[0]: 'env'\narg[1]: $\
507469
'-vvS printf x%sx\\\\n A \\t B \\x0B\\x0C\\r\\n'\nexecuting: printf\
508-
\n arg[0]= 'printf'\n arg[1]= $'x%sx\\n'\n arg[2]= 'A'\n arg[3]= 'B'\n"
509-
);
470+
\n arg[0]= 'printf'\n arg[1]= $'x%sx\\n'\n arg[2]= 'A'\n arg[3]= 'B'\n",
471+
);
510472
}
511473

512474
// FixMe: This test fails on MACOS:
@@ -516,8 +478,6 @@ fn test_split_string_into_args_debug_output_whitespace_handling() {
516478
#[cfg(not(target_os = "macos"))]
517479
#[test]
518480
fn test_gnu_e20() {
519-
let scene = TestScenario::new(util_name!());
520-
521481
let env_bin = String::from(crate::common::util::TESTS_BINARY) + " " + util_name!();
522482

523483
let (input, output) = (
@@ -528,7 +488,7 @@ fn test_gnu_e20() {
528488
"A=B C=D\n",
529489
);
530490

531-
let out = scene.ucmd().args(&input).succeeds();
491+
let out = new_ucmd!().args(&input).succeeds();
532492
assert_eq!(out.stdout_str(), output);
533493
}
534494

@@ -630,9 +590,7 @@ fn test_env_parsing_errors() {
630590

631591
#[test]
632592
fn test_env_with_empty_executable_single_quotes() {
633-
let ts = TestScenario::new(util_name!());
634-
635-
ts.ucmd()
593+
new_ucmd!()
636594
.args(&["-S''"]) // empty single quotes, considered as program name
637595
.fails_with_code(127)
638596
.no_stdout()
@@ -641,9 +599,7 @@ fn test_env_with_empty_executable_single_quotes() {
641599

642600
#[test]
643601
fn test_env_with_empty_executable_double_quotes() {
644-
let ts = TestScenario::new(util_name!());
645-
646-
ts.ucmd()
602+
new_ucmd!()
647603
.args(&["-S\"\""]) // empty double quotes, considered as program name
648604
.fails_with_code(127)
649605
.no_stdout()
@@ -861,8 +817,7 @@ fn test_env_arg_ignore_signal_valid_signals() {
861817
#[test]
862818
#[cfg(unix)]
863819
fn test_env_arg_ignore_signal_empty() {
864-
let ts = TestScenario::new(util_name!());
865-
ts.ucmd()
820+
new_ucmd!()
866821
.args(&["--ignore-signal=", "echo", "hello"])
867822
.succeeds()
868823
.no_stderr()

0 commit comments

Comments
 (0)