11use std:: env;
2+ use std:: io:: Write ;
23use std:: path:: Path ;
3- use std:: process:: { Command , Output } ;
4+ use std:: process:: { Command , Output , Stdio } ;
45
56use crate :: { handle_failed_output, set_host_rpath} ;
67
@@ -17,6 +18,7 @@ pub fn rustdoc() -> Rustdoc {
1718#[ derive( Debug ) ]
1819pub struct Rustdoc {
1920 cmd : Command ,
21+ stdin : Option < Box < [ u8 ] > > ,
2022}
2123
2224crate :: impl_common_helpers!( Rustdoc ) ;
@@ -32,15 +34,15 @@ impl Rustdoc {
3234 /// Construct a bare `rustdoc` invocation.
3335 pub fn bare ( ) -> Self {
3436 let cmd = setup_common ( ) ;
35- Self { cmd }
37+ Self { cmd, stdin : None }
3638 }
3739
3840 /// Construct a `rustdoc` invocation with `-L $(TARGET_RPATH_DIR)` set.
3941 pub fn new ( ) -> Self {
4042 let mut cmd = setup_common ( ) ;
4143 let target_rpath_dir = env:: var_os ( "TARGET_RPATH_DIR" ) . unwrap ( ) ;
4244 cmd. arg ( format ! ( "-L{}" , target_rpath_dir. to_string_lossy( ) ) ) ;
43- Self { cmd }
45+ Self { cmd, stdin : None }
4446 }
4547
4648 /// Specify path to the input file.
@@ -62,12 +64,41 @@ impl Rustdoc {
6264 self
6365 }
6466
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+
6596 #[ track_caller]
6697 pub fn run_fail_assert_exit_code ( & mut self , code : i32 ) -> Output {
6798 let caller_location = std:: panic:: Location :: caller ( ) ;
6899 let caller_line_number = caller_location. line ( ) ;
69100
70- let output = self . cmd . output ( ) . unwrap ( ) ;
101+ let output = self . output ( ) ;
71102 if output. status . code ( ) . unwrap ( ) != code {
72103 handle_failed_output ( & self . cmd , output, caller_line_number) ;
73104 }
0 commit comments