about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2013-12-26 18:28:24 -0800
committerAlex Crichton <alex@alexcrichton.com>2013-12-27 17:41:04 -0800
commit1763f36c9d47550838793e129f2297ecfc8bebd1 (patch)
tree67036da1dac631ab73b437c96d276c35d3949717 /src/libstd
parent00d87e0d8198ffb268251b5af2eb2ce19249c7f8 (diff)
downloadrust-1763f36c9d47550838793e129f2297ecfc8bebd1.tar.gz
rust-1763f36c9d47550838793e129f2297ecfc8bebd1.zip
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.
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/io/comm_adapters.rs1
-rw-r--r--src/libstd/io/net/tcp.rs3
-rw-r--r--src/libstd/io/process.rs151
-rw-r--r--src/libstd/io/test.rs1
-rw-r--r--src/libstd/run.rs2
5 files changed, 152 insertions, 6 deletions
diff --git a/src/libstd/io/comm_adapters.rs b/src/libstd/io/comm_adapters.rs
index 7a78800c33d..9b8a8b57e67 100644
--- a/src/libstd/io/comm_adapters.rs
+++ b/src/libstd/io/comm_adapters.rs
@@ -117,7 +117,6 @@ mod test {
     use prelude::*;
     use super::*;
     use io;
-    use comm;
     use task;
 
     #[test]
diff --git a/src/libstd/io/net/tcp.rs b/src/libstd/io/net/tcp.rs
index e7787692dd2..c963057acde 100644
--- a/src/libstd/io/net/tcp.rs
+++ b/src/libstd/io/net/tcp.rs
@@ -134,9 +134,8 @@ impl Acceptor<TcpStream> for TcpAcceptor {
 #[cfg(test)]
 mod test {
     use super::*;
-    use io::net::ip::{Ipv4Addr, SocketAddr};
+    use io::net::ip::SocketAddr;
     use io::*;
-    use io::test::{next_test_ip4, next_test_ip6};
     use prelude::*;
 
     #[test] #[ignore]
diff --git a/src/libstd/io/process.rs b/src/libstd/io/process.rs
index bbb2a7ef398..a8b7e8e00ea 100644
--- a/src/libstd/io/process.rs
+++ b/src/libstd/io/process.rs
@@ -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");
+    })
+
+}
diff --git a/src/libstd/io/test.rs b/src/libstd/io/test.rs
index 4be11227965..c189bd47b06 100644
--- a/src/libstd/io/test.rs
+++ b/src/libstd/io/test.rs
@@ -33,6 +33,7 @@ macro_rules! iotest (
             use io::net::udp::*;
             #[cfg(unix)]
             use io::net::unix::*;
+            use io::process::*;
             use str;
             use util;
 
diff --git a/src/libstd/run.rs b/src/libstd/run.rs
index 69704c855ee..33be746e604 100644
--- a/src/libstd/run.rs
+++ b/src/libstd/run.rs
@@ -339,7 +339,7 @@ mod tests {
     use task::spawn;
     use unstable::running_on_valgrind;
     use io::pipe::PipeStream;
-    use io::{Writer, Reader, io_error, FileNotFound, OtherIoError};
+    use io::{Writer, Reader, io_error, FileNotFound};
 
     #[test]
     #[cfg(not(target_os="android"))] // FIXME(#10380)