rust/tests/ui/std/switch-stdout.rs
Joshua Nelson 88ee6e5a6c Fix tests on wasm
The `std` test straightforwardly can't work without file descriptors;
 #99417 tracks moving it out of tests/ui.

`issue-13560.rs` requires the target to support dynamic linking.

`extern-mod-syntax` is interesting. The original test was added to check
if `extern mod` could be parsed correctly and used `extern mod std` and
an import:
138dc3048a (diff-73700e1e851b7a37bc92174635dab726124c82e5bfabbbc45b4a3c2e8e14fadd)
At some point `std::json::Object` was moved out of std to an unstable
rustc-only `extras` crate, and rather than just changing the import it
got changed to use the unstable crate. When `extras` was removed, people
assumed the test was meant to also test rustc_private and changed it to
another unstable crate rather than using something in std.

This changes the test to remove the `rustc_private` import, to allow it
to work properly when cross-compiling.
2023-04-13 22:10:26 -05:00

53 lines
1.2 KiB
Rust

// run-pass
// ignore-wasm (needs file descriptors and env variables)
use std::env;
use std::fs::File;
use std::io::{Read, Write};
use std::path::PathBuf;
#[cfg(unix)]
fn switch_stdout_to(file: File) {
use std::os::unix::prelude::*;
extern "C" {
fn dup2(old: i32, new: i32) -> i32;
}
unsafe {
assert_eq!(dup2(file.as_raw_fd(), 1), 1);
}
}
#[cfg(windows)]
fn switch_stdout_to(file: File) {
use std::os::windows::prelude::*;
extern "system" {
fn SetStdHandle(nStdHandle: u32, handle: *mut u8) -> i32;
}
const STD_OUTPUT_HANDLE: u32 = (-11i32) as u32;
unsafe {
let rc = SetStdHandle(STD_OUTPUT_HANDLE, file.into_raw_handle() as *mut _);
assert!(rc != 0);
}
}
fn main() {
let path = PathBuf::from(env::var_os("RUST_TEST_TMPDIR").unwrap());
let path = path.join("switch-stdout-output");
let f = File::create(&path).unwrap();
println!("foo");
std::io::stdout().flush().unwrap();
switch_stdout_to(f);
println!("bar");
std::io::stdout().flush().unwrap();
let mut contents = String::new();
File::open(&path).unwrap().read_to_string(&mut contents).unwrap();
assert_eq!(contents, "bar\n");
}