]> git.lizzy.rs Git - rust.git/blob - src/compiletest/procsrv.rs
Fix --disable-rpath and tests
[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::os;
12 use std::str;
13 use std::io::process::{ProcessExit, Command, Process, ProcessOutput};
14 use std::dynamic_lib::DynamicLibrary;
15
16 fn target_env(lib_path: &str, aux_path: Option<&str>) -> Vec<(String, String)> {
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, Path::new(p)),
22         None => {}
23     }
24     path.insert(0, Path::new(lib_path));
25
26     // Remove the previous dylib search path var
27     let var = DynamicLibrary::envvar();
28     let mut env: Vec<(String,String)> = os::env();
29     match env.iter().position(|&(ref k, _)| k.as_slice() == var) {
30         Some(i) => { env.remove(i); }
31         None => {}
32     }
33
34     // Add the new dylib search path var
35     let newpath = DynamicLibrary::create_path(path.as_slice());
36     let newpath = str::from_utf8(newpath.as_slice()).unwrap().to_string();
37     env.push((var.to_string(), newpath));
38     return env;
39 }
40
41 pub struct Result {pub status: ProcessExit, pub out: String, pub err: String}
42
43 pub fn run(lib_path: &str,
44            prog: &str,
45            aux_path: Option<&str>,
46            args: &[String],
47            env: Vec<(String, String)> ,
48            input: Option<String>) -> Option<Result> {
49
50     let env = env.clone().append(target_env(lib_path, aux_path).as_slice());
51     match Command::new(prog).args(args).env(env.as_slice()).spawn() {
52         Ok(mut process) => {
53             for input in input.iter() {
54                 process.stdin.get_mut_ref().write(input.as_bytes()).unwrap();
55             }
56             let ProcessOutput { status, output, error } =
57                 process.wait_with_output().unwrap();
58
59             Some(Result {
60                 status: status,
61                 out: str::from_utf8(output.as_slice()).unwrap().to_string(),
62                 err: str::from_utf8(error.as_slice()).unwrap().to_string()
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<Process> {
75
76     let env = env.clone().append(target_env(lib_path, aux_path).as_slice());
77     match Command::new(prog).args(args).env(env.as_slice()).spawn() {
78         Ok(mut process) => {
79             for input in input.iter() {
80                 process.stdin.get_mut_ref().write(input.as_bytes()).unwrap();
81             }
82
83             Some(process)
84         },
85         Err(..) => None
86     }
87 }