Skip to content

Commit

Permalink
Add Mint::new_nonempty.
Browse files Browse the repository at this point in the history
  • Loading branch information
calder committed Apr 16, 2024
1 parent c0d4a6e commit caab87d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 21 deletions.
23 changes: 8 additions & 15 deletions src/differs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Functions for comparing files.
use std::fs::{metadata, File};
use std::fs;
use std::io;
use std::io::{BufReader, Read};
use std::path::Path;
Expand All @@ -12,7 +12,10 @@ pub type Differ = Box<dyn Fn(&Path, &Path)>;

/// Compare unicode text files. Print a colored diff and panic on failure.
pub fn text_diff(old: &Path, new: &Path) {
similar_asserts::assert_eq!(&read_file(old), &read_file(new));
similar_asserts::assert_eq!(
&fs::read_to_string(old).unwrap_or("".to_string()),
&fs::read_to_string(new).unwrap_or("".to_string()),
);
}

/// Panic if binary files differ with some basic information about where they
Expand All @@ -36,8 +39,8 @@ pub fn binary_diff(old: &Path, new: &Path) {
}
}

fn open_file(path: &Path) -> File {
check_io(File::open(path), "opening file", path)
fn open_file(path: &Path) -> fs::File {
check_io(fs::File::open(path), "opening file", path)
}

fn file_byte_iter(path: &Path) -> impl Iterator<Item = u8> + '_ {
Expand All @@ -47,17 +50,7 @@ fn file_byte_iter(path: &Path) -> impl Iterator<Item = u8> + '_ {
}

fn file_len(path: &Path) -> u64 {
check_io(metadata(path), "getting file length", path).len()
}

fn read_file(path: &Path) -> String {
let mut contents = String::new();
check_io(
open_file(path).read_to_string(&mut contents),
"reading file",
path,
);
contents
check_io(fs::metadata(path), "getting file length", path).len()
}

fn check_io<T>(x: Result<T, io::Error>, message: &str, path: &Path) -> T {
Expand Down
28 changes: 22 additions & 6 deletions src/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ pub struct Mint {
path: PathBuf,
tempdir: TempDir,
files: Vec<(PathBuf, Differ)>,
create_empty: bool,
}

impl Mint {
/// Create a new goldenfile Mint.
///
/// All goldenfiles will be created in the Mint's directory.
pub fn new<P: AsRef<Path>>(path: P) -> Self {
fn new_internal<P: AsRef<Path>>(path: P, create_empty: bool) -> Self {
let tempdir = TempDir::new().unwrap();
let mint = Mint {
path: path.as_ref().to_path_buf(),
files: vec![],
tempdir,
create_empty,
};
fs::create_dir_all(&mint.path).unwrap_or_else(|err| {
panic!(
Expand All @@ -47,6 +47,16 @@ impl Mint {
mint
}

/// Create a new goldenfile Mint.
pub fn new<P: AsRef<Path>>(path: P) -> Self {
Self::new_internal(path, true)
}

/// Create a new goldenfile Mint. Goldenfiles will only be created when non-empty.
pub fn new_nonempty<P: AsRef<Path>>(path: P) -> Self {
Self::new_internal(path, false)
}

/// Create a new goldenfile using a differ inferred from the file extension.
///
/// The returned File is a temporary file, not the goldenfile itself.
Expand Down Expand Up @@ -116,9 +126,15 @@ impl Mint {
let old = self.path.join(&file);
let new = self.tempdir.path().join(&file);

println!("Updating {:?}.", file.to_str().unwrap());
fs::copy(&new, &old)
.unwrap_or_else(|err| panic!("Error copying {:?} to {:?}: {:?}", &new, &old, err));
let empty = File::open(&new).unwrap().metadata().unwrap().len() == 0;
if self.create_empty || !empty {
println!("Updating {:?}.", file.to_str().unwrap());
fs::copy(&new, &old).unwrap_or_else(|err| {
panic!("Error copying {:?} to {:?}: {:?}", &new, &old, err)
});
} else {
std::fs::remove_file(&old).unwrap();
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/goldenfiles/nonempty.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Some content
9 changes: 9 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,12 @@ fn update() {

mint.update_goldenfiles()
}

#[test]
fn nonempty() {
let mut mint = Mint::new_nonempty("tests/goldenfiles");
let mut file = mint.new_goldenfile("nonempty.txt").unwrap();
mint.new_goldenfile("empty.txt").unwrap();

writeln!(file, "Some content").unwrap();
}

0 comments on commit caab87d

Please sign in to comment.