From 2f093c67eadbf763601fd195dfc0d3e0c421fe82 Mon Sep 17 00:00:00 2001 From: MoskalykA <100430077+MoskalykA@users.noreply.github.com> Date: Wed, 7 Jun 2023 18:40:25 +0200 Subject: Move two tests from `tests/ui/std` to `library/std/tests` --- library/std/tests/issue-15149.rs | 36 ++++++++++++++++++++++++ library/std/tests/switch-stdout.rs | 51 ++++++++++++++++++++++++++++++++++ tests/ui/std/issue-15149.rs | 57 -------------------------------------- tests/ui/std/switch-stdout.rs | 52 ---------------------------------- 4 files changed, 87 insertions(+), 109 deletions(-) create mode 100644 library/std/tests/issue-15149.rs create mode 100644 library/std/tests/switch-stdout.rs delete mode 100644 tests/ui/std/issue-15149.rs delete mode 100644 tests/ui/std/switch-stdout.rs diff --git a/library/std/tests/issue-15149.rs b/library/std/tests/issue-15149.rs new file mode 100644 index 00000000000..52e5b55fb9d --- /dev/null +++ b/library/std/tests/issue-15149.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..2f01504043d --- /dev/null +++ b/library/std/tests/switch-stdout.rs @@ -0,0 +1,51 @@ +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"); +} diff --git a/tests/ui/std/issue-15149.rs b/tests/ui/std/issue-15149.rs deleted file mode 100644 index 064472f5785..00000000000 --- a/tests/ui/std/issue-15149.rs +++ /dev/null @@ -1,57 +0,0 @@ -// run-pass - -#![allow(unused_variables)] -// no-prefer-dynamic -// ignore-cross-compile - -use std::env; -use std::ffi::OsStr; -use std::fs; -use std::path::PathBuf; -use std::process; -use std::str; - -fn main() { - // If we're the child, make sure we were invoked correctly - let args: Vec = env::args().collect(); - if args.len() > 1 && args[1] == "child" { - // FIXME: This should check the whole `args[0]` instead of just - // checking that it ends_with the executable name. This - // is needed because of Windows, which has a different behavior. - // See #15149 for more info. - let my_path = env::current_exe().unwrap(); - return assert_eq!(my_path.file_stem(), Some(OsStr::new("mytest"))); - } - - test(); -} - -fn test() { - // If we're the parent, copy our own binary to a new directory. - let my_path = env::current_exe().unwrap(); - let my_dir = my_path.parent().unwrap(); - - let child_dir = PathBuf::from(env::var_os("RUST_TEST_TMPDIR").unwrap()); - let child_dir = child_dir.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/tests/ui/std/switch-stdout.rs b/tests/ui/std/switch-stdout.rs deleted file mode 100644 index 2d936d96b05..00000000000 --- a/tests/ui/std/switch-stdout.rs +++ /dev/null @@ -1,52 +0,0 @@ -// 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"); -} -- cgit 1.4.1-3-g733a5 From 4227c0a406f31b7a2b2f4423541234d480a66e70 Mon Sep 17 00:00:00 2001 From: MoskalykA <100430077+MoskalykA@users.noreply.github.com> Date: Wed, 7 Jun 2023 21:51:57 +0200 Subject: Make Unix or Windows mandatory for the `switch-stdout.rs` test --- library/std/tests/switch-stdout.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/library/std/tests/switch-stdout.rs b/library/std/tests/switch-stdout.rs index 2f01504043d..28ce6dfccd3 100644 --- a/library/std/tests/switch-stdout.rs +++ b/library/std/tests/switch-stdout.rs @@ -1,3 +1,5 @@ +#[cfg(any(target_family = "unix", target_family = "windows"))] + use std::fs::File; use std::io::{Read, Write}; -- cgit 1.4.1-3-g733a5 From 48e7f3e313c19d8df1a05f0ccc17ac02e41dda6a Mon Sep 17 00:00:00 2001 From: MoskalykA <100430077+MoskalykA@users.noreply.github.com> Date: Wed, 26 Jul 2023 15:07:57 +0200 Subject: Have a better file name than just the issue id --- library/std/tests/issue-15149.rs | 36 ----------------------------------- library/std/tests/process_spawning.rs | 36 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 36 deletions(-) delete mode 100644 library/std/tests/issue-15149.rs create mode 100644 library/std/tests/process_spawning.rs diff --git a/library/std/tests/issue-15149.rs b/library/std/tests/issue-15149.rs deleted file mode 100644 index 52e5b55fb9d..00000000000 --- a/library/std/tests/issue-15149.rs +++ /dev/null @@ -1,36 +0,0 @@ -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/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() + ); +} -- cgit 1.4.1-3-g733a5