1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 // ignore-windows - this is a unix-specific test
12 // ignore-emscripten no processes
14 #![feature(process_exec, libc)]
20 use std::os::unix::process::CommandExt;
21 use std::process::Command;
23 use std::sync::atomic::{AtomicUsize, Ordering};
26 if let Some(arg) = env::args().skip(1).next() {
28 "test1" => println!("hello2"),
29 "test2" => assert_eq!(env::var("FOO").unwrap(), "BAR"),
30 "test3" => assert_eq!(env::current_dir().unwrap()
31 .to_str().unwrap(), "/"),
33 _ => panic!("unknown argument: {}", arg),
38 let me = env::current_exe().unwrap();
40 let output = Command::new(&me).arg("test1").before_exec(|| {
44 assert!(output.status.success());
45 assert!(output.stderr.is_empty());
46 assert_eq!(output.stdout, b"hello\nhello2\n");
48 let output = Command::new(&me).arg("test2").before_exec(|| {
49 env::set_var("FOO", "BAR");
52 assert!(output.status.success());
53 assert!(output.stderr.is_empty());
54 assert!(output.stdout.is_empty());
56 let output = Command::new(&me).arg("test3").before_exec(|| {
57 env::set_current_dir("/").unwrap();
60 assert!(output.status.success());
61 assert!(output.stderr.is_empty());
62 assert!(output.stdout.is_empty());
64 let output = Command::new(&me).arg("bad").before_exec(|| {
65 Err(Error::from_raw_os_error(102))
66 }).output().unwrap_err();
67 assert_eq!(output.raw_os_error(), Some(102));
69 let pid = unsafe { libc::getpid() };
71 let output = Command::new(&me).arg("empty").before_exec(move || {
72 let child = unsafe { libc::getpid() };
74 assert!(pid != child);
77 assert!(output.status.success());
78 assert!(output.stderr.is_empty());
79 assert!(output.stdout.is_empty());
81 let mem = Arc::new(AtomicUsize::new(0));
82 let mem2 = mem.clone();
83 let output = Command::new(&me).arg("empty").before_exec(move || {
84 assert_eq!(mem2.fetch_add(1, Ordering::SeqCst), 0);
87 assert!(output.status.success());
88 assert!(output.stderr.is_empty());
89 assert!(output.stdout.is_empty());
90 assert_eq!(mem.load(Ordering::SeqCst), 0);