Skip to content

Commit

Permalink
Windows command files support
Browse files Browse the repository at this point in the history
  • Loading branch information
vsrs committed Jul 19, 2024
1 parent 7242531 commit 6d3dae9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
21 changes: 21 additions & 0 deletions tests/windows.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use xshell::{cmd, Shell};

#[test]
#[cfg(windows)]
fn echo() {
let sh = Shell::new().unwrap();

let res = cmd!(sh, "echo test").read().unwrap();
assert_eq!(res, "test");
}

#[test]
#[cfg(windows)]
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"))
}
}

0 comments on commit 6d3dae9

Please sign in to comment.