Skip to content

Commit

Permalink
Rollup merge of #91151 - name1e5s:chore/process_test, r=m-ou-se
Browse files Browse the repository at this point in the history
Fix test in std::process on android

closes #10380
  • Loading branch information
GuillaumeGomez authored Nov 24, 2021
2 parents 9390b50 + 08a500f commit a81f361
Showing 1 changed file with 36 additions and 28 deletions.
64 changes: 36 additions & 28 deletions library/std/src/process/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,23 @@ use super::{Command, Output, Stdio};
use crate::io::ErrorKind;
use crate::str;

// FIXME(#10380) these tests should not all be ignored on android.
#[cfg(target_os = "android")]
fn shell_cmd() -> Command {
Command::new("/system/bin/sh")
}

#[cfg(not(target_os = "android"))]
fn shell_cmd() -> Command {
Command::new("/bin/sh")
}

#[test]
#[cfg_attr(any(target_os = "vxworks", target_os = "android"), ignore)]
#[cfg_attr(any(target_os = "vxworks"), ignore)]
fn smoke() {
let p = if cfg!(target_os = "windows") {
Command::new("cmd").args(&["/C", "exit 0"]).spawn()
} else {
Command::new("true").spawn()
shell_cmd().arg("-c").arg("true").spawn()
};
assert!(p.is_ok());
let mut p = p.unwrap();
Expand All @@ -29,12 +37,12 @@ fn smoke_failure() {
}

#[test]
#[cfg_attr(any(target_os = "vxworks", target_os = "android"), ignore)]
#[cfg_attr(any(target_os = "vxworks"), ignore)]
fn exit_reported_right() {
let p = if cfg!(target_os = "windows") {
Command::new("cmd").args(&["/C", "exit 1"]).spawn()
} else {
Command::new("false").spawn()
shell_cmd().arg("-c").arg("false").spawn()
};
assert!(p.is_ok());
let mut p = p.unwrap();
Expand All @@ -44,12 +52,11 @@ fn exit_reported_right() {

#[test]
#[cfg(unix)]
#[cfg_attr(any(target_os = "vxworks", target_os = "android"), ignore)]
#[cfg_attr(any(target_os = "vxworks"), ignore)]
fn signal_reported_right() {
use crate::os::unix::process::ExitStatusExt;

let mut p =
Command::new("/bin/sh").arg("-c").arg("read a").stdin(Stdio::piped()).spawn().unwrap();
let mut p = shell_cmd().arg("-c").arg("read a").stdin(Stdio::piped()).spawn().unwrap();
p.kill().unwrap();
match p.wait().unwrap().signal() {
Some(9) => {}
Expand All @@ -69,31 +76,31 @@ pub fn run_output(mut cmd: Command) -> String {
}

#[test]
#[cfg_attr(any(target_os = "vxworks", target_os = "android"), ignore)]
#[cfg_attr(any(target_os = "vxworks"), ignore)]
fn stdout_works() {
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());
let mut cmd = shell_cmd();
cmd.arg("-c").arg("echo foobar").stdout(Stdio::piped());
assert_eq!(run_output(cmd), "foobar\n");
}
}

#[test]
#[cfg_attr(any(windows, target_os = "android", target_os = "vxworks"), ignore)]
#[cfg_attr(any(windows, target_os = "vxworks"), ignore)]
fn set_current_dir_works() {
let mut cmd = Command::new("/bin/sh");
let mut cmd = shell_cmd();
cmd.arg("-c").arg("pwd").current_dir("/").stdout(Stdio::piped());
assert_eq!(run_output(cmd), "/\n");
}

#[test]
#[cfg_attr(any(windows, target_os = "android", target_os = "vxworks"), ignore)]
#[cfg_attr(any(windows, target_os = "vxworks"), ignore)]
fn stdin_works() {
let mut p = Command::new("/bin/sh")
let mut p = shell_cmd()
.arg("-c")
.arg("read line; echo $line")
.stdin(Stdio::piped())
Expand All @@ -109,19 +116,19 @@ fn stdin_works() {
}

#[test]
#[cfg_attr(any(target_os = "vxworks", target_os = "android"), ignore)]
#[cfg_attr(any(target_os = "vxworks"), ignore)]
fn test_process_status() {
let mut status = if cfg!(target_os = "windows") {
Command::new("cmd").args(&["/C", "exit 1"]).status().unwrap()
} else {
Command::new("false").status().unwrap()
shell_cmd().arg("-c").arg("false").status().unwrap()
};
assert!(status.code() == Some(1));

status = if cfg!(target_os = "windows") {
Command::new("cmd").args(&["/C", "exit 0"]).status().unwrap()
} else {
Command::new("true").status().unwrap()
shell_cmd().arg("-c").arg("true").status().unwrap()
};
assert!(status.success());
}
Expand All @@ -135,12 +142,12 @@ fn test_process_output_fail_to_start() {
}

#[test]
#[cfg_attr(any(target_os = "vxworks", target_os = "android"), ignore)]
#[cfg_attr(any(target_os = "vxworks"), ignore)]
fn test_process_output_output() {
let Output { status, stdout, stderr } = if cfg!(target_os = "windows") {
Command::new("cmd").args(&["/C", "echo hello"]).output().unwrap()
} else {
Command::new("echo").arg("hello").output().unwrap()
shell_cmd().arg("-c").arg("echo hello").output().unwrap()
};
let output_str = str::from_utf8(&stdout).unwrap();

Expand All @@ -150,49 +157,50 @@ fn test_process_output_output() {
}

#[test]
#[cfg_attr(any(target_os = "vxworks", target_os = "android"), ignore)]
#[cfg_attr(any(target_os = "vxworks"), ignore)]
fn test_process_output_error() {
let Output { status, stdout, stderr } = 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!(status.code().is_some());
assert!(status.code() != Some(0));
assert_eq!(stdout, Vec::new());
assert!(!stderr.is_empty());
}

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

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

#[test]
#[cfg_attr(any(target_os = "vxworks", target_os = "android"), ignore)]
#[cfg_attr(any(target_os = "vxworks"), ignore)]
fn test_wait_with_output_once() {
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()
shell_cmd().arg("-c").arg("echo hello").stdout(Stdio::piped()).spawn().unwrap()
};

let Output { status, stdout, stderr } = prog.wait_with_output().unwrap();
Expand Down

0 comments on commit a81f361

Please sign in to comment.