Skip to content

Commit a10714d

Browse files
committed
Creating a custom message closure for configuration mismatches
1 parent 4f22102 commit a10714d

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

tests/system.rs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ fn assert_output(source: &Path, expected_filename: &Path) {
103103
if !compare.is_empty() {
104104
let mut failures = HashMap::new();
105105
failures.insert(source.to_owned(), compare);
106-
print_mismatches(failures);
106+
print_mismatches_default_message(failures, source.display());
107107
assert!(false, "Text does not match expected output");
108108
}
109109
}
@@ -219,15 +219,15 @@ where
219219
for file_name in files.filter(|f| f.extension().map_or(false, |f| f == "rs")) {
220220
debug!("Testing '{}'...", file_name.display());
221221

222-
match idempotent_check(file_name) {
222+
match idempotent_check(&file_name) {
223223
Ok(ref report) if report.has_warnings() => {
224224
print!("{}", report);
225225
fails += 1;
226226
}
227227
Ok(report) => reports.push(report),
228228
Err(err) => {
229229
if let IdempotentCheckError::Mismatch(msg) = err {
230-
print_mismatches(msg);
230+
print_mismatches_default_message(msg, file_name.display());
231231
}
232232
fails += 1;
233233
}
@@ -239,15 +239,22 @@ where
239239
(reports, count, fails)
240240
}
241241

242-
fn print_mismatches(result: HashMap<PathBuf, Vec<Mismatch>>) {
243-
let mut t = term::stdout().unwrap();
242+
fn print_mismatches_default_message(
243+
result: HashMap<PathBuf, Vec<Mismatch>>,
244+
file_name: std::path::Display,
245+
) {
246+
print_mismatches(result, |line_num| {
247+
format!("\nMismatch at {}:{}:", file_name, line_num)
248+
});
249+
}
244250

245-
for (file_name, diff) in result {
246-
print_diff(
247-
diff,
248-
|line_num| format!("\nMismatch at {}:{}:", file_name.display(), line_num),
249-
Color::Auto,
250-
);
251+
fn print_mismatches<T: Fn(u32) -> String>(
252+
result: HashMap<PathBuf, Vec<Mismatch>>,
253+
mismatch_msg_formatter: T,
254+
) {
255+
let mut t = term::stdout().unwrap();
256+
for (_file_name, diff) in result {
257+
print_diff(diff, &mismatch_msg_formatter, Color::Auto);
251258
}
252259

253260
t.reset().unwrap();
@@ -287,7 +294,7 @@ pub enum IdempotentCheckError {
287294
Parse,
288295
}
289296

290-
pub fn idempotent_check(filename: PathBuf) -> Result<FormatReport, IdempotentCheckError> {
297+
pub fn idempotent_check(filename: &PathBuf) -> Result<FormatReport, IdempotentCheckError> {
291298
let sig_comments = read_significant_comments(&filename);
292299
let config = read_config(&filename);
293300
let (error_summary, file_map, format_report) = format_file(filename, &config);
@@ -611,7 +618,12 @@ fn check_blocks_idempotency(blocks: &Vec<CodeBlock>) {
611618
if !compare.is_empty() {
612619
let mut mismatches = HashMap::new();
613620
mismatches.insert(PathBuf::from("Configurations.md"), compare);
614-
print_mismatches(mismatches);
621+
print_mismatches(mismatches, |line_num| {
622+
format!(
623+
"\nMismatch at Configurations.md:{}:",
624+
line_num + block.code_block_start_line - 1
625+
)
626+
});
615627
failures += 1;
616628
}
617629
}

0 commit comments

Comments
 (0)