diff options
| author | bors <bors@rust-lang.org> | 2023-07-28 05:00:22 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2023-07-28 05:00:22 +0000 |
| commit | e40e22be550fdcf5f11ba95cc84f3f337b0695f4 (patch) | |
| tree | 93b7509b28f3bad452e3e40413c9a111579ee2d7 /library/std | |
| parent | 0ca28c3da75b165cc3e4870a1f397a23d2a58857 (diff) | |
| parent | 48e7f3e313c19d8df1a05f0ccc17ac02e41dda6a (diff) | |
Auto merge of #112390 - MoskalykA:move-two-tests-from-library-to-tests, r=workingjubilee
Move two tests from `tests/ui/std` to `library/std/tests` Hi, there, This pull request comes from this issue (#99417), sorry I made some mistakes creating the pull request, it's my first one.
Diffstat (limited to 'library/std')
| -rw-r--r-- | library/std/tests/process_spawning.rs | 36 | ||||
| -rw-r--r-- | library/std/tests/switch-stdout.rs | 53 |
2 files changed, 89 insertions, 0 deletions
diff --git a/library/std/tests/process_spawning.rs b/library/std/tests/process_spawning.rs new file mode 100644 index 00000000000..52e5b55fb9d --- /dev/null +++ b/library/std/tests/process_spawning.rs @@ -0,0 +1,36 @@ +use std::env; +use std::fs; +use std::process; +use std::str; + +mod common; + +#[test] +fn issue_15149() { + // If we're the parent, copy our own binary to a new directory. + let my_path = env::current_exe().unwrap(); + + let temp = common::tmpdir(); + let child_dir = temp.join("issue-15140-child"); + fs::create_dir_all(&child_dir).unwrap(); + + let child_path = child_dir.join(&format!("mytest{}", env::consts::EXE_SUFFIX)); + fs::copy(&my_path, &child_path).unwrap(); + + // Append the new directory to our own PATH. + let path = { + let mut paths: Vec<_> = env::split_paths(&env::var_os("PATH").unwrap()).collect(); + paths.push(child_dir.to_path_buf()); + env::join_paths(paths).unwrap() + }; + + let child_output = + process::Command::new("mytest").env("PATH", &path).arg("child").output().unwrap(); + + assert!( + child_output.status.success(), + "child assertion failed\n child stdout:\n {}\n child stderr:\n {}", + str::from_utf8(&child_output.stdout).unwrap(), + str::from_utf8(&child_output.stderr).unwrap() + ); +} diff --git a/library/std/tests/switch-stdout.rs b/library/std/tests/switch-stdout.rs new file mode 100644 index 00000000000..28ce6dfccd3 --- /dev/null +++ b/library/std/tests/switch-stdout.rs @@ -0,0 +1,53 @@ +#[cfg(any(target_family = "unix", target_family = "windows"))] + +use std::fs::File; +use std::io::{Read, Write}; + +mod common; + +#[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); + } +} + +#[test] +fn switch_stdout() { + let temp = common::tmpdir(); + let path = temp.join("switch-stdout-output"); + let f = File::create(&path).unwrap(); + + let mut stdout = std::io::stdout(); + stdout.write(b"foo\n").unwrap(); + stdout.flush().unwrap(); + switch_stdout_to(f); + stdout.write(b"bar\n").unwrap(); + stdout.flush().unwrap(); + + let mut contents = String::new(); + File::open(&path).unwrap().read_to_string(&mut contents).unwrap(); + assert_eq!(contents, "bar\n"); +} |
