Rollup merge of #91151 - name1e5s:chore/process_test, r=m-ou-se

Fix test in std::process on android

closes #10380
This commit is contained in:
Guillaume Gomez 2021-11-24 22:56:38 +01:00 committed by GitHub
commit a81f3610ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,15 +4,23 @@ use super::{Command, Output, Stdio};
use crate::io::ErrorKind; use crate::io::ErrorKind;
use crate::str; 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] #[test]
#[cfg_attr(any(target_os = "vxworks", target_os = "android"), ignore)] #[cfg_attr(any(target_os = "vxworks"), ignore)]
fn smoke() { fn smoke() {
let p = if cfg!(target_os = "windows") { let p = if cfg!(target_os = "windows") {
Command::new("cmd").args(&["/C", "exit 0"]).spawn() Command::new("cmd").args(&["/C", "exit 0"]).spawn()
} else { } else {
Command::new("true").spawn() shell_cmd().arg("-c").arg("true").spawn()
}; };
assert!(p.is_ok()); assert!(p.is_ok());
let mut p = p.unwrap(); let mut p = p.unwrap();
@ -29,12 +37,12 @@ fn smoke_failure() {
} }
#[test] #[test]
#[cfg_attr(any(target_os = "vxworks", target_os = "android"), ignore)] #[cfg_attr(any(target_os = "vxworks"), ignore)]
fn exit_reported_right() { fn exit_reported_right() {
let p = if cfg!(target_os = "windows") { let p = if cfg!(target_os = "windows") {
Command::new("cmd").args(&["/C", "exit 1"]).spawn() Command::new("cmd").args(&["/C", "exit 1"]).spawn()
} else { } else {
Command::new("false").spawn() shell_cmd().arg("-c").arg("false").spawn()
}; };
assert!(p.is_ok()); assert!(p.is_ok());
let mut p = p.unwrap(); let mut p = p.unwrap();
@ -44,12 +52,11 @@ fn exit_reported_right() {
#[test] #[test]
#[cfg(unix)] #[cfg(unix)]
#[cfg_attr(any(target_os = "vxworks", target_os = "android"), ignore)] #[cfg_attr(any(target_os = "vxworks"), ignore)]
fn signal_reported_right() { fn signal_reported_right() {
use crate::os::unix::process::ExitStatusExt; use crate::os::unix::process::ExitStatusExt;
let mut p = let mut p = shell_cmd().arg("-c").arg("read a").stdin(Stdio::piped()).spawn().unwrap();
Command::new("/bin/sh").arg("-c").arg("read a").stdin(Stdio::piped()).spawn().unwrap();
p.kill().unwrap(); p.kill().unwrap();
match p.wait().unwrap().signal() { match p.wait().unwrap().signal() {
Some(9) => {} Some(9) => {}
@ -69,31 +76,31 @@ pub fn run_output(mut cmd: Command) -> String {
} }
#[test] #[test]
#[cfg_attr(any(target_os = "vxworks", target_os = "android"), ignore)] #[cfg_attr(any(target_os = "vxworks"), ignore)]
fn stdout_works() { fn stdout_works() {
if cfg!(target_os = "windows") { if cfg!(target_os = "windows") {
let mut cmd = Command::new("cmd"); let mut cmd = Command::new("cmd");
cmd.args(&["/C", "echo foobar"]).stdout(Stdio::piped()); cmd.args(&["/C", "echo foobar"]).stdout(Stdio::piped());
assert_eq!(run_output(cmd), "foobar\r\n"); assert_eq!(run_output(cmd), "foobar\r\n");
} else { } else {
let mut cmd = Command::new("echo"); let mut cmd = shell_cmd();
cmd.arg("foobar").stdout(Stdio::piped()); cmd.arg("-c").arg("echo foobar").stdout(Stdio::piped());
assert_eq!(run_output(cmd), "foobar\n"); assert_eq!(run_output(cmd), "foobar\n");
} }
} }
#[test] #[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() { 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()); cmd.arg("-c").arg("pwd").current_dir("/").stdout(Stdio::piped());
assert_eq!(run_output(cmd), "/\n"); assert_eq!(run_output(cmd), "/\n");
} }
#[test] #[test]
#[cfg_attr(any(windows, target_os = "android", target_os = "vxworks"), ignore)] #[cfg_attr(any(windows, target_os = "vxworks"), ignore)]
fn stdin_works() { fn stdin_works() {
let mut p = Command::new("/bin/sh") let mut p = shell_cmd()
.arg("-c") .arg("-c")
.arg("read line; echo $line") .arg("read line; echo $line")
.stdin(Stdio::piped()) .stdin(Stdio::piped())
@ -109,19 +116,19 @@ fn stdin_works() {
} }
#[test] #[test]
#[cfg_attr(any(target_os = "vxworks", target_os = "android"), ignore)] #[cfg_attr(any(target_os = "vxworks"), ignore)]
fn test_process_status() { fn test_process_status() {
let mut status = if cfg!(target_os = "windows") { let mut status = if cfg!(target_os = "windows") {
Command::new("cmd").args(&["/C", "exit 1"]).status().unwrap() Command::new("cmd").args(&["/C", "exit 1"]).status().unwrap()
} else { } else {
Command::new("false").status().unwrap() shell_cmd().arg("-c").arg("false").status().unwrap()
}; };
assert!(status.code() == Some(1)); assert!(status.code() == Some(1));
status = if cfg!(target_os = "windows") { status = if cfg!(target_os = "windows") {
Command::new("cmd").args(&["/C", "exit 0"]).status().unwrap() Command::new("cmd").args(&["/C", "exit 0"]).status().unwrap()
} else { } else {
Command::new("true").status().unwrap() shell_cmd().arg("-c").arg("true").status().unwrap()
}; };
assert!(status.success()); assert!(status.success());
} }
@ -135,12 +142,12 @@ fn test_process_output_fail_to_start() {
} }
#[test] #[test]
#[cfg_attr(any(target_os = "vxworks", target_os = "android"), ignore)] #[cfg_attr(any(target_os = "vxworks"), ignore)]
fn test_process_output_output() { fn test_process_output_output() {
let Output { status, stdout, stderr } = if cfg!(target_os = "windows") { let Output { status, stdout, stderr } = if cfg!(target_os = "windows") {
Command::new("cmd").args(&["/C", "echo hello"]).output().unwrap() Command::new("cmd").args(&["/C", "echo hello"]).output().unwrap()
} else { } 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(); let output_str = str::from_utf8(&stdout).unwrap();
@ -150,7 +157,7 @@ fn test_process_output_output() {
} }
#[test] #[test]
#[cfg_attr(any(target_os = "vxworks", target_os = "android"), ignore)] #[cfg_attr(any(target_os = "vxworks"), ignore)]
fn test_process_output_error() { fn test_process_output_error() {
let Output { status, stdout, stderr } = if cfg!(target_os = "windows") { let Output { status, stdout, stderr } = if cfg!(target_os = "windows") {
Command::new("cmd").args(&["/C", "mkdir ."]).output().unwrap() Command::new("cmd").args(&["/C", "mkdir ."]).output().unwrap()
@ -158,41 +165,42 @@ fn test_process_output_error() {
Command::new("mkdir").arg("./").output().unwrap() 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_eq!(stdout, Vec::new());
assert!(!stderr.is_empty()); assert!(!stderr.is_empty());
} }
#[test] #[test]
#[cfg_attr(any(target_os = "vxworks", target_os = "android"), ignore)] #[cfg_attr(any(target_os = "vxworks"), ignore)]
fn test_finish_once() { fn test_finish_once() {
let mut prog = if cfg!(target_os = "windows") { let mut prog = if cfg!(target_os = "windows") {
Command::new("cmd").args(&["/C", "exit 1"]).spawn().unwrap() Command::new("cmd").args(&["/C", "exit 1"]).spawn().unwrap()
} else { } 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] #[test]
#[cfg_attr(any(target_os = "vxworks", target_os = "android"), ignore)] #[cfg_attr(any(target_os = "vxworks"), ignore)]
fn test_finish_twice() { fn test_finish_twice() {
let mut prog = if cfg!(target_os = "windows") { let mut prog = if cfg!(target_os = "windows") {
Command::new("cmd").args(&["/C", "exit 1"]).spawn().unwrap() Command::new("cmd").args(&["/C", "exit 1"]).spawn().unwrap()
} else { } 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));
assert!(prog.wait().unwrap().code() == Some(1)); assert!(prog.wait().unwrap().code() == Some(1));
} }
#[test] #[test]
#[cfg_attr(any(target_os = "vxworks", target_os = "android"), ignore)] #[cfg_attr(any(target_os = "vxworks"), ignore)]
fn test_wait_with_output_once() { fn test_wait_with_output_once() {
let prog = if cfg!(target_os = "windows") { let prog = if cfg!(target_os = "windows") {
Command::new("cmd").args(&["/C", "echo hello"]).stdout(Stdio::piped()).spawn().unwrap() Command::new("cmd").args(&["/C", "echo hello"]).stdout(Stdio::piped()).spawn().unwrap()
} else { } 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(); let Output { status, stdout, stderr } = prog.wait_with_output().unwrap();