]> git.lizzy.rs Git - rust.git/blob - src/compiletest/procsrv.rs
Doc says to avoid mixing allocator instead of forbiding it
[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::io::process::{ProcessExit, Command, Process, ProcessOutput};
12 use std::dynamic_lib::DynamicLibrary;
13
14 fn add_target_env(cmd: &mut Command, lib_path: &str, aux_path: Option<&str>) {
15     // Need to be sure to put both the lib_path and the aux path in the dylib
16     // search path for the child.
17     let mut path = DynamicLibrary::search_path();
18     match aux_path {
19         Some(p) => path.insert(0, Path::new(p)),
20         None => {}
21     }
22     path.insert(0, Path::new(lib_path));
23
24     // Add the new dylib search path var
25     let var = DynamicLibrary::envvar();
26     let newpath = DynamicLibrary::create_path(path.as_slice());
27     let newpath = String::from_utf8(newpath).unwrap();
28     cmd.env(var.to_string(), newpath);
29 }
30
31 pub struct Result {pub status: ProcessExit, pub out: String, pub err: String}
32
33 pub fn run(lib_path: &str,
34            prog: &str,
35            aux_path: Option<&str>,
36            args: &[String],
37            env: Vec<(String, String)> ,
38            input: Option<String>) -> Option<Result> {
39
40     let mut cmd = Command::new(prog);
41     cmd.args(args);
42     add_target_env(&mut cmd, lib_path, aux_path);
43     for (key, val) in env.move_iter() {
44         cmd.env(key, val);
45     }
46
47     match cmd.spawn() {
48         Ok(mut process) => {
49             for input in input.iter() {
50                 process.stdin.as_mut().unwrap().write(input.as_bytes()).unwrap();
51             }
52             let ProcessOutput { status, output, error } =
53                 process.wait_with_output().unwrap();
54
55             Some(Result {
56                 status: status,
57                 out: String::from_utf8(output).unwrap(),
58                 err: String::from_utf8(error).unwrap()
59             })
60         },
61         Err(..) => None
62     }
63 }
64
65 pub fn run_background(lib_path: &str,
66            prog: &str,
67            aux_path: Option<&str>,
68            args: &[String],
69            env: Vec<(String, String)> ,
70            input: Option<String>) -> Option<Process> {
71
72     let mut cmd = Command::new(prog);
73     cmd.args(args);
74     add_target_env(&mut cmd, lib_path, aux_path);
75     for (key, val) in env.move_iter() {
76         cmd.env(key, val);
77     }
78
79     match cmd.spawn() {
80         Ok(mut process) => {
81             for input in input.iter() {
82                 process.stdin.as_mut().unwrap().write(input.as_bytes()).unwrap();
83             }
84
85             Some(process)
86         },
87         Err(..) => None
88     }
89 }