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