]> git.lizzy.rs Git - rust.git/blob - src/compiletest/procsrv.rs
rollup merge of #27618: dotdash/drop_fixes
[rust.git] / src / compiletest / procsrv.rs
1 // Copyright 2012 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 use std::dynamic_lib::DynamicLibrary;
12 use std::io::prelude::*;
13 use std::path::PathBuf;
14 use std::process::{ExitStatus, Command, Child, Output, Stdio};
15
16 fn add_target_env(cmd: &mut Command, lib_path: &str, aux_path: Option<&str>) {
17     // Need to be sure to put both the lib_path and the aux path in the dylib
18     // search path for the child.
19     let mut path = DynamicLibrary::search_path();
20     match aux_path {
21         Some(p) => path.insert(0, PathBuf::from(p)),
22         None => {}
23     }
24     path.insert(0, PathBuf::from(lib_path));
25
26     // Add the new dylib search path var
27     let var = DynamicLibrary::envvar();
28     let newpath = DynamicLibrary::create_path(&path);
29     cmd.env(var, newpath);
30 }
31
32 pub struct Result {pub status: ExitStatus, pub out: String, pub err: String}
33
34 pub fn run(lib_path: &str,
35            prog: &str,
36            aux_path: Option<&str>,
37            args: &[String],
38            env: Vec<(String, String)> ,
39            input: Option<String>) -> Option<Result> {
40
41     let mut cmd = Command::new(prog);
42     cmd.args(args)
43        .stdin(Stdio::piped())
44        .stdout(Stdio::piped())
45        .stderr(Stdio::piped());
46     add_target_env(&mut cmd, lib_path, aux_path);
47     for (key, val) in env {
48         cmd.env(&key, &val);
49     }
50
51     match cmd.spawn() {
52         Ok(mut process) => {
53             if let Some(input) = input {
54                 process.stdin.as_mut().unwrap().write_all(input.as_bytes()).unwrap();
55             }
56             let Output { status, stdout, stderr } =
57                 process.wait_with_output().unwrap();
58
59             Some(Result {
60                 status: status,
61                 out: String::from_utf8(stdout).unwrap(),
62                 err: String::from_utf8(stderr).unwrap()
63             })
64         },
65         Err(..) => None
66     }
67 }
68
69 pub fn run_background(lib_path: &str,
70            prog: &str,
71            aux_path: Option<&str>,
72            args: &[String],
73            env: Vec<(String, String)> ,
74            input: Option<String>) -> Option<Child> {
75
76     let mut cmd = Command::new(prog);
77     cmd.args(args)
78        .stdin(Stdio::piped())
79        .stdout(Stdio::piped())
80        .stderr(Stdio::piped());
81     add_target_env(&mut cmd, lib_path, aux_path);
82     for (key, val) in env {
83         cmd.env(&key, &val);
84     }
85
86     match cmd.spawn() {
87         Ok(mut process) => {
88             if let Some(input) = input {
89                 process.stdin.as_mut().unwrap().write_all(input.as_bytes()).unwrap();
90             }
91
92             Some(process)
93         },
94         Err(..) => None
95     }
96 }