Skip to content

Commit 86572d4

Browse files
committed
Merge pull request #793 from kamalmarhubi/expect-formatting
tests: Use Result::expect() throughout
2 parents edcc4ec + 2b991bc commit 86572d4

File tree

2 files changed

+22
-32
lines changed

2 files changed

+22
-32
lines changed

tests/system.rs

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ use rustfmt::rustfmt_diff::*;
2626
static DIFF_CONTEXT_SIZE: usize = 3;
2727

2828
fn get_path_string(dir_entry: io::Result<fs::DirEntry>) -> String {
29-
let path = dir_entry.ok().expect("Couldn't get DirEntry.").path();
29+
let path = dir_entry.expect("Couldn't get DirEntry").path();
3030

31-
path.to_str().expect("Couldn't stringify path.").to_owned()
31+
path.to_str().expect("Couldn't stringify path").to_owned()
3232
}
3333

3434
// Integration tests. The files in the tests/source are formatted and compared
@@ -40,7 +40,7 @@ fn get_path_string(dir_entry: io::Result<fs::DirEntry>) -> String {
4040
#[test]
4141
fn system_tests() {
4242
// Get all files in the tests/source directory.
43-
let files = fs::read_dir("tests/source").ok().expect("Couldn't read source dir.");
43+
let files = fs::read_dir("tests/source").expect("Couldn't read source dir");
4444
// Turn a DirEntry into a String that represents the relative path to the
4545
// file.
4646
let files = files.map(get_path_string);
@@ -55,7 +55,7 @@ fn system_tests() {
5555
// the only difference is the coverage mode
5656
#[test]
5757
fn coverage_tests() {
58-
let files = fs::read_dir("tests/coverage-source").ok().expect("Couldn't read source dir.");
58+
let files = fs::read_dir("tests/coverage-source").expect("Couldn't read source dir");
5959
let files = files.map(get_path_string);
6060
let (_reports, count, fails) = check_files(files, WriteMode::Coverage);
6161

@@ -82,13 +82,10 @@ fn assert_output(source: &str, expected_filename: &str, write_mode: WriteMode) {
8282
let _ = filemap::write_all_files(&file_map, &mut out, write_mode, &config);
8383
let output = String::from_utf8(out).unwrap();
8484

85-
let mut expected_file = fs::File::open(&expected_filename)
86-
.ok()
87-
.expect("Couldn't open target.");
85+
let mut expected_file = fs::File::open(&expected_filename).expect("Couldn't open target");
8886
let mut expected_text = String::new();
8987
expected_file.read_to_string(&mut expected_text)
90-
.ok()
91-
.expect("Failed reading target.");
88+
.expect("Failed reading target");
9289

9390
let compare = make_diff(&expected_text, &output, DIFF_CONTEXT_SIZE);
9491
if compare.len() > 0 {
@@ -105,8 +102,7 @@ fn assert_output(source: &str, expected_filename: &str, write_mode: WriteMode) {
105102
fn idempotence_tests() {
106103
// Get all files in the tests/target directory.
107104
let files = fs::read_dir("tests/target")
108-
.ok()
109-
.expect("Couldn't read target dir.")
105+
.expect("Couldn't read target dir")
110106
.map(get_path_string);
111107
let (_reports, count, fails) = check_files(files, WriteMode::Default);
112108

@@ -120,9 +116,8 @@ fn idempotence_tests() {
120116
#[test]
121117
fn self_tests() {
122118
let files = fs::read_dir("src/bin")
123-
.ok()
124-
.expect("Couldn't read src dir.")
125-
.chain(fs::read_dir("tests").ok().expect("Couldn't read tests dir."))
119+
.expect("Couldn't read src dir")
120+
.chain(fs::read_dir("tests").expect("Couldn't read tests dir"))
126121
.map(get_path_string);
127122
// Hack because there's no `IntoIterator` impl for `[T; N]`.
128123
let files = files.chain(Some("src/lib.rs".to_owned()).into_iter());
@@ -236,37 +231,32 @@ fn get_config(config_file: Option<&str>) -> Config {
236231
}
237232
};
238233

239-
let mut def_config_file = fs::File::open(config_file_name)
240-
.ok()
241-
.expect("Couldn't open config.");
234+
let mut def_config_file = fs::File::open(config_file_name).expect("Couldn't open config");
242235
let mut def_config = String::new();
243-
def_config_file.read_to_string(&mut def_config).ok().expect("Couldn't read config.");
236+
def_config_file.read_to_string(&mut def_config).expect("Couldn't read config");
244237

245238
Config::from_toml(&def_config)
246239
}
247240

248241
// Reads significant comments of the form: // rustfmt-key: value
249242
// into a hash map.
250243
fn read_significant_comments(file_name: &str) -> HashMap<String, String> {
251-
let file = fs::File::open(file_name)
252-
.ok()
253-
.expect(&format!("Couldn't read file {}.", file_name));
244+
let file = fs::File::open(file_name).expect(&format!("Couldn't read file {}", file_name));
254245
let reader = BufReader::new(file);
255246
let pattern = r"^\s*//\s*rustfmt-([^:]+):\s*(\S+)";
256-
let regex = regex::Regex::new(&pattern).ok().expect("Failed creating pattern 1.");
247+
let regex = regex::Regex::new(&pattern).expect("Failed creating pattern 1");
257248

258249
// Matches lines containing significant comments or whitespace.
259250
let line_regex = regex::Regex::new(r"(^\s*$)|(^\s*//\s*rustfmt-[^:]+:\s*\S+)")
260-
.ok()
261-
.expect("Failed creating pattern 2.");
251+
.expect("Failed creating pattern 2");
262252

263253
reader.lines()
264-
.map(|line| line.ok().expect("Failed getting line."))
254+
.map(|line| line.expect("Failed getting line"))
265255
.take_while(|line| line_regex.is_match(&line))
266256
.filter_map(|line| {
267257
regex.captures_iter(&line).next().map(|capture| {
268-
(capture.at(1).expect("Couldn't unwrap capture.").to_owned(),
269-
capture.at(2).expect("Couldn't unwrap capture.").to_owned())
258+
(capture.at(1).expect("Couldn't unwrap capture").to_owned(),
259+
capture.at(2).expect("Couldn't unwrap capture").to_owned())
270260
})
271261
})
272262
.collect()
@@ -283,10 +273,10 @@ fn handle_result(result: HashMap<String, String>,
283273
for (file_name, fmt_text) in result {
284274
// If file is in tests/source, compare to file with same name in tests/target.
285275
let target = get_target(&file_name, target, write_mode);
286-
let mut f = fs::File::open(&target).ok().expect("Couldn't open target.");
276+
let mut f = fs::File::open(&target).expect("Couldn't open target");
287277

288278
let mut text = String::new();
289-
f.read_to_string(&mut text).ok().expect("Failed reading target.");
279+
f.read_to_string(&mut text).expect("Failed reading target");
290280

291281
if fmt_text != text {
292282
let diff = make_diff(&text, &fmt_text, DIFF_CONTEXT_SIZE);

tests/target/chains-no-overlow-2.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
fn main() {
44
reader.lines()
5-
.map(|line| line.ok().expect("Failed getting line."))
5+
.map(|line| line.expect("Failed getting line"))
66
.take_while(|line| line_regex.is_match(&line))
77
.filter_map(|line| {
88
regex.captures_iter(&line)
99
.next()
1010
.map(|capture| {
11-
(capture.at(1).expect("Couldn\'t unwrap capture.").to_owned(),
12-
capture.at(2).expect("Couldn\'t unwrap capture.").to_owned())
11+
(capture.at(1).expect("Couldn\'t unwrap capture").to_owned(),
12+
capture.at(2).expect("Couldn\'t unwrap capture").to_owned())
1313
})
1414
})
1515
.collect();

0 commit comments

Comments
 (0)