Skip to content

Commit 6601ffc

Browse files
committed
replace procsrv functions with Command
1 parent c886246 commit 6601ffc

File tree

2 files changed

+70
-182
lines changed

2 files changed

+70
-182
lines changed

src/tools/compiletest/src/procsrv.rs

Lines changed: 2 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@
1010

1111
use std::env;
1212
use std::ffi::OsString;
13-
use std::io::prelude::*;
14-
use std::io;
1513
use std::path::PathBuf;
16-
use std::process::{Child, Command, ExitStatus, Output, Stdio};
14+
use std::process::Command;
1715

1816
/// Get the name of the environment variable that holds dynamic library
1917
/// locations
@@ -31,7 +29,7 @@ pub fn dylib_env_var() -> &'static str {
3129

3230
/// Add `lib_path` and `aux_path` (if it is `Some`) to the dynamic library
3331
/// env var
34-
fn add_target_env(cmd: &mut Command, lib_path: &str, aux_path: Option<&str>) {
32+
pub fn add_target_env(cmd: &mut Command, lib_path: &str, aux_path: Option<&str>) {
3533
// Need to be sure to put both the lib_path and the aux path in the dylib
3634
// search path for the child.
3735
let var = dylib_env_var();
@@ -46,89 +44,3 @@ fn add_target_env(cmd: &mut Command, lib_path: &str, aux_path: Option<&str>) {
4644
let newpath = env::join_paths(&path).unwrap();
4745
cmd.env(var, newpath);
4846
}
49-
50-
/// Represents exit status, stdout and stderr of a completed process
51-
pub struct Result {
52-
pub status: ExitStatus,
53-
pub out: String,
54-
pub err: String,
55-
}
56-
57-
/// Runs a test program
58-
///
59-
/// # Params
60-
/// - `lib_path` Path to search for required library
61-
/// - `prog` command to run
62-
/// - `aux_path` Optional extra path to search for required
63-
/// auxiliary libraries
64-
/// - `args` List of arguments to pass to `prog`
65-
/// - `env` List of environment variables to set, `.0` is variable name,
66-
/// `.1` is value
67-
/// - `input` String to be fed as stdin
68-
/// - `current_dir` Optional working dir to run command in
69-
///
70-
pub fn run(lib_path: &str,
71-
prog: &str,
72-
aux_path: Option<&str>,
73-
args: &[String],
74-
env: Vec<(String, String)>,
75-
input: Option<String>,
76-
current_dir: Option<String>)
77-
-> io::Result<Result> {
78-
79-
let mut cmd = Command::new(prog);
80-
cmd.args(args)
81-
.stdout(Stdio::piped())
82-
.stderr(Stdio::piped())
83-
.stdin(Stdio::piped());
84-
85-
add_target_env(&mut cmd, lib_path, aux_path);
86-
for (key, val) in env {
87-
cmd.env(&key, &val);
88-
}
89-
if let Some(cwd) = current_dir {
90-
cmd.current_dir(cwd);
91-
}
92-
93-
let mut process = cmd.spawn()?;
94-
if let Some(input) = input {
95-
process.stdin.as_mut().unwrap().write_all(input.as_bytes()).unwrap();
96-
}
97-
let Output { status, stdout, stderr } = process.wait_with_output().unwrap();
98-
99-
Ok(Result {
100-
status,
101-
out: String::from_utf8(stdout).unwrap(),
102-
err: String::from_utf8(stderr).unwrap(),
103-
})
104-
}
105-
106-
/// Same as `run`, but return process rather than waiting on completion
107-
pub fn run_background(lib_path: &str,
108-
prog: &str,
109-
aux_path: Option<&str>,
110-
args: &[String],
111-
env: Vec<(String, String)>,
112-
input: Option<String>,
113-
current_dir: Option<String>)
114-
-> io::Result<Child> {
115-
116-
let mut cmd = Command::new(prog);
117-
cmd.args(args)
118-
.stdin(Stdio::piped())
119-
.stdout(Stdio::piped());
120-
add_target_env(&mut cmd, lib_path, aux_path);
121-
for (key, val) in env {
122-
cmd.env(&key, &val);
123-
}
124-
if let Some(cwd) = current_dir {
125-
cmd.current_dir(cwd);
126-
}
127-
128-
let mut process = cmd.spawn()?;
129-
if let Some(input) = input {
130-
process.stdin.as_mut().unwrap().write_all(input.as_bytes()).unwrap();
131-
}
132-
133-
Ok(process)
134-
}

src/tools/compiletest/src/runtest.rs

Lines changed: 68 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use std::fs::{self, File, create_dir_all};
2727
use std::io::prelude::*;
2828
use std::io::{self, BufReader};
2929
use std::path::{Path, PathBuf};
30-
use std::process::{Command, Output, ExitStatus};
30+
use std::process::{Command, Output, ExitStatus, Stdio};
3131
use std::str;
3232
use std::collections::HashMap;
3333

@@ -500,32 +500,19 @@ actual:\n\
500500
debug!("script_str = {}", script_str);
501501
self.dump_output_file(&script_str, "debugger.script");
502502

503+
let adb_path = &self.config.adb_path;
503504

504-
procsrv::run("",
505-
&self.config.adb_path,
506-
None,
507-
&[
508-
"push".to_owned(),
509-
exe_file.to_str().unwrap().to_owned(),
510-
self.config.adb_test_dir.clone()
511-
],
512-
Vec::new(),
513-
None,
514-
None)
515-
.expect(&format!("failed to exec `{:?}`", self.config.adb_path));
516-
517-
procsrv::run("",
518-
&self.config.adb_path,
519-
None,
520-
&[
521-
"forward".to_owned(),
522-
"tcp:5039".to_owned(),
523-
"tcp:5039".to_owned()
524-
],
525-
Vec::new(),
526-
None,
527-
None)
528-
.expect(&format!("failed to exec `{:?}`", self.config.adb_path));
505+
Command::new(adb_path)
506+
.arg("push")
507+
.arg(&exe_file)
508+
.arg(&self.config.adb_test_dir)
509+
.status()
510+
.expect(&format!("failed to exec `{:?}`", adb_path));
511+
512+
Command::new(adb_path)
513+
.args(&["forward", "tcp:5039", "tcp:5039"])
514+
.status()
515+
.expect(&format!("failed to exec `{:?}`", adb_path));
529516

530517
let adb_arg = format!("export LD_LIBRARY_PATH={}; \
531518
gdbserver{} :5039 {}/{}",
@@ -537,23 +524,15 @@ actual:\n\
537524
.unwrap());
538525

539526
debug!("adb arg: {}", adb_arg);
540-
let mut process = procsrv::run_background("",
541-
&self.config.adb_path
542-
,
543-
None,
544-
&[
545-
"shell".to_owned(),
546-
adb_arg.clone()
547-
],
548-
Vec::new(),
549-
None,
550-
None)
551-
.expect(&format!("failed to exec `{:?}`", self.config.adb_path));
527+
let mut adb = Command::new(adb_path)
528+
.args(&["shell", &adb_arg])
529+
.spawn()
530+
.expect(&format!("failed to exec `{:?}`", adb_path));
552531

553532
// Wait for the gdbserver to print out "Listening on port ..."
554533
// at which point we know that it's started and then we can
555534
// execute the debugger below.
556-
let mut stdout = BufReader::new(process.stdout.take().unwrap());
535+
let mut stdout = BufReader::new(adb.stdout.take().unwrap());
557536
let mut line = String::new();
558537
loop {
559538
line.truncate(0);
@@ -574,17 +553,13 @@ actual:\n\
574553

575554
let mut gdb_path = tool_path;
576555
gdb_path.push_str("/bin/gdb");
577-
let procsrv::Result {
578-
out,
579-
err,
580-
status
581-
} = procsrv::run("",
582-
&gdb_path,
583-
None,
584-
&debugger_opts,
585-
Vec::new(),
586-
None,
587-
None)
556+
let Output {
557+
status,
558+
stdout,
559+
stderr
560+
} = Command::new(&gdb_path)
561+
.args(&debugger_opts)
562+
.output()
588563
.expect(&format!("failed to exec `{:?}`", gdb_path));
589564
let cmdline = {
590565
let cmdline = self.make_cmdline("",
@@ -596,11 +571,11 @@ actual:\n\
596571

597572
debugger_run_result = ProcRes {
598573
status,
599-
stdout: out,
600-
stderr: err,
574+
stdout: String::from_utf8(stdout).unwrap(),
575+
stderr: String::from_utf8(stderr).unwrap(),
601576
cmdline,
602577
};
603-
if process.kill().is_err() {
578+
if adb.kill().is_err() {
604579
println!("Adb process is already finished.");
605580
}
606581
}
@@ -1367,7 +1342,46 @@ actual:\n\
13671342
aux_path: Option<&str>,
13681343
input: Option<String>,
13691344
working_dir: Option<String>) -> ProcRes {
1370-
self.program_output(lib_path, prog, aux_path, args, procenv, input, working_dir)
1345+
let cmdline =
1346+
{
1347+
let cmdline = self.make_cmdline(lib_path,
1348+
&prog,
1349+
&args);
1350+
logv(self.config, format!("executing {}", cmdline));
1351+
cmdline
1352+
};
1353+
1354+
let mut process = Command::new(&prog);
1355+
process
1356+
.args(&args)
1357+
.stdout(Stdio::piped())
1358+
.stderr(Stdio::piped())
1359+
.stdin(Stdio::piped());
1360+
1361+
procsrv::add_target_env(&mut process, lib_path, aux_path);
1362+
for (key, val) in procenv {
1363+
process.env(&key, &val);
1364+
}
1365+
if let Some(cwd) = working_dir {
1366+
process.current_dir(cwd);
1367+
}
1368+
1369+
let mut child = process.spawn().expect(&format!("failed to exec `{}`", prog));
1370+
if let Some(input) = input {
1371+
child.stdin.as_mut().unwrap().write_all(input.as_bytes()).unwrap();
1372+
}
1373+
let Output { status, stdout, stderr } = child.wait_with_output().unwrap();
1374+
1375+
let result = ProcRes {
1376+
status,
1377+
stdout: String::from_utf8(stdout).unwrap(),
1378+
stderr: String::from_utf8(stderr).unwrap(),
1379+
cmdline,
1380+
};
1381+
1382+
self.dump_output(&result.stdout, &result.stderr);
1383+
1384+
result
13711385
}
13721386

13731387
fn make_compile_args(&self,
@@ -1554,44 +1568,6 @@ actual:\n\
15541568
}
15551569
}
15561570

1557-
fn program_output(&self,
1558-
lib_path: &str,
1559-
prog: String,
1560-
aux_path: Option<&str>,
1561-
args: Vec<String>,
1562-
env: Vec<(String, String)>,
1563-
input: Option<String>,
1564-
working_dir: Option<String>)
1565-
-> ProcRes {
1566-
let cmdline =
1567-
{
1568-
let cmdline = self.make_cmdline(lib_path,
1569-
&prog,
1570-
&args);
1571-
logv(self.config, format!("executing {}", cmdline));
1572-
cmdline
1573-
};
1574-
1575-
let procsrv::Result {
1576-
out,
1577-
err,
1578-
status
1579-
} = procsrv::run(lib_path,
1580-
&prog,
1581-
aux_path,
1582-
&args,
1583-
env,
1584-
input,
1585-
working_dir).expect(&format!("failed to exec `{}`", prog));
1586-
self.dump_output(&out, &err);
1587-
ProcRes {
1588-
status,
1589-
stdout: out,
1590-
stderr: err,
1591-
cmdline,
1592-
}
1593-
}
1594-
15951571
fn make_cmdline(&self, libpath: &str, prog: &str, args: &[String]) -> String {
15961572
use util;
15971573

0 commit comments

Comments
 (0)