Bring native process bindings up to date

Move the tests into libstd, use the `iotest!` macro to test both native and uv
bindings, and use the cloexec trick to figure out when the child process fails
in exec.
This commit is contained in:
Alex Crichton
2013-12-26 18:28:24 -08:00
parent 00d87e0d81
commit 1763f36c9d
9 changed files with 288 additions and 254 deletions

View File

@@ -169,5 +169,152 @@ impl Drop for Process {
}
}
// Tests for this module can be found in the rtio-processes run-pass test, along
// with the justification for why it's not located here.
#[cfg(test)]
mod tests {
use io::process::{ProcessConfig, Process};
use prelude::*;
use str;
// FIXME(#10380)
#[cfg(unix, not(target_os="android"))]
iotest!(fn smoke() {
let io = ~[];
let args = ProcessConfig {
program: "/bin/sh",
args: [~"-c", ~"true"],
env: None,
cwd: None,
io: io,
};
let p = Process::new(args);
assert!(p.is_some());
let mut p = p.unwrap();
assert!(p.wait().success());
})
// FIXME(#10380)
#[cfg(unix, not(target_os="android"))]
iotest!(fn smoke_failure() {
let io = ~[];
let args = ProcessConfig {
program: "if-this-is-a-binary-then-the-world-has-ended",
args: [],
env: None,
cwd: None,
io: io,
};
match io::result(|| Process::new(args)) {
Ok(..) => fail!(),
Err(..) => {}
}
})
// FIXME(#10380)
#[cfg(unix, not(target_os="android"))]
iotest!(fn exit_reported_right() {
let io = ~[];
let args = ProcessConfig {
program: "/bin/sh",
args: [~"-c", ~"exit 1"],
env: None,
cwd: None,
io: io,
};
let p = Process::new(args);
assert!(p.is_some());
let mut p = p.unwrap();
assert!(p.wait().matches_exit_status(1));
})
#[cfg(unix, not(target_os="android"))]
iotest!(fn signal_reported_right() {
let io = ~[];
let args = ProcessConfig {
program: "/bin/sh",
args: [~"-c", ~"kill -1 $$"],
env: None,
cwd: None,
io: io,
};
let p = Process::new(args);
assert!(p.is_some());
let mut p = p.unwrap();
match p.wait() {
process::ExitSignal(1) => {},
result => fail!("not terminated by signal 1 (instead, {})", result),
}
})
pub fn read_all(input: &mut Reader) -> ~str {
let mut ret = ~"";
let mut buf = [0, ..1024];
loop {
match input.read(buf) {
None => { break }
Some(n) => { ret.push_str(str::from_utf8(buf.slice_to(n))); }
}
}
return ret;
}
pub fn run_output(args: ProcessConfig) -> ~str {
let p = Process::new(args);
assert!(p.is_some());
let mut p = p.unwrap();
assert!(p.io[0].is_none());
assert!(p.io[1].is_some());
let ret = read_all(p.io[1].get_mut_ref() as &mut Reader);
assert!(p.wait().success());
return ret;
}
// FIXME(#10380)
#[cfg(unix, not(target_os="android"))]
iotest!(fn stdout_works() {
let io = ~[Ignored, CreatePipe(false, true)];
let args = ProcessConfig {
program: "/bin/sh",
args: [~"-c", ~"echo foobar"],
env: None,
cwd: None,
io: io,
};
assert_eq!(run_output(args), ~"foobar\n");
})
// FIXME(#10380)
#[cfg(unix, not(target_os="android"))]
iotest!(fn set_cwd_works() {
let io = ~[Ignored, CreatePipe(false, true)];
let cwd = Some("/");
let args = ProcessConfig {
program: "/bin/sh",
args: [~"-c", ~"pwd"],
env: None,
cwd: cwd,
io: io,
};
assert_eq!(run_output(args), ~"/\n");
})
// FIXME(#10380)
#[cfg(unix, not(target_os="android"))]
iotest!(fn stdin_works() {
let io = ~[CreatePipe(true, false),
CreatePipe(false, true)];
let args = ProcessConfig {
program: "/bin/sh",
args: [~"-c", ~"read line; echo $line"],
env: None,
cwd: None,
io: io,
};
let mut p = Process::new(args).expect("didn't create a proces?!");
p.io[0].get_mut_ref().write("foobar".as_bytes());
p.io[0] = None; // close stdin;
let out = read_all(p.io[1].get_mut_ref() as &mut Reader);
assert!(p.wait().success());
assert_eq!(out, ~"foobar\n");
})
}