]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass-fulldeps/stdio-from.rs
Auto merge of #54919 - alexcrichton:update-cargo, r=Mark-Simulacrum
[rust.git] / src / test / run-pass-fulldeps / stdio-from.rs
1 // Copyright 2017 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 // ignore-cross-compile
12
13 use std::env;
14 use std::fs::File;
15 use std::io;
16 use std::io::{Read, Write};
17 use std::process::{Command, Stdio};
18 use std::path::PathBuf;
19
20 fn main() {
21     if env::args().len() > 1 {
22         child().unwrap()
23     } else {
24         parent().unwrap()
25     }
26 }
27
28 fn parent() -> io::Result<()> {
29     let td = PathBuf::from(env::var_os("RUST_TEST_TMPDIR").unwrap());
30     let input = td.join("stdio-from-input");
31     let output = td.join("stdio-from-output");
32
33     File::create(&input)?.write_all(b"foo\n")?;
34
35     // Set up this chain:
36     //     $ me <file | me | me >file
37     // ... to duplicate each line 8 times total.
38
39     let mut child1 = Command::new(env::current_exe()?)
40         .arg("first")
41         .stdin(File::open(&input)?) // tests File::into()
42         .stdout(Stdio::piped())
43         .spawn()?;
44
45     let mut child3 = Command::new(env::current_exe()?)
46         .arg("third")
47         .stdin(Stdio::piped())
48         .stdout(File::create(&output)?) // tests File::into()
49         .spawn()?;
50
51     // Started out of order so we can test both `ChildStdin` and `ChildStdout`.
52     let mut child2 = Command::new(env::current_exe()?)
53         .arg("second")
54         .stdin(child1.stdout.take().unwrap()) // tests ChildStdout::into()
55         .stdout(child3.stdin.take().unwrap()) // tests ChildStdin::into()
56         .spawn()?;
57
58     assert!(child1.wait()?.success());
59     assert!(child2.wait()?.success());
60     assert!(child3.wait()?.success());
61
62     let mut data = String::new();
63     File::open(&output)?.read_to_string(&mut data)?;
64     for line in data.lines() {
65         assert_eq!(line, "foo");
66     }
67     assert_eq!(data.lines().count(), 8);
68     Ok(())
69 }
70
71 fn child() -> io::Result<()> {
72     // double everything
73     let mut input = vec![];
74     io::stdin().read_to_end(&mut input)?;
75     io::stdout().write_all(&input)?;
76     io::stdout().write_all(&input)?;
77     Ok(())
78 }