Skip to content

Commit 185de41

Browse files
committed
feat: Support OsStr, Path, etc for args
Fixes assert-rs#59
1 parent f94a996 commit 185de41

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};
@@ -11,7 +12,7 @@ use std::vec::Vec;
1112
/// Assertions for a specific command.
1213
#[derive(Debug)]
1314
pub struct Assert {
14-
cmd: Vec<String>,
15+
cmd: Vec<OsString>,
1516
env: Environment,
1617
current_dir: Option<PathBuf>,
1718
expect_success: Option<bool>,
@@ -28,7 +29,7 @@ impl default::Default for Assert {
2829
Assert {
2930
cmd: vec!["cargo", "run", "--quiet", "--"]
3031
.into_iter()
31-
.map(String::from)
32+
.map(OsString::from)
3233
.collect(),
3334
env: Environment::inherit(),
3435
current_dir: None,
@@ -51,11 +52,17 @@ impl Assert {
5152
/// Run a specific binary of the current crate.
5253
///
5354
/// Defaults to asserting _successful_ execution.
54-
pub fn cargo_binary(name: &str) -> Self {
55+
pub fn cargo_binary<S: AsRef<OsStr>>(name: S) -> Self {
5556
Assert {
56-
cmd: vec!["cargo", "run", "--quiet", "--bin", name, "--"]
57-
.into_iter()
58-
.map(String::from)
57+
cmd: vec![
58+
OsStr::new("cargo"),
59+
OsStr::new("run"),
60+
OsStr::new("--quiet"),
61+
OsStr::new("--bin"),
62+
name.as_ref(),
63+
OsStr::new("--"),
64+
].into_iter()
65+
.map(OsString::from)
5966
.collect(),
6067
..Self::default()
6168
}
@@ -73,9 +80,9 @@ impl Assert {
7380
/// assert_cli::Assert::command(&["echo", "1337"])
7481
/// .unwrap();
7582
/// ```
76-
pub fn command(cmd: &[&str]) -> Self {
83+
pub fn command<S: AsRef<OsStr>>(cmd: &[S]) -> Self {
7784
Assert {
78-
cmd: cmd.into_iter().cloned().map(String::from).collect(),
85+
cmd: cmd.into_iter().map(OsString::from).collect(),
7986
..Self::default()
8087
}
8188
}
@@ -93,8 +100,8 @@ impl Assert {
93100
/// .unwrap();
94101
///
95102
/// ```
96-
pub fn with_args(mut self, args: &[&str]) -> Self {
97-
self.cmd.extend(args.into_iter().cloned().map(String::from));
103+
pub fn with_args<S: AsRef<OsStr>>(mut self, args: &[S]) -> Self {
104+
self.cmd.extend(args.into_iter().map(OsString::from));
98105
self
99106
}
100107

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)