1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//! Functions for comparing files.

use std::fs::File;
use std::io::Read;
use std::path::Path;

use difference;

/// A function that displays a diff and panics if two files to not match.
pub type Differ = Box<Fn(&Path, &Path)>;

/// Compare unicode text files. Print a colored diff and panic on failure.
pub fn text_diff(old: &Path, new: &Path) {
    difference::assert_diff(&read_file(old), &read_file(new), "\n", 0);
}

fn read_file(path: &Path) -> String {
    let mut contents = String::new();
    File::open(path)
        .expect(&format!("Error opening file: {:?}", path))
        .read_to_string(&mut contents)
        .expect(&format!("Error reading file: {:?}", path));
    return contents;
}