]> git.proxmox.com Git - rustc.git/blob - src/test/ui/command/command-pre-exec.rs
New upstream version 1.52.0~beta.3+dfsg1
[rustc.git] / src / test / ui / command / command-pre-exec.rs
1 // run-pass
2
3 #![allow(stable_features)]
4 // ignore-windows - this is a unix-specific test
5 // ignore-emscripten no processes
6 // ignore-sgx no processes
7 #![feature(process_exec, rustc_private)]
8
9 extern crate libc;
10
11 use std::env;
12 use std::io::Error;
13 use std::os::unix::process::CommandExt;
14 use std::process::Command;
15 use std::sync::atomic::{AtomicUsize, Ordering};
16 use std::sync::Arc;
17
18 fn main() {
19 if let Some(arg) = env::args().nth(1) {
20 match &arg[..] {
21 "test1" => println!("hello2"),
22 "test2" => assert_eq!(env::var("FOO").unwrap(), "BAR"),
23 "test3" => assert_eq!(env::current_dir().unwrap().to_str().unwrap(), "/"),
24 "empty" => {}
25 _ => panic!("unknown argument: {}", arg),
26 }
27 return;
28 }
29
30 let me = env::current_exe().unwrap();
31
32 let output = unsafe {
33 Command::new(&me)
34 .arg("test1")
35 .pre_exec(|| {
36 println!("hello");
37 Ok(())
38 })
39 .output()
40 .unwrap()
41 };
42 assert!(output.status.success());
43 assert!(output.stderr.is_empty());
44 assert_eq!(output.stdout, b"hello\nhello2\n");
45
46 let output = unsafe {
47 Command::new(&me)
48 .arg("test3")
49 .pre_exec(|| {
50 env::set_current_dir("/").unwrap();
51 Ok(())
52 })
53 .output()
54 .unwrap()
55 };
56 assert!(output.status.success());
57 assert!(output.stderr.is_empty());
58 assert!(output.stdout.is_empty());
59
60 let output = unsafe {
61 Command::new(&me)
62 .arg("bad")
63 .pre_exec(|| Err(Error::from_raw_os_error(102)))
64 .output()
65 .unwrap_err()
66 };
67 assert_eq!(output.raw_os_error(), Some(102));
68
69 let pid = unsafe { libc::getpid() };
70 assert!(pid >= 0);
71 let output = unsafe {
72 Command::new(&me)
73 .arg("empty")
74 .pre_exec(move || {
75 let child = libc::getpid();
76 assert!(child >= 0);
77 assert!(pid != child);
78 Ok(())
79 })
80 .output()
81 .unwrap()
82 };
83 assert!(output.status.success());
84 assert!(output.stderr.is_empty());
85 assert!(output.stdout.is_empty());
86
87 let mem = Arc::new(AtomicUsize::new(0));
88 let mem2 = mem.clone();
89 let output = unsafe {
90 Command::new(&me)
91 .arg("empty")
92 .pre_exec(move || {
93 assert_eq!(mem2.fetch_add(1, Ordering::SeqCst), 0);
94 Ok(())
95 })
96 .output()
97 .unwrap()
98 };
99 assert!(output.status.success());
100 assert!(output.stderr.is_empty());
101 assert!(output.stdout.is_empty());
102 assert_eq!(mem.load(Ordering::SeqCst), 0);
103 }