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