Skip to content

Commit e350c44

Browse files
committed
Auto merge of #38797 - abhijeetbhagat:master, r=petrochenkov
Fix process module tests to run on Windows Fixes #38565 r? @retep998
2 parents cbf8873 + 7152bce commit e350c44

File tree

1 file changed

+67
-18
lines changed

1 file changed

+67
-18
lines changed

src/libstd/process.rs

+67-18
Original file line numberDiff line numberDiff line change
@@ -260,11 +260,18 @@ impl fmt::Debug for ChildStderr {
260260
/// ```
261261
/// use std::process::Command;
262262
///
263-
/// let output = Command::new("sh")
264-
/// .arg("-c")
265-
/// .arg("echo hello")
266-
/// .output()
267-
/// .expect("failed to execute process");
263+
/// let output = if cfg!(target_os = "windows") {
264+
/// Command::new("cmd")
265+
/// .args(&["/C", "echo hello"])
266+
/// .output()
267+
/// .expect("failed to execute process")
268+
/// } else {
269+
/// Command::new("sh")
270+
/// .arg("-c")
271+
/// .arg("echo hello")
272+
/// .output()
273+
/// .expect("failed to execute process")
274+
/// };
268275
///
269276
/// let hello = output.stdout;
270277
/// ```
@@ -925,7 +932,11 @@ mod tests {
925932
#[test]
926933
#[cfg_attr(target_os = "android", ignore)]
927934
fn smoke() {
928-
let p = Command::new("true").spawn();
935+
let p = if cfg!(target_os = "windows") {
936+
Command::new("cmd").args(&["/C", "exit 0"]).spawn()
937+
} else {
938+
Command::new("true").spawn()
939+
};
929940
assert!(p.is_ok());
930941
let mut p = p.unwrap();
931942
assert!(p.wait().unwrap().success());
@@ -943,7 +954,11 @@ mod tests {
943954
#[test]
944955
#[cfg_attr(target_os = "android", ignore)]
945956
fn exit_reported_right() {
946-
let p = Command::new("false").spawn();
957+
let p = if cfg!(target_os = "windows") {
958+
Command::new("cmd").args(&["/C", "exit 1"]).spawn()
959+
} else {
960+
Command::new("false").spawn()
961+
};
947962
assert!(p.is_ok());
948963
let mut p = p.unwrap();
949964
assert!(p.wait().unwrap().code() == Some(1));
@@ -982,9 +997,15 @@ mod tests {
982997
#[test]
983998
#[cfg_attr(target_os = "android", ignore)]
984999
fn stdout_works() {
985-
let mut cmd = Command::new("echo");
986-
cmd.arg("foobar").stdout(Stdio::piped());
987-
assert_eq!(run_output(cmd), "foobar\n");
1000+
if cfg!(target_os = "windows") {
1001+
let mut cmd = Command::new("cmd");
1002+
cmd.args(&["/C", "echo foobar"]).stdout(Stdio::piped());
1003+
assert_eq!(run_output(cmd), "foobar\r\n");
1004+
} else {
1005+
let mut cmd = Command::new("echo");
1006+
cmd.arg("foobar").stdout(Stdio::piped());
1007+
assert_eq!(run_output(cmd), "foobar\n");
1008+
}
9881009
}
9891010

9901011
#[test]
@@ -1044,10 +1065,18 @@ mod tests {
10441065
#[test]
10451066
#[cfg_attr(target_os = "android", ignore)]
10461067
fn test_process_status() {
1047-
let mut status = Command::new("false").status().unwrap();
1068+
let mut status = if cfg!(target_os = "windows") {
1069+
Command::new("cmd").args(&["/C", "exit 1"]).status().unwrap()
1070+
} else {
1071+
Command::new("false").status().unwrap()
1072+
};
10481073
assert!(status.code() == Some(1));
10491074

1050-
status = Command::new("true").status().unwrap();
1075+
status = if cfg!(target_os = "windows") {
1076+
Command::new("cmd").args(&["/C", "exit 0"]).status().unwrap()
1077+
} else {
1078+
Command::new("true").status().unwrap()
1079+
};
10511080
assert!(status.success());
10521081
}
10531082

@@ -1063,7 +1092,11 @@ mod tests {
10631092
#[cfg_attr(target_os = "android", ignore)]
10641093
fn test_process_output_output() {
10651094
let Output {status, stdout, stderr}
1066-
= Command::new("echo").arg("hello").output().unwrap();
1095+
= if cfg!(target_os = "windows") {
1096+
Command::new("cmd").args(&["/C", "echo hello"]).output().unwrap()
1097+
} else {
1098+
Command::new("echo").arg("hello").output().unwrap()
1099+
};
10671100
let output_str = str::from_utf8(&stdout).unwrap();
10681101

10691102
assert!(status.success());
@@ -1075,7 +1108,11 @@ mod tests {
10751108
#[cfg_attr(target_os = "android", ignore)]
10761109
fn test_process_output_error() {
10771110
let Output {status, stdout, stderr}
1078-
= Command::new("mkdir").arg(".").output().unwrap();
1111+
= if cfg!(target_os = "windows") {
1112+
Command::new("cmd").args(&["/C", "mkdir ."]).output().unwrap()
1113+
} else {
1114+
Command::new("mkdir").arg(".").output().unwrap()
1115+
};
10791116

10801117
assert!(status.code() == Some(1));
10811118
assert_eq!(stdout, Vec::new());
@@ -1085,23 +1122,35 @@ mod tests {
10851122
#[test]
10861123
#[cfg_attr(target_os = "android", ignore)]
10871124
fn test_finish_once() {
1088-
let mut prog = Command::new("false").spawn().unwrap();
1125+
let mut prog = if cfg!(target_os = "windows") {
1126+
Command::new("cmd").args(&["/C", "exit 1"]).spawn().unwrap()
1127+
} else {
1128+
Command::new("false").spawn().unwrap()
1129+
};
10891130
assert!(prog.wait().unwrap().code() == Some(1));
10901131
}
10911132

10921133
#[test]
10931134
#[cfg_attr(target_os = "android", ignore)]
10941135
fn test_finish_twice() {
1095-
let mut prog = Command::new("false").spawn().unwrap();
1136+
let mut prog = if cfg!(target_os = "windows") {
1137+
Command::new("cmd").args(&["/C", "exit 1"]).spawn().unwrap()
1138+
} else {
1139+
Command::new("false").spawn().unwrap()
1140+
};
10961141
assert!(prog.wait().unwrap().code() == Some(1));
10971142
assert!(prog.wait().unwrap().code() == Some(1));
10981143
}
10991144

11001145
#[test]
11011146
#[cfg_attr(target_os = "android", ignore)]
11021147
fn test_wait_with_output_once() {
1103-
let prog = Command::new("echo").arg("hello").stdout(Stdio::piped())
1104-
.spawn().unwrap();
1148+
let prog = if cfg!(target_os = "windows") {
1149+
Command::new("cmd").args(&["/C", "echo hello"]).stdout(Stdio::piped()).spawn().unwrap()
1150+
} else {
1151+
Command::new("echo").arg("hello").stdout(Stdio::piped()).spawn().unwrap()
1152+
};
1153+
11051154
let Output {status, stdout, stderr} = prog.wait_with_output().unwrap();
11061155
let output_str = str::from_utf8(&stdout).unwrap();
11071156

0 commit comments

Comments
 (0)