]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/command-before-exec.rs
New upstream version 1.23.0+dfsg1
[rustc.git] / src / test / run-pass / command-before-exec.rs
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.
4 //
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.
10
11 // ignore-windows - this is a unix-specific test
12 // ignore-emscripten no processes
13
14 #![feature(process_exec, libc)]
15
16 extern crate libc;
17
18 use std::env;
19 use std::io::Error;
20 use std::os::unix::process::CommandExt;
21 use std::process::Command;
22 use std::sync::Arc;
23 use std::sync::atomic::{AtomicUsize, Ordering};
24
25 fn main() {
26 if let Some(arg) = env::args().skip(1).next() {
27 match &arg[..] {
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(), "/"),
32 "empty" => {}
33 _ => panic!("unknown argument: {}", arg),
34 }
35 return
36 }
37
38 let me = env::current_exe().unwrap();
39
40 let output = Command::new(&me).arg("test1").before_exec(|| {
41 println!("hello");
42 Ok(())
43 }).output().unwrap();
44 assert!(output.status.success());
45 assert!(output.stderr.is_empty());
46 assert_eq!(output.stdout, b"hello\nhello2\n");
47
48 let output = Command::new(&me).arg("test2").before_exec(|| {
49 env::set_var("FOO", "BAR");
50 Ok(())
51 }).output().unwrap();
52 assert!(output.status.success());
53 assert!(output.stderr.is_empty());
54 assert!(output.stdout.is_empty());
55
56 let output = Command::new(&me).arg("test3").before_exec(|| {
57 env::set_current_dir("/").unwrap();
58 Ok(())
59 }).output().unwrap();
60 assert!(output.status.success());
61 assert!(output.stderr.is_empty());
62 assert!(output.stdout.is_empty());
63
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));
68
69 let pid = unsafe { libc::getpid() };
70 assert!(pid >= 0);
71 let output = Command::new(&me).arg("empty").before_exec(move || {
72 let child = unsafe { libc::getpid() };
73 assert!(child >= 0);
74 assert!(pid != child);
75 Ok(())
76 }).output().unwrap();
77 assert!(output.status.success());
78 assert!(output.stderr.is_empty());
79 assert!(output.stdout.is_empty());
80
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);
85 Ok(())
86 }).output().unwrap();
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);
91 }