summaryrefslogtreecommitdiffstats
path: root/library/std/tests
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 03:59:24 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 03:59:24 +0000
commit023939b627b7dc93b01471f7d41fb8553ddb4ffa (patch)
tree60fc59477c605c72b0a1051409062ddecc43f877 /library/std/tests
parentAdding debian version 1.72.1+dfsg1-1. (diff)
downloadrustc-023939b627b7dc93b01471f7d41fb8553ddb4ffa.tar.xz
rustc-023939b627b7dc93b01471f7d41fb8553ddb4ffa.zip
Merging upstream version 1.73.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'library/std/tests')
-rw-r--r--library/std/tests/process_spawning.rs38
-rw-r--r--library/std/tests/switch-stdout.rs53
2 files changed, 91 insertions, 0 deletions
diff --git a/library/std/tests/process_spawning.rs b/library/std/tests/process_spawning.rs
new file mode 100644
index 000000000..46dc9ff00
--- /dev/null
+++ b/library/std/tests/process_spawning.rs
@@ -0,0 +1,38 @@
+#![cfg(not(target_env="sgx"))]
+
+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 000000000..2605664d2
--- /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");
+}