Skip to content
Myr

Add tests for options::Shell

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by rhysd · committed by David Peter · parent e2fdace · Browse files · 1 file+48 added−13 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+48 added−13 removed

  • other:+48 added−13 removed

1 file+48 added−13 removed

modified:src/options.rs+48 added-13 removed
9 unmodified lines
10#[cfg(windows)]11pub const DEFAULT_SHELL: &str = "cmd.exe";1213/// Shell to use for executing benchmarked commands14#[derive(Debug)]15pub enum Shell {16 /// Default shell command17 Default(&'static str),18 /// Custom shell command specified via --shell
9 unmodified lines
28impl fmt::Display for Shell {29 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {30 match self {31 Shell::Default(cmd) => write!(f, "{}", cmd),31 Shell::Custom(cmdline) => {32 let mut first = true;33 for s in cmdline.iter() {34 if first {35 first = false;36 write!(f, "{}", s)?;37 } else {38 write!(f, " {}", s)?;39 }40 }41 Ok(())42 }32 Shell::Custom(cmdline) => write!(f, "{}", shell_words::join(cmdline)),33 }34 }35}3637impl Shell {38 /// Parse given string as shell command line39 pub fn parse<'a>(s: &str) -> Result<Self, OptionsError<'a>> {40 let v = shell_words::split(s.as_ref()).map_err(OptionsError::ShellParseError)?;51 if v.is_empty() {41 if v.is_empty() || v[0].is_empty() {42 return Err(OptionsError::EmptyShell);43 }44 Ok(Shell::Custom(v))45 }
108 unmodified lines
154 time_unit: None,155 }156 }157}158159#[test]160fn test_shell_default_command() {161 let shell = Shell::default();162163 let s = format!("{}", shell);164 assert_eq!(&s, DEFAULT_SHELL);165166 let cmd = shell.command();167 // Command::get_program is not yet available in stable channel.168 // https://doc.rust-lang.org/std/process/struct.Command.html#method.get_program169 let s = format!("{:?}", cmd);170 assert_eq!(s, format!("\"{}\"", DEFAULT_SHELL));171}172173#[test]174fn test_shell_parse_command() {175 let shell = Shell::parse("shell -x 'aaa bbb'").unwrap();176177 let s = format!("{}", shell);178 assert_eq!(&s, "shell -x 'aaa bbb'");179180 let cmd = shell.command();181 // Command::get_program and Command::args are not yet available in stable channel.182 // https://doc.rust-lang.org/std/process/struct.Command.html#method.get_program183 let s = format!("{:?}", cmd);184 assert_eq!(&s, r#""shell" "-x" "aaa bbb""#);185186 // Error cases187188 match Shell::parse("shell 'foo").unwrap_err() {189 OptionsError::ShellParseError(_) => { /* ok */ }190 e => assert!(false, "Unexpected error: {}", e),191 }192193 match Shell::parse("").unwrap_err() {194 OptionsError::EmptyShell => { /* ok */ }195 e => assert!(false, "Unexpected error: {}", e),196 }197198 match Shell::parse("''").unwrap_err() {199 OptionsError::EmptyShell => { /* ok */ }200 e => assert!(false, "Unexpected error: {}", e),201 }202}