rustfmt the test

This commit is contained in:
Ralf Jung 2019-02-02 11:08:21 +01:00
parent 6c67a7625f
commit 59da97d2b2

View File

@ -2,7 +2,6 @@
// ignore-windows - this is a unix-specific test // ignore-windows - this is a unix-specific test
// ignore-cloudabi no processes // ignore-cloudabi no processes
// ignore-emscripten no processes // ignore-emscripten no processes
#![feature(process_exec, rustc_private)] #![feature(process_exec, rustc_private)]
extern crate libc; extern crate libc;
@ -11,71 +10,104 @@
use std::io::Error; use std::io::Error;
use std::os::unix::process::CommandExt; use std::os::unix::process::CommandExt;
use std::process::Command; use std::process::Command;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
fn main() { fn main() {
if let Some(arg) = env::args().nth(1) { if let Some(arg) = env::args().nth(1) {
match &arg[..] { match &arg[..] {
"test1" => println!("hello2"), "test1" => println!("hello2"),
"test2" => assert_eq!(env::var("FOO").unwrap(), "BAR"), "test2" => assert_eq!(env::var("FOO").unwrap(), "BAR"),
"test3" => assert_eq!(env::current_dir().unwrap() "test3" => assert_eq!(env::current_dir().unwrap().to_str().unwrap(), "/"),
.to_str().unwrap(), "/"),
"empty" => {} "empty" => {}
_ => panic!("unknown argument: {}", arg), _ => panic!("unknown argument: {}", arg),
} }
return return;
} }
let me = env::current_exe().unwrap(); let me = env::current_exe().unwrap();
let output = unsafe { Command::new(&me).arg("test1").pre_exec(|| { let output = unsafe {
println!("hello"); Command::new(&me)
Ok(()) .arg("test1")
}).output().unwrap() }; .pre_exec(|| {
println!("hello");
Ok(())
})
.output()
.unwrap()
};
assert!(output.status.success()); assert!(output.status.success());
assert!(output.stderr.is_empty()); assert!(output.stderr.is_empty());
assert_eq!(output.stdout, b"hello\nhello2\n"); assert_eq!(output.stdout, b"hello\nhello2\n");
let output = unsafe { Command::new(&me).arg("test2").pre_exec(|| { let output = unsafe {
env::set_var("FOO", "BAR"); Command::new(&me)
Ok(()) .arg("test2")
}).output().unwrap() }; .pre_exec(|| {
env::set_var("FOO", "BAR");
Ok(())
})
.output()
.unwrap()
};
assert!(output.status.success()); assert!(output.status.success());
assert!(output.stderr.is_empty()); assert!(output.stderr.is_empty());
assert!(output.stdout.is_empty()); assert!(output.stdout.is_empty());
let output = unsafe { Command::new(&me).arg("test3").pre_exec(|| { let output = unsafe {
env::set_current_dir("/").unwrap(); Command::new(&me)
Ok(()) .arg("test3")
}).output().unwrap() }; .pre_exec(|| {
env::set_current_dir("/").unwrap();
Ok(())
})
.output()
.unwrap()
};
assert!(output.status.success()); assert!(output.status.success());
assert!(output.stderr.is_empty()); assert!(output.stderr.is_empty());
assert!(output.stdout.is_empty()); assert!(output.stdout.is_empty());
let output = unsafe { Command::new(&me).arg("bad").pre_exec(|| { let output = unsafe {
Err(Error::from_raw_os_error(102)) Command::new(&me)
}).output().unwrap_err() }; .arg("bad")
.pre_exec(|| Err(Error::from_raw_os_error(102)))
.output()
.unwrap_err()
};
assert_eq!(output.raw_os_error(), Some(102)); assert_eq!(output.raw_os_error(), Some(102));
let pid = unsafe { libc::getpid() }; let pid = unsafe { libc::getpid() };
assert!(pid >= 0); assert!(pid >= 0);
let output = unsafe { Command::new(&me).arg("empty").pre_exec(move || { let output = unsafe {
let child = libc::getpid(); Command::new(&me)
assert!(child >= 0); .arg("empty")
assert!(pid != child); .pre_exec(move || {
Ok(()) let child = libc::getpid();
}).output().unwrap() }; assert!(child >= 0);
assert!(pid != child);
Ok(())
})
.output()
.unwrap()
};
assert!(output.status.success()); assert!(output.status.success());
assert!(output.stderr.is_empty()); assert!(output.stderr.is_empty());
assert!(output.stdout.is_empty()); assert!(output.stdout.is_empty());
let mem = Arc::new(AtomicUsize::new(0)); let mem = Arc::new(AtomicUsize::new(0));
let mem2 = mem.clone(); let mem2 = mem.clone();
let output = unsafe { Command::new(&me).arg("empty").pre_exec(move || { let output = unsafe {
assert_eq!(mem2.fetch_add(1, Ordering::SeqCst), 0); Command::new(&me)
Ok(()) .arg("empty")
}).output().unwrap() }; .pre_exec(move || {
assert_eq!(mem2.fetch_add(1, Ordering::SeqCst), 0);
Ok(())
})
.output()
.unwrap()
};
assert!(output.status.success()); assert!(output.status.success());
assert!(output.stderr.is_empty()); assert!(output.stderr.is_empty());
assert!(output.stdout.is_empty()); assert!(output.stdout.is_empty());