PageRenderTime 47ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/src/test/ui-fulldeps/stdio-from.rs

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