Skip to content

Commit d6e912c

Browse files
committed
feat(test-support): Make multi-argument strings avaialble to snapbox
This is something the existing test infrastructure supports, so I figured I'd make it mirror it for snapbox. I'm mixed. - It reads more like what a user would type, making it easier to run a test locally or take a manual test case and automate it - It can make it harder to parse the arguments when scanning tests - Without using a crate like `shlex`, the syntax support is unclear
1 parent 83d4440 commit d6e912c

File tree

3 files changed

+106
-134
lines changed

3 files changed

+106
-134
lines changed

crates/cargo-test-support/src/lib.rs

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ pub mod registry;
6060
pub mod tools;
6161

6262
pub mod prelude {
63+
pub use crate::ArgLine;
6364
pub use crate::CargoCommand;
6465
pub use crate::ChannelChanger;
6566
pub use crate::TestEnv;
@@ -391,7 +392,7 @@ impl Project {
391392
pub fn cargo(&self, cmd: &str) -> Execs {
392393
let mut execs = self.process(&cargo_exe());
393394
if let Some(ref mut p) = execs.process_builder {
394-
split_and_add_args(p, cmd);
395+
p.arg_line(cmd);
395396
}
396397
execs
397398
}
@@ -1243,28 +1244,46 @@ impl CargoCommand for snapbox::cmd::Command {
12431244
}
12441245
}
12451246

1246-
fn split_and_add_args(p: &mut ProcessBuilder, s: &str) {
1247-
for mut arg in s.split_whitespace() {
1248-
if (arg.starts_with('"') && arg.ends_with('"'))
1249-
|| (arg.starts_with('\'') && arg.ends_with('\''))
1250-
{
1251-
arg = &arg[1..(arg.len() - 1).max(1)];
1252-
} else if arg.contains(&['"', '\''][..]) {
1253-
panic!("shell-style argument parsing is not supported")
1247+
/// Add a list of arguments as a line
1248+
pub trait ArgLine: Sized {
1249+
fn arg_line(mut self, s: &str) -> Self {
1250+
for mut arg in s.split_whitespace() {
1251+
if (arg.starts_with('"') && arg.ends_with('"'))
1252+
|| (arg.starts_with('\'') && arg.ends_with('\''))
1253+
{
1254+
arg = &arg[1..(arg.len() - 1).max(1)];
1255+
} else if arg.contains(&['"', '\''][..]) {
1256+
panic!("shell-style argument parsing is not supported")
1257+
}
1258+
self = self.arg(arg);
12541259
}
1255-
p.arg(arg);
1260+
self
1261+
}
1262+
1263+
fn arg<S: AsRef<std::ffi::OsStr>>(self, s: S) -> Self;
1264+
}
1265+
1266+
impl ArgLine for &mut ProcessBuilder {
1267+
fn arg<S: AsRef<std::ffi::OsStr>>(self, s: S) -> Self {
1268+
self.arg(s)
1269+
}
1270+
}
1271+
1272+
impl ArgLine for snapbox::cmd::Command {
1273+
fn arg<S: AsRef<std::ffi::OsStr>>(self, s: S) -> Self {
1274+
self.arg(s)
12561275
}
12571276
}
12581277

12591278
pub fn cargo_process(s: &str) -> Execs {
12601279
let mut p = process(&cargo_exe());
1261-
split_and_add_args(&mut p, s);
1280+
p.arg_line(s);
12621281
execs().with_process_builder(p)
12631282
}
12641283

12651284
pub fn git_process(s: &str) -> ProcessBuilder {
12661285
let mut p = process("git");
1267-
split_and_add_args(&mut p, s);
1286+
p.arg_line(s);
12681287
p
12691288
}
12701289

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: invalid version requirement `invalid version string`
1+
error: invalid version requirement `invalid-version-string`
22

33
Caused by:
44
unexpected character 'i' while parsing major version number

0 commit comments

Comments
 (0)