Only set cwd for test process, not compiler

This commit is contained in:
Richard Dodd 2017-07-17 16:54:38 +01:00
parent c35030af87
commit 5aa8cc8412
2 changed files with 40 additions and 11 deletions

View File

@ -9,11 +9,14 @@
// except according to those terms. // except according to those terms.
use std::env; use std::env;
use std::ffi::OsString;
use std::io::prelude::*; use std::io::prelude::*;
use std::io; use std::io;
use std::path::PathBuf; use std::path::PathBuf;
use std::process::{Child, Command, ExitStatus, Output, Stdio}; use std::process::{Child, Command, ExitStatus, Output, Stdio};
/// Get the name of the environment variable that holds dynamic library
/// locations
pub fn dylib_env_var() -> &'static str { pub fn dylib_env_var() -> &'static str {
if cfg!(windows) { if cfg!(windows) {
"PATH" "PATH"
@ -26,11 +29,13 @@ pub fn dylib_env_var() -> &'static str {
} }
} }
/// Add `lib_path` and `aux_path` (if it is `Some`) to the dynamic library
/// env var
fn add_target_env(cmd: &mut Command, lib_path: &str, aux_path: Option<&str>) { 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 // Need to be sure to put both the lib_path and the aux path in the dylib
// search path for the child. // search path for the child.
let var = dylib_env_var(); let var = dylib_env_var();
let mut path = env::split_paths(&env::var_os(var).unwrap_or_default()) let mut path = env::split_paths(&env::var_os(var).unwrap_or(OsString::new()))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
if let Some(p) = aux_path { if let Some(p) = aux_path {
path.insert(0, PathBuf::from(p)) path.insert(0, PathBuf::from(p))
@ -42,12 +47,26 @@ fn add_target_env(cmd: &mut Command, lib_path: &str, aux_path: Option<&str>) {
cmd.env(var, newpath); cmd.env(var, newpath);
} }
/// Represents exit status, stdout and stderr of a completed process
pub struct Result { pub struct Result {
pub status: ExitStatus, pub status: ExitStatus,
pub out: String, pub out: String,
pub err: String, pub err: String,
} }
/// Runs a test program
///
/// # Params
/// - `lib_path` Path to search for required library
/// - `prog` command to run
/// - `aux_path` Optional extra path to search for required
/// auxiliary libraries
/// - `args` List of arguments to pass to `prog`
/// - `env` List of environment variables to set, `.0` is variable name,
/// `.1` is value
/// - `input` String to be fed as stdin
/// - `current_dir` Optional working dir to run command in
///
pub fn run(lib_path: &str, pub fn run(lib_path: &str,
prog: &str, prog: &str,
aux_path: Option<&str>, aux_path: Option<&str>,
@ -69,7 +88,6 @@ pub fn run(lib_path: &str,
} }
if let Some(cwd) = current_dir { if let Some(cwd) = current_dir {
cmd.current_dir(cwd); cmd.current_dir(cwd);
panic!("Backtrace");
} }
let mut process = cmd.spawn()?; let mut process = cmd.spawn()?;
@ -85,6 +103,7 @@ pub fn run(lib_path: &str,
}) })
} }
/// Same as `run`, but return process rather than waiting on completion
pub fn run_background(lib_path: &str, pub fn run_background(lib_path: &str,
prog: &str, prog: &str,
aux_path: Option<&str>, aux_path: Option<&str>,

View File

@ -334,7 +334,8 @@ impl<'test> TestCx<'test> {
self.props.exec_env.clone(), self.props.exec_env.clone(),
self.config.compile_lib_path.to_str().unwrap(), self.config.compile_lib_path.to_str().unwrap(),
Some(aux_dir.to_str().unwrap()), Some(aux_dir.to_str().unwrap()),
Some(src)) Some(src),
None)
} }
fn make_pp_args(&self, fn make_pp_args(&self,
@ -690,6 +691,7 @@ actual:\n\
environment, environment,
self.config.run_lib_path.to_str().unwrap(), self.config.run_lib_path.to_str().unwrap(),
None, None,
None,
None); None);
} }
} }
@ -1235,15 +1237,21 @@ actual:\n\
env, env,
self.config.run_lib_path.to_str().unwrap(), self.config.run_lib_path.to_str().unwrap(),
Some(aux_dir.to_str().unwrap()), Some(aux_dir.to_str().unwrap()),
None,
None) None)
} }
_ => { _ => {
let aux_dir = self.aux_output_dir_name(); let aux_dir = self.aux_output_dir_name();
let working_dir =
Some(self.output_base_name()
.parent().unwrap()
.to_str().unwrap().to_owned());
self.compose_and_run(self.make_run_args(), self.compose_and_run(self.make_run_args(),
env, env,
self.config.run_lib_path.to_str().unwrap(), self.config.run_lib_path.to_str().unwrap(),
Some(aux_dir.to_str().unwrap()), Some(aux_dir.to_str().unwrap()),
None) None,
working_dir)
} }
} }
} }
@ -1321,6 +1329,7 @@ actual:\n\
Vec::new(), Vec::new(),
aux_cx.config.compile_lib_path.to_str().unwrap(), aux_cx.config.compile_lib_path.to_str().unwrap(),
Some(aux_dir.to_str().unwrap()), Some(aux_dir.to_str().unwrap()),
None,
None); None);
if !auxres.status.success() { if !auxres.status.success() {
self.fatal_proc_rec( self.fatal_proc_rec(
@ -1334,7 +1343,8 @@ actual:\n\
self.props.rustc_env.clone(), self.props.rustc_env.clone(),
self.config.compile_lib_path.to_str().unwrap(), self.config.compile_lib_path.to_str().unwrap(),
Some(aux_dir.to_str().unwrap()), Some(aux_dir.to_str().unwrap()),
input) input,
None)
} }
fn compose_and_run(&self, fn compose_and_run(&self,
@ -1342,8 +1352,9 @@ actual:\n\
procenv: Vec<(String, String)> , procenv: Vec<(String, String)> ,
lib_path: &str, lib_path: &str,
aux_path: Option<&str>, aux_path: Option<&str>,
input: Option<String>) -> ProcRes { input: Option<String>,
self.program_output(lib_path, prog, aux_path, args, procenv, input) working_dir: Option<String>) -> ProcRes {
self.program_output(lib_path, prog, aux_path, args, procenv, input, working_dir)
} }
fn make_compile_args(&self, fn make_compile_args(&self,
@ -1536,7 +1547,8 @@ actual:\n\
aux_path: Option<&str>, aux_path: Option<&str>,
args: Vec<String>, args: Vec<String>,
env: Vec<(String, String)>, env: Vec<(String, String)>,
input: Option<String>) input: Option<String>,
working_dir: Option<String>)
-> ProcRes { -> ProcRes {
let cmdline = let cmdline =
{ {
@ -1546,8 +1558,6 @@ actual:\n\
logv(self.config, format!("executing {}", cmdline)); logv(self.config, format!("executing {}", cmdline));
cmdline cmdline
}; };
let working_dir =
Some(self.output_base_name().parent().unwrap().to_str().unwrap().to_owned());
let procsrv::Result { let procsrv::Result {
out, out,
@ -1723,7 +1733,7 @@ actual:\n\
args: vec![format!("-input-file={}", irfile.to_str().unwrap()), args: vec![format!("-input-file={}", irfile.to_str().unwrap()),
self.testpaths.file.to_str().unwrap().to_owned()] self.testpaths.file.to_str().unwrap().to_owned()]
}; };
self.compose_and_run(proc_args, Vec::new(), "", None, None) self.compose_and_run(proc_args, Vec::new(), "", None, None, None)
} }
fn run_codegen_test(&self) { fn run_codegen_test(&self) {