Skip to content

Commit f0ea252

Browse files
committed
Merge branch 'refactor-cli-tests' into 'master'
Clean up CLI tests See merge request mkjeldsen/commitmsgfmt!60
2 parents e41d7df + 8fe0833 commit f0ea252

File tree

1 file changed

+48
-74
lines changed

1 file changed

+48
-74
lines changed

src/main.rs

Lines changed: 48 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ mod tests {
219219

220220
use std::process::Child;
221221
use std::process::Command;
222+
use std::process::Output;
222223
use std::process::Stdio;
223224

224225
fn cargo_run_cmd() -> Vec<String> {
@@ -253,6 +254,37 @@ mod tests {
253254
cmd
254255
}
255256

257+
fn run_debug_binary_no_input(mut cmd: Command) -> Output {
258+
cmd.output().expect("run debug binary")
259+
}
260+
261+
fn run_debug_binary_with_input(mut cmd: Command, input: &[u8]) -> Output {
262+
use std::io::Write;
263+
let mut cmd: Child = cmd
264+
.stdin(Stdio::piped())
265+
.stdout(Stdio::piped())
266+
.spawn()
267+
.expect("spawn debug binary");
268+
269+
cmd.stdin
270+
.as_mut()
271+
.expect("child stdin")
272+
.write_all(input)
273+
.expect("write to child stdin");
274+
275+
cmd.wait_with_output().expect("run debug binary")
276+
}
277+
278+
fn assert_cmd_success(output: &Output) {
279+
assert!(output.status.success());
280+
assert_stderr_empty(&output);
281+
}
282+
283+
fn assert_stderr_empty(output: &Output) {
284+
let err = String::from_utf8_lossy(&output.stderr);
285+
assert!(err.is_empty());
286+
}
287+
256288
#[test]
257289
fn to_utf8_converts_utf_8_bytes_to_utf_8() {
258290
let some_utf_8_str = "å";
@@ -283,10 +315,8 @@ mod tests {
283315
}
284316

285317
#[test]
286-
fn width_0_exits_with_code_1() {
287-
let output = target_binary_with_width("0")
288-
.output()
289-
.expect("run debug binary");
318+
fn arg_width_0_exits_with_code_1() {
319+
let output = run_debug_binary_no_input(target_binary_with_width("0"));
290320

291321
assert_eq!(1, output.status.code().unwrap());
292322

@@ -296,10 +326,8 @@ mod tests {
296326
}
297327

298328
#[test]
299-
fn width_0foo_exits_with_code_1() {
300-
let output = target_binary_with_width("0foo")
301-
.output()
302-
.expect("run debug binary");
329+
fn arg_width_0foo_exits_with_code_1() {
330+
let output = run_debug_binary_no_input(target_binary_with_width("0foo"));
303331

304332
assert_eq!(1, output.status.code().unwrap());
305333

@@ -309,11 +337,11 @@ mod tests {
309337
}
310338

311339
#[test]
312-
fn width_with_multiple_values_exits_with_code_1() {
340+
fn arg_width_with_multiple_values_exits_with_code_1() {
313341
let mut cmd = target_binary();
314342
cmd.args(&["-w", "1", "2"]);
315343

316-
let output = cmd.output().expect("run debug binary");
344+
let output = run_debug_binary_no_input(cmd);
317345

318346
assert_eq!(1, output.status.code().unwrap());
319347

@@ -322,24 +350,11 @@ mod tests {
322350
}
323351

324352
#[test]
325-
fn width_1_wraps_body_at_width_1_and_exits_successfully() {
326-
use std::io::Write;
327-
328-
let mut cmd: Child = target_binary_with_width("1")
329-
.stdin(Stdio::piped())
330-
.stdout(Stdio::piped())
331-
.spawn()
332-
.expect("spawn debug binary");
333-
334-
cmd.stdin
335-
.as_mut()
336-
.expect("child stdin")
337-
.write_all(b"subject\nb o d y")
338-
.expect("write to child stdin");
339-
340-
let output = cmd.wait_with_output().expect("run debug binary");
353+
fn arg_width_1_wraps_body_at_width_1_and_exits_successfully() {
354+
let cmd = target_binary_with_width("1");
355+
let output = run_debug_binary_with_input(cmd, b"subject\nb o d y");
341356

342-
assert!(output.status.success());
357+
assert_cmd_success(&output);
343358

344359
let out = String::from_utf8_lossy(&output.stdout);
345360
assert_eq!(
@@ -352,32 +367,15 @@ y
352367
",
353368
out
354369
);
355-
356-
let err = String::from_utf8_lossy(&output.stderr);
357-
assert!(err.is_empty());
358370
}
359371

360372
#[test]
361-
fn width_short_form_is_w() {
362-
use std::io::Write;
363-
373+
fn arg_width_short_form_is_w() {
364374
let mut cmd = target_binary();
365375
cmd.args(&["-w", "1"]);
366-
let mut cmd: Child = cmd
367-
.stdin(Stdio::piped())
368-
.stdout(Stdio::piped())
369-
.spawn()
370-
.expect("spawn debug binary");
371-
372-
cmd.stdin
373-
.as_mut()
374-
.expect("child stdin")
375-
.write_all(b"subject\nb o d y")
376-
.expect("write to child stdin");
376+
let output = run_debug_binary_with_input(cmd, b"subject\nb o d y");
377377

378-
let output = cmd.wait_with_output().expect("run debug binary");
379-
380-
assert!(output.status.success());
378+
assert_cmd_success(&output);
381379

382380
let out = String::from_utf8_lossy(&output.stdout);
383381
assert_eq!(
@@ -390,34 +388,16 @@ y
390388
",
391389
out
392390
);
393-
394-
let err = String::from_utf8_lossy(&output.stderr);
395-
assert!(err.is_empty());
396391
}
397392

398393
#[test]
399-
fn only_last_specified_width_matters() {
400-
use std::io::Write;
401-
394+
fn arg_width_only_last_specified_matters() {
402395
let mut cmd = target_binary_with_width("string");
403396
cmd.args(&["-w", "1"]);
404397
cmd.args(&["-w", "100"]);
398+
let output = run_debug_binary_with_input(cmd, b"subject\nb o d y");
405399

406-
let mut cmd: Child = cmd
407-
.stdin(Stdio::piped())
408-
.stdout(Stdio::piped())
409-
.spawn()
410-
.expect("spawn debug binary");
411-
412-
cmd.stdin
413-
.as_mut()
414-
.expect("child stdin")
415-
.write_all(b"subject\nb o d y")
416-
.expect("write to child stdin");
417-
418-
let output = cmd.wait_with_output().expect("run debug binary");
419-
420-
assert!(output.status.success());
400+
assert_cmd_success(&output);
421401

422402
let out = String::from_utf8_lossy(&output.stdout);
423403
assert_eq!(
@@ -427,9 +407,6 @@ b o d y
427407
",
428408
out
429409
);
430-
431-
let err = String::from_utf8_lossy(&output.stderr);
432-
assert!(err.is_empty());
433410
}
434411

435412
// Sometime after the release of v1.2.0, external changes to Travis CI have
@@ -516,8 +493,5 @@ b o d y
516493
.expect("trigger broken pipe for debug binary");
517494

518495
assert_eq!(141, output.status.code().unwrap());
519-
520-
let err = String::from_utf8_lossy(&output.stderr);
521-
assert!(err.is_empty());
522496
}
523497
}

0 commit comments

Comments
 (0)