File tree Expand file tree Collapse file tree 2 files changed +87
-0
lines changed
Expand file tree Collapse file tree 2 files changed +87
-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+ use std:: fs:: File ;
2+ use std:: io:: { Read , Write } ;
3+
4+ mod common;
5+
6+ #[ cfg( unix) ]
7+ fn switch_stdout_to ( file : File ) {
8+ use std:: os:: unix:: prelude:: * ;
9+
10+ extern "C" {
11+ fn dup2 ( old : i32 , new : i32 ) -> i32 ;
12+ }
13+
14+ unsafe {
15+ assert_eq ! ( dup2( file. as_raw_fd( ) , 1 ) , 1 ) ;
16+ }
17+ }
18+
19+ #[ cfg( windows) ]
20+ fn switch_stdout_to ( file : File ) {
21+ use std:: os:: windows:: prelude:: * ;
22+
23+ extern "system" {
24+ fn SetStdHandle ( nStdHandle : u32 , handle : * mut u8 ) -> i32 ;
25+ }
26+
27+ const STD_OUTPUT_HANDLE : u32 = ( -11i32 ) as u32 ;
28+
29+ unsafe {
30+ let rc = SetStdHandle ( STD_OUTPUT_HANDLE , file. into_raw_handle ( ) as * mut _ ) ;
31+ assert ! ( rc != 0 ) ;
32+ }
33+ }
34+
35+ #[ test]
36+ fn switch_stdout ( ) {
37+ let temp = common:: tmpdir ( ) ;
38+ let path = temp. join ( "switch-stdout-output" ) ;
39+ let f = File :: create ( & path) . unwrap ( ) ;
40+
41+ let mut stdout = std:: io:: stdout ( ) ;
42+ stdout. write ( b"foo\n " ) . unwrap ( ) ;
43+ stdout. flush ( ) . unwrap ( ) ;
44+ switch_stdout_to ( f) ;
45+ stdout. write ( b"bar\n " ) . unwrap ( ) ;
46+ stdout. flush ( ) . unwrap ( ) ;
47+
48+ let mut contents = String :: new ( ) ;
49+ File :: open ( & path) . unwrap ( ) . read_to_string ( & mut contents) . unwrap ( ) ;
50+ assert_eq ! ( contents, "bar\n " ) ;
51+ }
You can’t perform that action at this time.
0 commit comments