|
1 | 1 | #![cfg(not(target_os = "wasi"))]
|
2 |
| -// This test interacts with `cargo test` in ways which causes failures on |
3 |
| -// darwin; disable it until we have a better option. |
4 |
| -#![cfg(not(any(target_os = "ios", target_os = "macos")))] |
| 2 | + |
| 3 | +use std::env; |
| 4 | +use std::process::Command; |
5 | 5 |
|
6 | 6 | /// Use `dup2` to replace the stdin and stdout file descriptors.
|
7 | 7 | #[test]
|
8 | 8 | fn dup2_to_replace_stdio() {
|
9 |
| - use io_lifetimes::AsFilelike; |
10 |
| - use rustix::io::{dup2, pipe}; |
11 |
| - use std::io::{BufRead, BufReader, Write}; |
12 |
| - use std::mem::forget; |
13 |
| - |
14 |
| - // This test is flaky under qemu. |
15 |
| - if std::env::vars().any(|var| var.0.starts_with("CARGO_TARGET_") && var.0.ends_with("_RUNNER")) |
16 |
| - { |
17 |
| - return; |
18 |
| - } |
19 |
| - |
20 |
| - let (reader, writer) = pipe().unwrap(); |
21 |
| - let (stdin, stdout) = unsafe { (rustix::io::take_stdin(), rustix::io::take_stdout()) }; |
22 |
| - dup2(&reader, &stdin).unwrap(); |
23 |
| - dup2(&writer, &stdout).unwrap(); |
24 |
| - forget(stdin); |
25 |
| - forget(stdout); |
26 |
| - |
27 |
| - drop(reader); |
28 |
| - drop(writer); |
29 |
| - |
30 |
| - // Don't use `std::io::stdout()` because in tests it's captured. |
31 |
| - unsafe { |
32 |
| - writeln!( |
33 |
| - rustix::io::stdout().as_filelike_view::<std::fs::File>(), |
34 |
| - "hello, world!" |
35 |
| - ) |
36 |
| - .unwrap(); |
37 |
| - |
38 |
| - let mut s = String::new(); |
39 |
| - BufReader::new(&*rustix::io::stdin().as_filelike_view::<std::fs::File>()) |
40 |
| - .read_line(&mut s) |
41 |
| - .unwrap(); |
42 |
| - assert_eq!(s, "hello, world!\n"); |
43 |
| - } |
| 9 | + // This test modifies the stdio file descriptors, so we run it in a |
| 10 | + // separate process so that it doesn't inferfere with the test harness. |
| 11 | + assert!(Command::new(env::var("CARGO").unwrap()) |
| 12 | + .arg("run") |
| 13 | + .arg("--example") |
| 14 | + .arg("dup2_to_replace_stdio") |
| 15 | + .status() |
| 16 | + .unwrap() |
| 17 | + .success()); |
44 | 18 | }
|
0 commit comments