diff options
| author | bors <bors@rust-lang.org> | 2017-01-08 13:40:46 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2017-01-08 13:40:46 +0000 |
| commit | e350c44e0c43d9c7981d502e27cf149287d924cb (patch) | |
| tree | 9f05380c2fefb305791ac84ab34a9ab8d60f20a3 | |
| parent | cbf88730e755d099c854f84dd0f1990490bf0088 (diff) | |
| parent | 7152bce19203a2260147ae1ed84caa1d6de2ec40 (diff) | |
| download | rust-e350c44e0c43d9c7981d502e27cf149287d924cb.tar.gz rust-e350c44e0c43d9c7981d502e27cf149287d924cb.zip | |
Auto merge of #38797 - abhijeetbhagat:master, r=petrochenkov
Fix process module tests to run on Windows Fixes #38565 r? @retep998
| -rw-r--r-- | src/libstd/process.rs | 85 |
1 files changed, 67 insertions, 18 deletions
diff --git a/src/libstd/process.rs b/src/libstd/process.rs index e15c37aaf24..1a11d7ad9d7 100644 --- a/src/libstd/process.rs +++ b/src/libstd/process.rs @@ -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; /// ``` @@ -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()); @@ -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)); @@ -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] @@ -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()); } @@ -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()); @@ -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()); @@ -1085,14 +1122,22 @@ 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)); } @@ -1100,8 +1145,12 @@ mod tests { #[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(); |
