Skip to content

Commit

Permalink
Add tests for options::Shell
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd committed Oct 4, 2021
1 parent fe9c89c commit a6a85d9
Showing 1 changed file with 48 additions and 13 deletions.
61 changes: 48 additions & 13 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub const DEFAULT_SHELL: &str = "sh";
pub const DEFAULT_SHELL: &str = "cmd.exe";

/// Shell to use for executing benchmarked commands
#[derive(Debug)]
pub enum Shell {
/// Default shell command
Default(&'static str),
Expand All @@ -28,18 +29,7 @@ impl fmt::Display for Shell {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Shell::Default(cmd) => write!(f, "{}", cmd),
Shell::Custom(cmdline) => {
let mut first = true;
for s in cmdline.iter() {
if first {
first = false;
write!(f, "{}", s)?;
} else {
write!(f, " {}", s)?;
}
}
Ok(())
}
Shell::Custom(cmdline) => write!(f, "{}", shell_words::join(cmdline)),
}
}
}
Expand All @@ -48,7 +38,7 @@ impl Shell {
/// Parse given string as shell command line
pub fn parse<'a>(s: &str) -> Result<Self, OptionsError<'a>> {
let v = shell_words::split(s.as_ref()).map_err(OptionsError::ShellParseError)?;
if v.is_empty() {
if v.is_empty() || v[0].is_empty() {
return Err(OptionsError::EmptyShell);
}
Ok(Shell::Custom(v))
Expand Down Expand Up @@ -165,3 +155,48 @@ impl Default for HyperfineOptions {
}
}
}

#[test]
fn test_shell_default_command() {
let shell = Shell::default();

let s = format!("{}", shell);
assert_eq!(&s, DEFAULT_SHELL);

let cmd = shell.command();
// Command::get_program is not yet available in stable channel.
// https://doc.rust-lang.org/std/process/struct.Command.html#method.get_program
let s = format!("{:?}", cmd);
assert_eq!(s, format!("\"{}\"", DEFAULT_SHELL));
}

#[test]
fn test_shell_parse_command() {
let shell = Shell::parse("shell -x 'aaa bbb'").unwrap();

let s = format!("{}", shell);
assert_eq!(&s, "shell -x 'aaa bbb'");

let cmd = shell.command();
// Command::get_program and Command::args are not yet available in stable channel.
// https://doc.rust-lang.org/std/process/struct.Command.html#method.get_program
let s = format!("{:?}", cmd);
assert_eq!(&s, r#""shell" "-x" "aaa bbb""#);

// Error cases

match Shell::parse("shell 'foo").unwrap_err() {
OptionsError::ShellParseError(_) => { /* ok */ }
e => assert!(false, "Unexpected error: {}", e),
}

match Shell::parse("").unwrap_err() {
OptionsError::EmptyShell => { /* ok */ }
e => assert!(false, "Unexpected error: {}", e),
}

match Shell::parse("''").unwrap_err() {
OptionsError::EmptyShell => { /* ok */ }
e => assert!(false, "Unexpected error: {}", e),
}
}

0 comments on commit a6a85d9

Please sign in to comment.