|
2 | 2 |
|
3 | 3 | use std::env;
|
4 | 4 | use std::fs::File;
|
5 |
| -use std::io::prelude::*; |
| 5 | +use std::io::{self, prelude::*}; |
6 | 6 | use std::path::PathBuf;
|
7 | 7 |
|
8 | 8 | fn main() {
|
9 | 9 | let mut args = env::args();
|
10 | 10 | let program = args.next().expect("Unexpected empty args");
|
11 | 11 |
|
12 | 12 | let out_dir = PathBuf::from(
|
13 |
| - env::var_os("GCCTEST_OUT_DIR").expect(&format!("{}: GCCTEST_OUT_DIR not found", program)), |
| 13 | + env::var_os("GCCTEST_OUT_DIR") |
| 14 | + .unwrap_or_else(|| panic!("{}: GCCTEST_OUT_DIR not found", program)), |
14 | 15 | );
|
15 | 16 |
|
16 | 17 | // Find the first nonexistent candidate file to which the program's args can be written.
|
17 |
| - for i in 0.. { |
18 |
| - let candidate = &out_dir.join(format!("out{}", i)); |
| 18 | + let candidate = (0..).find_map(|i| { |
| 19 | + let candidate = out_dir.join(format!("out{}", i)); |
19 | 20 |
|
20 |
| - // If the file exists, commands have already run. Try again. |
21 | 21 | if candidate.exists() {
|
22 |
| - continue; |
| 22 | + // If the file exists, commands have already run. Try again. |
| 23 | + None |
| 24 | + } else { |
| 25 | + Some(candidate) |
23 | 26 | }
|
| 27 | + }).unwrap_or_else(|| panic!("Cannot find the first nonexistent candidate file to which the program's args can be written under out_dir '{}'", out_dir.display())); |
24 | 28 |
|
25 |
| - // Create a file and record the args passed to the command. |
26 |
| - let mut f = File::create(candidate).expect(&format!( |
27 |
| - "{}: can't create candidate: {}", |
| 29 | + // Create a file and record the args passed to the command. |
| 30 | + let f = File::create(&candidate).unwrap_or_else(|e| { |
| 31 | + panic!( |
| 32 | + "{}: can't create candidate: {}, error: {}", |
28 | 33 | program,
|
29 |
| - candidate.to_string_lossy() |
30 |
| - )); |
| 34 | + candidate.display(), |
| 35 | + e |
| 36 | + ) |
| 37 | + }); |
| 38 | + let mut f = io::BufWriter::new(f); |
| 39 | + |
| 40 | + (|| { |
31 | 41 | for arg in args {
|
32 |
| - writeln!(f, "{}", arg).expect(&format!( |
33 |
| - "{}: can't write to candidate: {}", |
34 |
| - program, |
35 |
| - candidate.to_string_lossy() |
36 |
| - )); |
| 42 | + writeln!(f, "{}", arg)?; |
37 | 43 | }
|
38 |
| - break; |
39 |
| - } |
| 44 | + |
| 45 | + f.flush()?; |
| 46 | + |
| 47 | + let mut f = f.into_inner()?; |
| 48 | + f.flush()?; |
| 49 | + f.sync_all() |
| 50 | + })() |
| 51 | + .unwrap_or_else(|e| { |
| 52 | + panic!( |
| 53 | + "{}: can't write to candidate: {}, error: {}", |
| 54 | + program, |
| 55 | + candidate.display(), |
| 56 | + e |
| 57 | + ) |
| 58 | + }); |
40 | 59 |
|
41 | 60 | // Create a file used by some tests.
|
42 | 61 | let path = &out_dir.join("libfoo.a");
|
43 |
| - File::create(path).expect(&format!( |
44 |
| - "{}: can't create libfoo.a: {}", |
45 |
| - program, |
46 |
| - path.to_string_lossy() |
47 |
| - )); |
| 62 | + File::create(path).unwrap_or_else(|e| { |
| 63 | + panic!( |
| 64 | + "{}: can't create libfoo.a: {}, error: {}", |
| 65 | + program, |
| 66 | + path.display(), |
| 67 | + e |
| 68 | + ) |
| 69 | + }); |
48 | 70 | }
|
0 commit comments