File tree Expand file tree Collapse file tree 2 files changed +89
-0
lines changed Expand file tree Collapse file tree 2 files changed +89
-0
lines changed Original file line number Diff line number Diff line change
1
+ use std::env;
2
+ use std::fs;
3
+ use std::process;
4
+ use std::str;
5
+
6
+ mod common;
7
+
8
+ #[test]
9
+ fn issue_15149() {
10
+ // If we're the parent, copy our own binary to a new directory.
11
+ let my_path = env::current_exe().unwrap();
12
+
13
+ let temp = common::tmpdir();
14
+ let child_dir = temp.join("issue-15140-child");
15
+ fs::create_dir_all(&child_dir).unwrap();
16
+
17
+ let child_path = child_dir.join(&format!("mytest{}", env::consts::EXE_SUFFIX));
18
+ fs::copy(&my_path, &child_path).unwrap();
19
+
20
+ // Append the new directory to our own PATH.
21
+ let path = {
22
+ let mut paths: Vec<_> = env::split_paths(&env::var_os("PATH").unwrap()).collect();
23
+ paths.push(child_dir.to_path_buf());
24
+ env::join_paths(paths).unwrap()
25
+ };
26
+
27
+ let child_output =
28
+ process::Command::new("mytest").env("PATH", &path).arg("child").output().unwrap();
29
+
30
+ assert!(
31
+ child_output.status.success(),
32
+ "child assertion failed\n child stdout:\n {}\n child stderr:\n {}",
33
+ str::from_utf8(&child_output.stdout).unwrap(),
34
+ str::from_utf8(&child_output.stderr).unwrap()
35
+ );
36
+ }
Original file line number Diff line number Diff line change
1
+ #[cfg(any(target_family = "unix", target_family = "windows"))]
2
+
3
+ use std::fs::File;
4
+ use std::io::{Read, Write};
5
+
6
+ mod common;
7
+
8
+ #[cfg(unix)]
9
+ fn switch_stdout_to(file: File) {
10
+ use std::os::unix::prelude::*;
11
+
12
+ extern "C" {
13
+ fn dup2(old: i32, new: i32) -> i32;
14
+ }
15
+
16
+ unsafe {
17
+ assert_eq!(dup2(file.as_raw_fd(), 1), 1);
18
+ }
19
+ }
20
+
21
+ #[cfg(windows)]
22
+ fn switch_stdout_to(file: File) {
23
+ use std::os::windows::prelude::*;
24
+
25
+ extern "system" {
26
+ fn SetStdHandle(nStdHandle: u32, handle: *mut u8) -> i32;
27
+ }
28
+
29
+ const STD_OUTPUT_HANDLE: u32 = (-11i32) as u32;
30
+
31
+ unsafe {
32
+ let rc = SetStdHandle(STD_OUTPUT_HANDLE, file.into_raw_handle() as *mut _);
33
+ assert!(rc != 0);
34
+ }
35
+ }
36
+
37
+ #[test]
38
+ fn switch_stdout() {
39
+ let temp = common::tmpdir();
40
+ let path = temp.join("switch-stdout-output");
41
+ let f = File::create(&path).unwrap();
42
+
43
+ let mut stdout = std::io::stdout();
44
+ stdout.write(b"foo\n").unwrap();
45
+ stdout.flush().unwrap();
46
+ switch_stdout_to(f);
47
+ stdout.write(b"bar\n").unwrap();
48
+ stdout.flush().unwrap();
49
+
50
+ let mut contents = String::new();
51
+ File::open(&path).unwrap().read_to_string(&mut contents).unwrap();
52
+ assert_eq!(contents, "bar\n");
53
+ }
You can’t perform that action at this time.
0 commit comments