Skip to content

Fix process module tests to run on Windows #38797

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 8, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 67 additions & 18 deletions src/libstd/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,11 +260,18 @@ impl fmt::Debug for ChildStderr {
/// ```
/// use std::process::Command;
///
/// let output = Command::new("sh")
/// .arg("-c")
/// .arg("echo hello")
/// .output()
/// .expect("failed to execute process");
/// let output = if cfg!(target_os = "windows") {
/// Command::new("cmd")
/// .args(&["/C", "echo hello"])
/// .output()
/// .expect("failed to execute process")
/// } else {
/// Command::new("sh")
/// .arg("-c")
/// .arg("echo hello")
/// .output()
/// .expect("failed to execute process")
/// };
///
/// let hello = output.stdout;
/// ```
Expand Down Expand Up @@ -925,7 +932,11 @@ mod tests {
#[test]
#[cfg_attr(target_os = "android", ignore)]
fn smoke() {
let p = Command::new("true").spawn();
let p = if cfg!(target_os = "windows") {
Command::new("cmd").args(&["/C", "exit 0"]).spawn()
} else {
Command::new("true").spawn()
};
assert!(p.is_ok());
let mut p = p.unwrap();
assert!(p.wait().unwrap().success());
Expand All @@ -943,7 +954,11 @@ mod tests {
#[test]
#[cfg_attr(target_os = "android", ignore)]
fn exit_reported_right() {
let p = Command::new("false").spawn();
let p = if cfg!(target_os = "windows") {
Command::new("cmd").args(&["/C", "exit 1"]).spawn()
} else {
Command::new("false").spawn()
};
assert!(p.is_ok());
let mut p = p.unwrap();
assert!(p.wait().unwrap().code() == Some(1));
Expand Down Expand Up @@ -982,9 +997,15 @@ mod tests {
#[test]
#[cfg_attr(target_os = "android", ignore)]
fn stdout_works() {
let mut cmd = Command::new("echo");
cmd.arg("foobar").stdout(Stdio::piped());
assert_eq!(run_output(cmd), "foobar\n");
if cfg!(target_os = "windows") {
let mut cmd = Command::new("cmd");
cmd.args(&["/C", "echo foobar"]).stdout(Stdio::piped());
assert_eq!(run_output(cmd), "foobar\r\n");
} else {
let mut cmd = Command::new("echo");
cmd.arg("foobar").stdout(Stdio::piped());
assert_eq!(run_output(cmd), "foobar\n");
}
}

#[test]
Expand Down Expand Up @@ -1044,10 +1065,18 @@ mod tests {
#[test]
#[cfg_attr(target_os = "android", ignore)]
fn test_process_status() {
let mut status = Command::new("false").status().unwrap();
let mut status = if cfg!(target_os = "windows") {
Command::new("cmd").args(&["/C", "exit 1"]).status().unwrap()
} else {
Command::new("false").status().unwrap()
};
assert!(status.code() == Some(1));

status = Command::new("true").status().unwrap();
status = if cfg!(target_os = "windows") {
Command::new("cmd").args(&["/C", "exit 0"]).status().unwrap()
} else {
Command::new("true").status().unwrap()
};
assert!(status.success());
}

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

assert!(status.success());
Expand All @@ -1075,7 +1108,11 @@ mod tests {
#[cfg_attr(target_os = "android", ignore)]
fn test_process_output_error() {
let Output {status, stdout, stderr}
= Command::new("mkdir").arg(".").output().unwrap();
= if cfg!(target_os = "windows") {
Command::new("cmd").args(&["/C", "mkdir ."]).output().unwrap()
} else {
Command::new("mkdir").arg(".").output().unwrap()
};

assert!(status.code() == Some(1));
assert_eq!(stdout, Vec::new());
Expand All @@ -1085,23 +1122,35 @@ mod tests {
#[test]
#[cfg_attr(target_os = "android", ignore)]
fn test_finish_once() {
let mut prog = Command::new("false").spawn().unwrap();
let mut prog = if cfg!(target_os = "windows") {
Command::new("cmd").args(&["/C", "exit 1"]).spawn().unwrap()
} else {
Command::new("false").spawn().unwrap()
};
assert!(prog.wait().unwrap().code() == Some(1));
}

#[test]
#[cfg_attr(target_os = "android", ignore)]
fn test_finish_twice() {
let mut prog = Command::new("false").spawn().unwrap();
let mut prog = if cfg!(target_os = "windows") {
Command::new("cmd").args(&["/C", "exit 1"]).spawn().unwrap()
} else {
Command::new("false").spawn().unwrap()
};
assert!(prog.wait().unwrap().code() == Some(1));
assert!(prog.wait().unwrap().code() == Some(1));
}

#[test]
#[cfg_attr(target_os = "android", ignore)]
fn test_wait_with_output_once() {
let prog = Command::new("echo").arg("hello").stdout(Stdio::piped())
.spawn().unwrap();
let prog = if cfg!(target_os = "windows") {
Command::new("cmd").args(&["/C", "echo hello"]).stdout(Stdio::piped()).spawn().unwrap()
} else {
Command::new("echo").arg("hello").stdout(Stdio::piped()).spawn().unwrap()
};

let Output {status, stdout, stderr} = prog.wait_with_output().unwrap();
let output_str = str::from_utf8(&stdout).unwrap();

Expand Down