1
1
use std:: env;
2
+ use std:: io:: Write ;
2
3
use std:: path:: Path ;
3
- use std:: process:: { Command , Output } ;
4
+ use std:: process:: { Command , Output , Stdio } ;
4
5
5
6
use crate :: { handle_failed_output, set_host_rpath} ;
6
7
@@ -17,6 +18,7 @@ pub fn rustdoc() -> Rustdoc {
17
18
#[ derive( Debug ) ]
18
19
pub struct Rustdoc {
19
20
cmd : Command ,
21
+ stdin : Option < Box < [ u8 ] > > ,
20
22
}
21
23
22
24
crate :: impl_common_helpers!( Rustdoc ) ;
@@ -32,15 +34,15 @@ impl Rustdoc {
32
34
/// Construct a bare `rustdoc` invocation.
33
35
pub fn bare ( ) -> Self {
34
36
let cmd = setup_common ( ) ;
35
- Self { cmd }
37
+ Self { cmd, stdin : None }
36
38
}
37
39
38
40
/// Construct a `rustdoc` invocation with `-L $(TARGET_RPATH_DIR)` set.
39
41
pub fn new ( ) -> Self {
40
42
let mut cmd = setup_common ( ) ;
41
43
let target_rpath_dir = env:: var_os ( "TARGET_RPATH_DIR" ) . unwrap ( ) ;
42
44
cmd. arg ( format ! ( "-L{}" , target_rpath_dir. to_string_lossy( ) ) ) ;
43
- Self { cmd }
45
+ Self { cmd, stdin : None }
44
46
}
45
47
46
48
/// Specify path to the input file.
@@ -62,12 +64,41 @@ impl Rustdoc {
62
64
self
63
65
}
64
66
67
+ /// Specify a stdin input
68
+ pub fn stdin < I : AsRef < [ u8 ] > > ( & mut self , input : I ) -> & mut Self {
69
+ self . cmd . stdin ( Stdio :: piped ( ) ) ;
70
+ self . stdin = Some ( input. as_ref ( ) . to_vec ( ) . into_boxed_slice ( ) ) ;
71
+ self
72
+ }
73
+
74
+ /// Get the [`Output`][::std::process::Output] of the finished process.
75
+ #[ track_caller]
76
+ pub fn output ( & mut self ) -> :: std:: process:: Output {
77
+ // let's make sure we piped all the input and outputs
78
+ self . cmd . stdin ( Stdio :: piped ( ) ) ;
79
+ self . cmd . stdout ( Stdio :: piped ( ) ) ;
80
+ self . cmd . stderr ( Stdio :: piped ( ) ) ;
81
+
82
+ if let Some ( input) = & self . stdin {
83
+ let mut child = self . cmd . spawn ( ) . unwrap ( ) ;
84
+
85
+ {
86
+ let mut stdin = child. stdin . take ( ) . unwrap ( ) ;
87
+ stdin. write_all ( input. as_ref ( ) ) . unwrap ( ) ;
88
+ }
89
+
90
+ child. wait_with_output ( ) . expect ( "failed to get output of finished process" )
91
+ } else {
92
+ self . cmd . output ( ) . expect ( "failed to get output of finished process" )
93
+ }
94
+ }
95
+
65
96
#[ track_caller]
66
97
pub fn run_fail_assert_exit_code ( & mut self , code : i32 ) -> Output {
67
98
let caller_location = std:: panic:: Location :: caller ( ) ;
68
99
let caller_line_number = caller_location. line ( ) ;
69
100
70
- let output = self . cmd . output ( ) . unwrap ( ) ;
101
+ let output = self . output ( ) ;
71
102
if output. status . code ( ) . unwrap ( ) != code {
72
103
handle_failed_output ( & self . cmd , output, caller_line_number) ;
73
104
}
0 commit comments