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
Provenance
- Commit
1b7d139- Tree
6486255- Author
rhysd <lin90162@yahoo.co.jp>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
e2fdace
1 file changed+48 added−13 removed
modified:src/options.rs+48 added-13 removed
9 unmodified lines10 #[cfg(windows)]11 pub const DEFAULT_SHELL: &str = "cmd.exe";12 13 /// Shell to use for executing benchmarked commands14+#[derive(Debug)]15 pub enum Shell {16 /// Default shell command17 Default(&'static str),18 /// Custom shell command specified via --shell9 unmodified lines28 impl 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 }36 37 impl 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 lines154 time_unit: None,155 }156 }157 }158+159+#[test]160+fn test_shell_default_command() {161+ let shell = Shell::default();162+163+ let s = format!("{}", shell);164+ assert_eq!(&s, DEFAULT_SHELL);165+166+ 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+}172+173+#[test]174+fn test_shell_parse_command() {175+ let shell = Shell::parse("shell -x 'aaa bbb'").unwrap();176+177+ let s = format!("{}", shell);178+ assert_eq!(&s, "shell -x 'aaa bbb'");179+180+ 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""#);185+186+ // Error cases187+188+ match Shell::parse("shell 'foo").unwrap_err() {189+ OptionsError::ShellParseError(_) => { /* ok */ }190+ e => assert!(false, "Unexpected error: {}", e),191+ }192+193+ match Shell::parse("").unwrap_err() {194+ OptionsError::EmptyShell => { /* ok */ }195+ e => assert!(false, "Unexpected error: {}", e),196+ }197+198+ match Shell::parse("''").unwrap_err() {199+ OptionsError::EmptyShell => { /* ok */ }200+ e => assert!(false, "Unexpected error: {}", e),201+ }202+}