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