From 1b7d139f21f6c73f937bd18b040b4fce56ab5f73 Mon Sep 17 00:00:00 2001 From: rhysd Date: Mon, 4 Oct 2021 22:49:37 +0900 Subject: [PATCH] Add tests for options::Shell --- src/options.rs | 61 +++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 48 insertions(+), 13 deletions(-) diff --git a/src/options.rs b/src/options.rs index 228d3c2..f3eafc3 100644 --- a/src/options.rs +++ b/src/options.rs @@ -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), @@ -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)), } } } @@ -48,7 +38,7 @@ impl Shell { /// Parse given string as shell command line pub fn parse<'a>(s: &str) -> Result> { 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)) @@ -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), + } +}