Skip to content

Commit 79faeaf

Browse files
committed
Windows command files support
1 parent 7242531 commit 79faeaf

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

src/lib.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,9 +1049,23 @@ impl<'a> Cmd<'a> {
10491049
}
10501050

10511051
fn to_command(&self) -> Command {
1052-
let mut res = Command::new(&self.data.prog);
1053-
res.current_dir(self.shell.current_dir());
1054-
res.args(&self.data.args);
1052+
let mut res = if cfg!(windows) {
1053+
// On windows have to use "cmd /c" workaround to allow batch (command) files
1054+
let mut res = Command::new("cmd");
1055+
res.current_dir(self.shell.current_dir());
1056+
res.args(
1057+
[OsStr::new("/c"), self.data.prog.as_os_str()]
1058+
.iter()
1059+
.map(|it| *it)
1060+
.chain(self.data.args.iter().map(|it| it.as_os_str())),
1061+
);
1062+
res
1063+
} else {
1064+
let mut res = Command::new(&self.data.prog);
1065+
res.current_dir(self.shell.current_dir());
1066+
res.args(&self.data.args);
1067+
res
1068+
};
10551069

10561070
for (key, val) in &*self.shell.env.borrow() {
10571071
res.env(key, val);

tests/windows.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#![cfg(windows)]
2+
3+
use xshell::{cmd, Shell};
4+
5+
#[test]
6+
fn echo() {
7+
let sh = Shell::new().unwrap();
8+
9+
let res = cmd!(sh, "echo test").read().unwrap();
10+
assert_eq!(res, "test");
11+
}
12+
13+
#[test]
14+
fn npm() {
15+
let sh = Shell::new().unwrap();
16+
17+
if cmd!(sh, "where npm.cmd").read().is_ok() {
18+
let script_shell = cmd!(sh, "npm get shell").read().unwrap();
19+
assert!(script_shell.ends_with(".exe"))
20+
}
21+
}

0 commit comments

Comments
 (0)