Skip to content

Commit 0bf3a8f

Browse files
committed
Move the dup2_to_replace_stdio test into a separate child process.
This should help it run better on macos and ios, as well as on older Linux versions and qemu.
1 parent 631ba24 commit 0bf3a8f

File tree

2 files changed

+52
-38
lines changed

2 files changed

+52
-38
lines changed

examples/dup2_to_replace_stdio.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//! This is an example of how to use `dup2` to replace the stdin and stdout file
2+
//! descriptors.
3+
4+
#[cfg(not(windows))]
5+
fn main() {
6+
use io_lifetimes::AsFilelike;
7+
use rustix::io::{dup2, pipe};
8+
use std::io::{BufRead, BufReader, Write};
9+
use std::mem::forget;
10+
11+
let (reader, writer) = pipe().unwrap();
12+
let (stdin, stdout) = unsafe { (rustix::io::take_stdin(), rustix::io::take_stdout()) };
13+
dup2(&reader, &stdin).unwrap();
14+
dup2(&writer, &stdout).unwrap();
15+
forget(stdin);
16+
forget(stdout);
17+
18+
drop(reader);
19+
drop(writer);
20+
21+
// Don't use `std::io::stdout()` because in tests it's captured.
22+
unsafe {
23+
writeln!(
24+
rustix::io::stdout().as_filelike_view::<std::fs::File>(),
25+
"hello, world!"
26+
)
27+
.unwrap();
28+
29+
let mut s = String::new();
30+
BufReader::new(&*rustix::io::stdin().as_filelike_view::<std::fs::File>())
31+
.read_line(&mut s)
32+
.unwrap();
33+
assert_eq!(s, "hello, world!\n");
34+
}
35+
}
36+
37+
#[cfg(windows)]
38+
fn main() {
39+
unimplemented!()
40+
}

tests/io/dup2_to_replace_stdio.rs

Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,18 @@
11
#![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;
55

66
/// Use `dup2` to replace the stdin and stdout file descriptors.
77
#[test]
88
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());
4418
}

0 commit comments

Comments
 (0)