]> git.proxmox.com Git - rustc.git/blame - src/test/run-pass/command-before-exec.rs
New upstream version 1.25.0+dfsg1
[rustc.git] / src / test / run-pass / command-before-exec.rs
CommitLineData
7453a54e
SL
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
2c00a5a8 12// ignore-cloudabi no processes
abe05a73 13// ignore-emscripten no processes
7453a54e
SL
14
15#![feature(process_exec, libc)]
16
17extern crate libc;
18
19use std::env;
20use std::io::Error;
21use std::os::unix::process::CommandExt;
22use std::process::Command;
23use std::sync::Arc;
24use std::sync::atomic::{AtomicUsize, Ordering};
25
26fn main() {
27 if let Some(arg) = env::args().skip(1).next() {
28 match &arg[..] {
29 "test1" => println!("hello2"),
30 "test2" => assert_eq!(env::var("FOO").unwrap(), "BAR"),
31 "test3" => assert_eq!(env::current_dir().unwrap()
32 .to_str().unwrap(), "/"),
33 "empty" => {}
34 _ => panic!("unknown argument: {}", arg),
35 }
36 return
37 }
38
39 let me = env::current_exe().unwrap();
40
41 let output = Command::new(&me).arg("test1").before_exec(|| {
42 println!("hello");
43 Ok(())
44 }).output().unwrap();
45 assert!(output.status.success());
46 assert!(output.stderr.is_empty());
47 assert_eq!(output.stdout, b"hello\nhello2\n");
48
49 let output = Command::new(&me).arg("test2").before_exec(|| {
50 env::set_var("FOO", "BAR");
51 Ok(())
52 }).output().unwrap();
53 assert!(output.status.success());
54 assert!(output.stderr.is_empty());
55 assert!(output.stdout.is_empty());
56
57 let output = Command::new(&me).arg("test3").before_exec(|| {
58 env::set_current_dir("/").unwrap();
59 Ok(())
60 }).output().unwrap();
61 assert!(output.status.success());
62 assert!(output.stderr.is_empty());
63 assert!(output.stdout.is_empty());
64
65 let output = Command::new(&me).arg("bad").before_exec(|| {
66 Err(Error::from_raw_os_error(102))
a7813a04 67 }).output().unwrap_err();
7453a54e
SL
68 assert_eq!(output.raw_os_error(), Some(102));
69
70 let pid = unsafe { libc::getpid() };
71 assert!(pid >= 0);
72 let output = Command::new(&me).arg("empty").before_exec(move || {
73 let child = unsafe { libc::getpid() };
74 assert!(child >= 0);
75 assert!(pid != child);
76 Ok(())
77 }).output().unwrap();
78 assert!(output.status.success());
79 assert!(output.stderr.is_empty());
80 assert!(output.stdout.is_empty());
81
82 let mem = Arc::new(AtomicUsize::new(0));
83 let mem2 = mem.clone();
84 let output = Command::new(&me).arg("empty").before_exec(move || {
85 assert_eq!(mem2.fetch_add(1, Ordering::SeqCst), 0);
86 Ok(())
87 }).output().unwrap();
88 assert!(output.status.success());
89 assert!(output.stderr.is_empty());
90 assert!(output.stdout.is_empty());
91 assert_eq!(mem.load(Ordering::SeqCst), 0);
92}