|
| 1 | +// Copyright 2013 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +// compile-flags: --test |
| 12 | +// xfail-fast |
| 13 | + |
| 14 | +// In the current state of affairs, libuv registers a SIGCHLD handler when a |
| 15 | +// process is spawned through it. This is not done with a SA_RESTART flag, |
| 16 | +// meaning that all of our syscalls run the risk of returning EINTR. This error |
| 17 | +// is not correctly handled in the majority of std::io, so these can't run with |
| 18 | +// the main body of tests there. |
| 19 | +// |
| 20 | +// That being said, libuv correctly handles EINTR completely, so these tests |
| 21 | +// themselves are safe against that. Currently the test runner may run into this |
| 22 | +// problem, but it's less likely than a whole suite of tests... |
| 23 | +// |
| 24 | +// See #9341 |
| 25 | + |
| 26 | +use std::rt::io::process::*; |
| 27 | +use std::rt::io::{Reader, Writer}; |
| 28 | +use std::rt::io::pipe::*; |
| 29 | +use std::str; |
| 30 | + |
| 31 | +#[test] |
| 32 | +#[cfg(unix, not(android))] |
| 33 | +fn smoke() { |
| 34 | + let io = ~[]; |
| 35 | + let args = ProcessConfig { |
| 36 | + program: "/bin/sh", |
| 37 | + args: [~"-c", ~"true"], |
| 38 | + env: None, |
| 39 | + cwd: None, |
| 40 | + io: io, |
| 41 | + }; |
| 42 | + let p = Process::new(args); |
| 43 | + assert!(p.is_some()); |
| 44 | + let mut p = p.unwrap(); |
| 45 | + assert_eq!(p.wait(), 0); |
| 46 | +} |
| 47 | + |
| 48 | +#[test] |
| 49 | +#[cfg(unix, not(android))] |
| 50 | +fn smoke_failure() { |
| 51 | + let io = ~[]; |
| 52 | + let args = ProcessConfig { |
| 53 | + program: "if-this-is-a-binary-then-the-world-has-ended", |
| 54 | + args: [], |
| 55 | + env: None, |
| 56 | + cwd: None, |
| 57 | + io: io, |
| 58 | + }; |
| 59 | + let p = Process::new(args); |
| 60 | + assert!(p.is_some()); |
| 61 | + let mut p = p.unwrap(); |
| 62 | + assert!(p.wait() != 0); |
| 63 | +} |
| 64 | + |
| 65 | +#[test] |
| 66 | +#[cfg(unix, not(android))] |
| 67 | +fn exit_reported_right() { |
| 68 | + let io = ~[]; |
| 69 | + let args = ProcessConfig { |
| 70 | + program: "/bin/sh", |
| 71 | + args: [~"-c", ~"exit 1"], |
| 72 | + env: None, |
| 73 | + cwd: None, |
| 74 | + io: io, |
| 75 | + }; |
| 76 | + let p = Process::new(args); |
| 77 | + assert!(p.is_some()); |
| 78 | + let mut p = p.unwrap(); |
| 79 | + assert_eq!(p.wait(), 1); |
| 80 | +} |
| 81 | + |
| 82 | +fn read_all(input: &mut Reader) -> ~str { |
| 83 | + let mut ret = ~""; |
| 84 | + let mut buf = [0, ..1024]; |
| 85 | + loop { |
| 86 | + match input.read(buf) { |
| 87 | + None | Some(0) => { break } |
| 88 | + Some(n) => { ret = ret + str::from_utf8(buf.slice_to(n)); } |
| 89 | + } |
| 90 | + } |
| 91 | + return ret; |
| 92 | +} |
| 93 | + |
| 94 | +fn run_output(args: ProcessConfig) -> ~str { |
| 95 | + let p = Process::new(args); |
| 96 | + assert!(p.is_some()); |
| 97 | + let mut p = p.unwrap(); |
| 98 | + assert!(p.io[0].is_none()); |
| 99 | + assert!(p.io[1].is_some()); |
| 100 | + let ret = read_all(p.io[1].get_mut_ref() as &mut Reader); |
| 101 | + assert_eq!(p.wait(), 0); |
| 102 | + return ret; |
| 103 | +} |
| 104 | + |
| 105 | +#[test] |
| 106 | +#[cfg(unix, not(android))] |
| 107 | +fn stdout_works() { |
| 108 | + let pipe = PipeStream::new().unwrap(); |
| 109 | + let io = ~[Ignored, CreatePipe(pipe, false, true)]; |
| 110 | + let args = ProcessConfig { |
| 111 | + program: "/bin/sh", |
| 112 | + args: [~"-c", ~"echo foobar"], |
| 113 | + env: None, |
| 114 | + cwd: None, |
| 115 | + io: io, |
| 116 | + }; |
| 117 | + assert_eq!(run_output(args), ~"foobar\n"); |
| 118 | +} |
| 119 | + |
| 120 | +#[test] |
| 121 | +#[cfg(unix, not(android))] |
| 122 | +fn set_cwd_works() { |
| 123 | + let pipe = PipeStream::new().unwrap(); |
| 124 | + let io = ~[Ignored, CreatePipe(pipe, false, true)]; |
| 125 | + let cwd = Some("/"); |
| 126 | + let args = ProcessConfig { |
| 127 | + program: "/bin/sh", |
| 128 | + args: [~"-c", ~"pwd"], |
| 129 | + env: None, |
| 130 | + cwd: cwd, |
| 131 | + io: io, |
| 132 | + }; |
| 133 | + assert_eq!(run_output(args), ~"/\n"); |
| 134 | +} |
| 135 | + |
| 136 | +#[test] |
| 137 | +#[cfg(unix, not(android))] |
| 138 | +fn stdin_works() { |
| 139 | + let input = PipeStream::new().unwrap(); |
| 140 | + let output = PipeStream::new().unwrap(); |
| 141 | + let io = ~[CreatePipe(input, true, false), |
| 142 | + CreatePipe(output, false, true)]; |
| 143 | + let args = ProcessConfig { |
| 144 | + program: "/bin/sh", |
| 145 | + args: [~"-c", ~"read line; echo $line"], |
| 146 | + env: None, |
| 147 | + cwd: None, |
| 148 | + io: io, |
| 149 | + }; |
| 150 | + let mut p = Process::new(args).expect("didn't create a proces?!"); |
| 151 | + p.io[0].get_mut_ref().write("foobar".as_bytes()); |
| 152 | + p.io[0] = None; // close stdin; |
| 153 | + let out = read_all(p.io[1].get_mut_ref() as &mut Reader); |
| 154 | + assert_eq!(p.wait(), 0); |
| 155 | + assert_eq!(out, ~"foobar\n"); |
| 156 | +} |
0 commit comments