1 // run-pass
2 
3 #![allow(stable_features)]
4 // ignore-windows - this is a unix-specific test
5 // ignore-pretty issue #37199
6 // ignore-emscripten no processes
7 // ignore-sgx no processes
8 
9 #![feature(process_exec)]
10 
11 use std::env;
12 use std::os::unix::process::CommandExt;
13 use std::process::Command;
14 
main()15 fn main() {
16     let mut args = env::args();
17     let me = args.next().unwrap();
18 
19     if let Some(arg) = args.next() {
20         match &arg[..] {
21             "test1" => println!("passed"),
22 
23             "exec-test1" => {
24                 let err = Command::new(&me).arg("test1").exec();
25                 panic!("failed to spawn: {}", err);
26             }
27 
28             "exec-test2" => {
29                 Command::new("/path/to/nowhere").exec();
30                 println!("passed");
31             }
32 
33             "exec-test3" => {
34                 Command::new(&me).arg("bad\0").exec();
35                 println!("passed");
36             }
37 
38             "exec-test4" => {
39                 Command::new(&me).current_dir("/path/to/nowhere").exec();
40                 println!("passed");
41             }
42 
43             "exec-test5" => {
44                 env::set_var("VARIABLE", "ABC");
45                 Command::new("definitely-not-a-real-binary").env("VARIABLE", "XYZ").exec();
46                 assert_eq!(env::var("VARIABLE").unwrap(), "ABC");
47                 println!("passed");
48             }
49 
50             "exec-test6" => {
51                 let err = Command::new("echo").arg("passed").env_clear().exec();
52                 panic!("failed to spawn: {}", err);
53             }
54 
55             "exec-test7" => {
56                 let err = Command::new("echo").arg("passed").env_remove("PATH").exec();
57                 panic!("failed to spawn: {}", err);
58             }
59 
60             _ => panic!("unknown argument: {}", arg),
61         }
62         return
63     }
64 
65     let output = Command::new(&me).arg("exec-test1").output().unwrap();
66     assert!(output.status.success());
67     assert!(output.stderr.is_empty());
68     assert_eq!(output.stdout, b"passed\n");
69 
70     let output = Command::new(&me).arg("exec-test2").output().unwrap();
71     assert!(output.status.success());
72     assert!(output.stderr.is_empty());
73     assert_eq!(output.stdout, b"passed\n");
74 
75     let output = Command::new(&me).arg("exec-test3").output().unwrap();
76     assert!(output.status.success());
77     assert!(output.stderr.is_empty());
78     assert_eq!(output.stdout, b"passed\n");
79 
80     let output = Command::new(&me).arg("exec-test4").output().unwrap();
81     assert!(output.status.success());
82     assert!(output.stderr.is_empty());
83     assert_eq!(output.stdout, b"passed\n");
84 
85     let output = Command::new(&me).arg("exec-test5").output().unwrap();
86     assert!(output.status.success());
87     assert!(output.stderr.is_empty());
88     assert_eq!(output.stdout, b"passed\n");
89 
90     if cfg!(target_os = "linux") {
91         let output = Command::new(&me).arg("exec-test6").output().unwrap();
92         println!("{:?}", output);
93         assert!(output.status.success());
94         assert!(output.stderr.is_empty());
95         assert_eq!(output.stdout, b"passed\n");
96 
97         let output = Command::new(&me).arg("exec-test7").output().unwrap();
98         println!("{:?}", output);
99         assert!(output.status.success());
100         assert!(output.stderr.is_empty());
101         assert_eq!(output.stdout, b"passed\n");
102     }
103 }
104