Skip to content
This repository was archived by the owner on Dec 29, 2021. It is now read-only.

Commit 3bc9787

Browse files
committed
Merge #73
73: feat: Support OsStr, Path, etc for args r=epage a=epage Fixes #59
2 parents 4b1f1b0 + 185de41 commit 3bc9787

File tree

3 files changed

+37
-19
lines changed

3 files changed

+37
-19
lines changed

src/assert.rs

+17-10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use error_chain::ChainedError;
33
use errors::*;
44
use output::{OutputAssertion, OutputKind};
55
use std::default;
6+
use std::ffi::{OsStr, OsString};
67
use std::io::Write;
78
use std::path::PathBuf;
89
use std::process::{Command, Stdio};
@@ -12,7 +13,7 @@ use std::vec::Vec;
1213
#[derive(Debug)]
1314
#[must_use]
1415
pub struct Assert {
15-
cmd: Vec<String>,
16+
cmd: Vec<OsString>,
1617
env: Environment,
1718
current_dir: Option<PathBuf>,
1819
expect_success: Option<bool>,
@@ -29,7 +30,7 @@ impl default::Default for Assert {
2930
Assert {
3031
cmd: vec!["cargo", "run", "--quiet", "--"]
3132
.into_iter()
32-
.map(String::from)
33+
.map(OsString::from)
3334
.collect(),
3435
env: Environment::inherit(),
3536
current_dir: None,
@@ -52,11 +53,17 @@ impl Assert {
5253
/// Run a specific binary of the current crate.
5354
///
5455
/// Defaults to asserting _successful_ execution.
55-
pub fn cargo_binary(name: &str) -> Self {
56+
pub fn cargo_binary<S: AsRef<OsStr>>(name: S) -> Self {
5657
Assert {
57-
cmd: vec!["cargo", "run", "--quiet", "--bin", name, "--"]
58-
.into_iter()
59-
.map(String::from)
58+
cmd: vec![
59+
OsStr::new("cargo"),
60+
OsStr::new("run"),
61+
OsStr::new("--quiet"),
62+
OsStr::new("--bin"),
63+
name.as_ref(),
64+
OsStr::new("--"),
65+
].into_iter()
66+
.map(OsString::from)
6067
.collect(),
6168
..Self::default()
6269
}
@@ -74,9 +81,9 @@ impl Assert {
7481
/// assert_cli::Assert::command(&["echo", "1337"])
7582
/// .unwrap();
7683
/// ```
77-
pub fn command(cmd: &[&str]) -> Self {
84+
pub fn command<S: AsRef<OsStr>>(cmd: &[S]) -> Self {
7885
Assert {
79-
cmd: cmd.into_iter().cloned().map(String::from).collect(),
86+
cmd: cmd.into_iter().map(OsString::from).collect(),
8087
..Self::default()
8188
}
8289
}
@@ -94,8 +101,8 @@ impl Assert {
94101
/// .unwrap();
95102
///
96103
/// ```
97-
pub fn with_args(mut self, args: &[&str]) -> Self {
98-
self.cmd.extend(args.into_iter().cloned().map(String::from));
104+
pub fn with_args<S: AsRef<OsStr>>(mut self, args: &[S]) -> Self {
105+
self.cmd.extend(args.into_iter().map(OsString::from));
99106
self
100107
}
101108

src/errors.rs

+18-8
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,43 @@
1+
use output;
2+
use std::ffi::OsString;
3+
14
const ERROR_PREFIX: &'static str = "CLI assertion failed";
25

6+
fn format_cmd(cmd: &[OsString]) -> String {
7+
let result: Vec<String> = cmd.iter()
8+
.map(|s| s.to_string_lossy().into_owned())
9+
.collect();
10+
result.join(" ")
11+
}
12+
313
error_chain! {
414
foreign_links {
515
Io(::std::io::Error);
616
Fmt(::std::fmt::Error);
717
}
818
errors {
9-
SpawnFailed(cmd: Vec<String>) {
19+
SpawnFailed(cmd: Vec<OsString>) {
1020
description("Spawn failed")
1121
display(
1222
"{}: (command `{}` failed to run)",
1323
ERROR_PREFIX,
14-
cmd.join(" "),
24+
format_cmd(cmd),
1525
)
1626
}
17-
StatusMismatch(cmd: Vec<String>, expected: bool, out: String, err: String) {
27+
StatusMismatch(cmd: Vec<OsString>, expected: bool, out: String, err: String) {
1828
description("Wrong status")
1929
display(
2030
"{}: (command `{}` expected to {})\nstatus={}\nstdout=```{}```\nstderr=```{}```",
2131
ERROR_PREFIX,
22-
cmd.join(" "),
32+
format_cmd(cmd),
2333
expected = if *expected { "succeed" } else { "fail" },
2434
got = if *expected { "failed" } else { "succeeded" },
2535
out = out,
2636
err = err,
2737
)
2838
}
2939
ExitCodeMismatch(
30-
cmd: Vec<String>,
40+
cmd: Vec<OsString>,
3141
expected: Option<i32>,
3242
got: Option<i32>,
3343
out: String,
@@ -40,18 +50,18 @@ error_chain! {
4050
stdout=```{stdout}```\n\
4151
stderr=```{stderr}```",
4252
prefix=ERROR_PREFIX,
43-
cmd=cmd.join(" "),
53+
cmd=format_cmd(cmd),
4454
expected=expected,
4555
code=got,
4656
stdout=out,
4757
stderr=err,
4858
)
4959
}
50-
OutputMismatch(cmd: Vec<String>, output_err: ::output::Error, kind: ::output::OutputKind) {
60+
OutputMismatch(cmd: Vec<OsString>, output_err: output::Error, kind: output::OutputKind) {
5161
description("Output was not as expected")
5262
display(
5363
"{}: `{}` {:?} mismatch: {}",
54-
ERROR_PREFIX, cmd.join(" "), kind, output_err,
64+
ERROR_PREFIX, format_cmd(cmd), kind, output_err,
5565
)
5666
}
5767

src/output.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use self::errors::*;
22
pub use self::errors::{Error, ErrorKind};
33
use diff;
44
use difference::Changeset;
5+
use std::ffi::OsString;
56
use std::process::Output;
67

78
#[derive(Debug, Clone)]
@@ -49,7 +50,7 @@ impl OutputAssertion {
4950
Ok(())
5051
}
5152

52-
pub fn execute(&self, output: &Output, cmd: &[String]) -> super::errors::Result<()> {
53+
pub fn execute(&self, output: &Output, cmd: &[OsString]) -> super::errors::Result<()> {
5354
let observed = String::from_utf8_lossy(self.kind.select(output));
5455

5556
let result = if self.fuzzy {

0 commit comments

Comments
 (0)