@@ -458,11 +458,7 @@ impl<'test> TestCx<'test> {
458
458
None => 2 ,
459
459
} ;
460
460
461
- let mut src = String :: new ( ) ;
462
- File :: open ( & self . testpaths . file )
463
- . unwrap ( )
464
- . read_to_string ( & mut src)
465
- . unwrap ( ) ;
461
+ let src = fs:: read_to_string ( & self . testpaths . file ) . unwrap ( ) ;
466
462
let mut srcs = vec ! [ src] ;
467
463
468
464
let mut round = 0 ;
@@ -500,12 +496,7 @@ impl<'test> TestCx<'test> {
500
496
let mut expected = match self . props . pp_exact {
501
497
Some ( ref file) => {
502
498
let filepath = self . testpaths . file . parent ( ) . unwrap ( ) . join ( file) ;
503
- let mut s = String :: new ( ) ;
504
- File :: open ( & filepath)
505
- . unwrap ( )
506
- . read_to_string ( & mut s)
507
- . unwrap ( ) ;
508
- s
499
+ fs:: read_to_string ( & filepath) . unwrap ( )
509
500
}
510
501
None => srcs[ srcs. len ( ) - 2 ] . clone ( ) ,
511
502
} ;
@@ -1949,10 +1940,7 @@ impl<'test> TestCx<'test> {
1949
1940
1950
1941
fn dump_output_file ( & self , out : & str , extension : & str ) {
1951
1942
let outfile = self . make_out_name ( extension) ;
1952
- File :: create ( & outfile)
1953
- . unwrap ( )
1954
- . write_all ( out. as_bytes ( ) )
1955
- . unwrap ( ) ;
1943
+ fs:: write ( & outfile, out) . unwrap ( ) ;
1956
1944
}
1957
1945
1958
1946
/// Create a filename for output with the given extension. Example:
@@ -2149,11 +2137,7 @@ impl<'test> TestCx<'test> {
2149
2137
path : & P ,
2150
2138
mut other_files : Option < & mut Vec < String > > ,
2151
2139
) -> Vec < usize > {
2152
- let mut file =
2153
- fs:: File :: open ( path) . expect ( "markdown_test_output_check_entry File::open failed" ) ;
2154
- let mut content = String :: new ( ) ;
2155
- file. read_to_string ( & mut content)
2156
- . expect ( "markdown_test_output_check_entry read_to_string failed" ) ;
2140
+ let content = fs:: read_to_string ( & path) . unwrap ( ) ;
2157
2141
let mut ignore = false ;
2158
2142
content
2159
2143
. lines ( )
@@ -2826,11 +2810,7 @@ impl<'test> TestCx<'test> {
2826
2810
}
2827
2811
2828
2812
fn check_mir_dump ( & self ) {
2829
- let mut test_file_contents = String :: new ( ) ;
2830
- fs:: File :: open ( self . testpaths . file . clone ( ) )
2831
- . unwrap ( )
2832
- . read_to_string ( & mut test_file_contents)
2833
- . unwrap ( ) ;
2813
+ let test_file_contents = fs:: read_to_string ( & self . testpaths . file ) . unwrap ( ) ;
2834
2814
if let Some ( idx) = test_file_contents. find ( "// END RUST SOURCE" ) {
2835
2815
let ( _, tests_text) = test_file_contents. split_at ( idx + "// END_RUST SOURCE" . len ( ) ) ;
2836
2816
let tests_text_str = String :: from ( tests_text) ;
@@ -2894,9 +2874,7 @@ impl<'test> TestCx<'test> {
2894
2874
}
2895
2875
self . check_mir_test_timestamp ( test_name, & output_file) ;
2896
2876
2897
- let mut dumped_file = fs:: File :: open ( output_file. clone ( ) ) . unwrap ( ) ;
2898
- let mut dumped_string = String :: new ( ) ;
2899
- dumped_file. read_to_string ( & mut dumped_string) . unwrap ( ) ;
2877
+ let dumped_string = fs:: read_to_string ( & output_file) . unwrap ( ) ;
2900
2878
let mut dumped_lines = dumped_string
2901
2879
. lines ( )
2902
2880
. map ( |l| nocomment_mir_line ( l) )
@@ -3108,19 +3086,13 @@ impl<'test> TestCx<'test> {
3108
3086
}
3109
3087
3110
3088
fn load_expected_output_from_path ( & self , path : & Path ) -> Result < String , String > {
3111
- let mut result = String :: new ( ) ;
3112
- match File :: open ( path) . and_then ( |mut f| f. read_to_string ( & mut result) ) {
3113
- Ok ( _) => Ok ( result) ,
3114
- Err ( e) => Err ( format ! (
3115
- "failed to load expected output from `{}`: {}" ,
3116
- path. display( ) ,
3117
- e
3118
- ) ) ,
3119
- }
3089
+ fs:: read_to_string ( path) . map_err ( |err| {
3090
+ format ! ( "failed to load expected output from `{}`: {}" , path. display( ) , err)
3091
+ } )
3120
3092
}
3121
3093
3122
3094
fn delete_file ( & self , file : & PathBuf ) {
3123
- if let Err ( e) = :: std :: fs:: remove_file ( file) {
3095
+ if let Err ( e) = fs:: remove_file ( file) {
3124
3096
self . fatal ( & format ! (
3125
3097
"failed to delete `{}`: {}" ,
3126
3098
file. display( ) ,
@@ -3182,16 +3154,13 @@ impl<'test> TestCx<'test> {
3182
3154
for output_file in & files {
3183
3155
if actual. is_empty ( ) {
3184
3156
self . delete_file ( output_file) ;
3185
- } else {
3186
- match File :: create ( & output_file) . and_then ( |mut f| f. write_all ( actual. as_bytes ( ) ) ) {
3187
- Ok ( ( ) ) => { }
3188
- Err ( e) => self . fatal ( & format ! (
3189
- "failed to write {} to `{}`: {}" ,
3190
- kind,
3191
- output_file. display( ) ,
3192
- e
3193
- ) ) ,
3194
- }
3157
+ } else if let Err ( err) = fs:: write ( & output_file, & actual) {
3158
+ self . fatal ( & format ! (
3159
+ "failed to write {} to `{}`: {}" ,
3160
+ kind,
3161
+ output_file. display( ) ,
3162
+ err,
3163
+ ) ) ;
3195
3164
}
3196
3165
}
3197
3166
@@ -3243,9 +3212,8 @@ impl<'test> TestCx<'test> {
3243
3212
}
3244
3213
3245
3214
fn create_stamp ( & self ) {
3246
- let mut f = File :: create ( :: stamp ( & self . config , self . testpaths , self . revision ) ) . unwrap ( ) ;
3247
- f. write_all ( compute_stamp_hash ( & self . config ) . as_bytes ( ) )
3248
- . unwrap ( ) ;
3215
+ let stamp = :: stamp ( & self . config , self . testpaths , self . revision ) ;
3216
+ fs:: write ( & stamp, compute_stamp_hash ( & self . config ) ) . unwrap ( ) ;
3249
3217
}
3250
3218
}
3251
3219
0 commit comments