rust/src/compiletest/procsrv.rs
Aaron Turon 42c4e481cd Stabilize std::path
This commit stabilizes essentially all of the new `std::path` API. The
API itself is changed in a couple of ways (which brings it in closer
alignment with the RFC):

* `.` components are now normalized away, unless they appear at the
  start of a path. This in turn effects the semantics of e.g. asking for
  the file name of `foo/` or `foo/.`, both of which yield `Some("foo")`
  now. This semantics is what the original RFC specified, and is also
  desirable given early experience rolling out the new API.

* The `parent` function now succeeds if, and only if, the path has at
  least one non-root/prefix component. This change affects `pop` as
  well.

* The `Prefix` component now involves a separate `PrefixComponent`
  struct, to better allow for keeping both parsed and unparsed prefix data.

In addition, the `old_path` module is now deprecated.

Closes #23264

[breaking-change]
2015-03-12 16:38:58 -07:00

98 lines
3 KiB
Rust

// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![allow(deprecated)] // for old path, for dynamic_lib
use std::process::{ExitStatus, Command, Child, Output, Stdio};
use std::io::prelude::*;
use std::dynamic_lib::DynamicLibrary;
fn add_target_env(cmd: &mut Command, lib_path: &str, aux_path: Option<&str>) {
// Need to be sure to put both the lib_path and the aux path in the dylib
// search path for the child.
let mut path = DynamicLibrary::search_path();
match aux_path {
Some(p) => path.insert(0, Path::new(p)),
None => {}
}
path.insert(0, Path::new(lib_path));
// Add the new dylib search path var
let var = DynamicLibrary::envvar();
let newpath = DynamicLibrary::create_path(&path);
let newpath = String::from_utf8(newpath).unwrap();
cmd.env(var, &newpath);
}
pub struct Result {pub status: ExitStatus, pub out: String, pub err: String}
pub fn run(lib_path: &str,
prog: &str,
aux_path: Option<&str>,
args: &[String],
env: Vec<(String, String)> ,
input: Option<String>) -> Option<Result> {
let mut cmd = Command::new(prog);
cmd.args(args)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped());
add_target_env(&mut cmd, lib_path, aux_path);
for (key, val) in env {
cmd.env(&key, &val);
}
match cmd.spawn() {
Ok(mut process) => {
if let Some(input) = input {
process.stdin.as_mut().unwrap().write_all(input.as_bytes()).unwrap();
}
let Output { status, stdout, stderr } =
process.wait_with_output().unwrap();
Some(Result {
status: status,
out: String::from_utf8(stdout).unwrap(),
err: String::from_utf8(stderr).unwrap()
})
},
Err(..) => None
}
}
pub fn run_background(lib_path: &str,
prog: &str,
aux_path: Option<&str>,
args: &[String],
env: Vec<(String, String)> ,
input: Option<String>) -> Option<Child> {
let mut cmd = Command::new(prog);
cmd.args(args)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped());
add_target_env(&mut cmd, lib_path, aux_path);
for (key, val) in env {
cmd.env(&key, &val);
}
match cmd.spawn() {
Ok(mut process) => {
if let Some(input) = input {
process.stdin.as_mut().unwrap().write_all(input.as_bytes()).unwrap();
}
Some(process)
},
Err(..) => None
}
}