2
2
3
3
use std:: env;
4
4
use std:: fs:: remove_file;
5
+ use std:: io:: { Result , Write } ;
5
6
use std:: path:: Path ;
6
- use std:: process:: Command ;
7
+ use std:: process:: { Command , Output , Stdio } ;
7
8
8
9
/// Run the rustfmt executable and return its output.
9
10
fn rustfmt ( args : & [ & str ] ) -> ( String , String ) {
11
+ match rustfm_builder ( |rustfmt| rustfmt. args ( args) . output ( ) ) {
12
+ Ok ( output) => (
13
+ String :: from_utf8 ( output. stdout ) . expect ( "utf-8" ) ,
14
+ String :: from_utf8 ( output. stderr ) . expect ( "utf-8" ) ,
15
+ ) ,
16
+ Err ( e) => panic ! ( "failed to run `rustfmt {:?}`: {}" , args, e) ,
17
+ }
18
+ }
19
+
20
+ /// Run the rustfmt executable and take input from stdin
21
+ fn rustfmt_std_input ( args : & [ & str ] , input : & str ) -> ( String , String ) {
22
+ let output = rustfm_builder ( |cmd| {
23
+ let mut rustfmt = cmd
24
+ . args ( args)
25
+ . stdin ( Stdio :: piped ( ) )
26
+ . stdout ( Stdio :: piped ( ) )
27
+ . stderr ( Stdio :: piped ( ) )
28
+ . spawn ( )
29
+ . unwrap ( ) ;
30
+
31
+ rustfmt
32
+ . stdin
33
+ . as_mut ( )
34
+ . unwrap ( )
35
+ . write_all ( input. as_bytes ( ) ) ?;
36
+ rustfmt. wait_with_output ( )
37
+ } ) ;
38
+ match output {
39
+ Ok ( output) => (
40
+ String :: from_utf8 ( output. stdout ) . expect ( "utf-8" ) ,
41
+ String :: from_utf8 ( output. stderr ) . expect ( "utf-8" ) ,
42
+ ) ,
43
+ Err ( e) => panic ! ( "failed to run `rustfmt {:?}`: {}" , args, e) ,
44
+ }
45
+ }
46
+
47
+ fn rustfm_builder < F : Fn ( & mut Command ) -> Result < Output > > ( f : F ) -> Result < Output > {
10
48
let mut bin_dir = env:: current_exe ( ) . unwrap ( ) ;
11
49
bin_dir. pop ( ) ; // chop off test exe name
12
50
if bin_dir. ends_with ( "deps" ) {
@@ -20,13 +58,8 @@ fn rustfmt(args: &[&str]) -> (String, String) {
20
58
paths. insert ( 0 , bin_dir) ;
21
59
let new_path = env:: join_paths ( paths) . unwrap ( ) ;
22
60
23
- match Command :: new ( & cmd) . args ( args) . env ( "PATH" , new_path) . output ( ) {
24
- Ok ( output) => (
25
- String :: from_utf8 ( output. stdout ) . expect ( "utf-8" ) ,
26
- String :: from_utf8 ( output. stderr ) . expect ( "utf-8" ) ,
27
- ) ,
28
- Err ( e) => panic ! ( "failed to run `{:?} {:?}`: {}" , cmd, args, e) ,
29
- }
61
+ let mut rustfmt = Command :: new ( & cmd) ;
62
+ f ( rustfmt. env ( "PATH" , new_path) )
30
63
}
31
64
32
65
macro_rules! assert_that {
@@ -157,3 +190,29 @@ fn mod_resolution_error_path_attribute_does_not_exist() {
157
190
// The path attribute points to a file that does not exist
158
191
assert ! ( stderr. contains( "does_not_exist.rs does not exist" ) ) ;
159
192
}
193
+
194
+ mod rustfmt_stdin_formatting {
195
+ use super :: rustfmt_std_input;
196
+
197
+ #[ rustfmt:: skip]
198
+ #[ test]
199
+ fn changes_are_output_to_stdout ( ) {
200
+ let args = [ ] ;
201
+ let source = "fn main () { println!(\" hello world!\" ); }" ;
202
+ let ( stdout, _stderr) = rustfmt_std_input ( & args, source) ;
203
+ let expected_output =
204
+ r#"fn main() {
205
+ println!("hello world!");
206
+ }"# ;
207
+ assert ! ( stdout. contains( expected_output) )
208
+ }
209
+
210
+ #[ test]
211
+ fn properly_formatted_input_is_output_to_stdout_unchanged ( ) {
212
+ // NOTE: Technicallly a newline is added, but nothing meaningful is changed
213
+ let args = [ ] ;
214
+ let source = "fn main() {}" ;
215
+ let ( stdout, _stderr) = rustfmt_std_input ( & args, source) ;
216
+ assert ! ( stdout. trim_end( ) == source)
217
+ }
218
+ }
0 commit comments