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