diff --git a/src/lib.rs b/src/lib.rs index 9a7ccda..f9315a9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1049,9 +1049,23 @@ impl<'a> Cmd<'a> { } fn to_command(&self) -> Command { - let mut res = Command::new(&self.data.prog); - res.current_dir(self.shell.current_dir()); - res.args(&self.data.args); + let mut res = if cfg!(windows) { + // On windows have to use "cmd /c" workaround to allow batch (command) files + let mut res = Command::new("cmd"); + res.current_dir(self.shell.current_dir()); + res.args( + [OsStr::new("/c"), self.data.prog.as_os_str()] + .iter() + .map(|it| *it) + .chain(self.data.args.iter().map(|it| it.as_os_str())), + ); + res + } else { + let mut res = Command::new(&self.data.prog); + res.current_dir(self.shell.current_dir()); + res.args(&self.data.args); + res + }; for (key, val) in &*self.shell.env.borrow() { res.env(key, val); diff --git a/tests/windows.rs b/tests/windows.rs new file mode 100644 index 0000000..37e5177 --- /dev/null +++ b/tests/windows.rs @@ -0,0 +1,21 @@ +#![cfg(windows)] + +use xshell::{cmd, Shell}; + +#[test] +fn echo() { + let sh = Shell::new().unwrap(); + + let res = cmd!(sh, "echo test").read().unwrap(); + assert_eq!(res, "test"); +} + +#[test] +fn npm() { + let sh = Shell::new().unwrap(); + + if cmd!(sh, "where npm.cmd").read().is_ok() { + let script_shell = cmd!(sh, "npm get shell").read().unwrap(); + assert!(script_shell.ends_with(".exe")) + } +}