Skip to content

Commit

Permalink
Add pre-commit and fix lints.
Browse files Browse the repository at this point in the history
  • Loading branch information
calder committed Apr 16, 2024
1 parent 837b4ed commit 44f671c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 42 deletions.
18 changes: 6 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,15 @@ You can use goldenfiles to test the output of a parser, the order of a graph tra
## Usage

```rust
extern crate goldenfile;

use std::io::Write;

use goldenfile::Mint;
use std::io::Write;

#[test]
fn test() {
let mut mint = Mint::new("tests/goldenfiles");
let mut file1 = mint.new_goldenfile("file1.txt").unwrap();
let mut file2 = mint.new_goldenfile("file2.txt").unwrap();
let mut mint = Mint::new("tests/goldenfiles");
let mut file1 = mint.new_goldenfile("file1.txt").unwrap();
let mut file2 = mint.new_goldenfile("file2.txt").unwrap();

writeln!(file1, "Hello world!").unwrap();
writeln!(file2, "Foo bar!").unwrap();
}
writeln!(file1, "Hello world!").unwrap();
writeln!(file2, "Foo bar!").unwrap();
```

When the `Mint` goes out of scope, it compares the contents of each file to its checked-in golden version and fails the test if they differ. To update the checked-in versions, run:
Expand Down
16 changes: 8 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
//! # Example
//!
//! ```rust
//! #[test]
//! fn test() {
//! let mut mint = Mint::new("tests/goldenfiles");
//! let mut file1 = mint.new_goldenfile("file1.txt").unwrap();
//! let mut file2 = mint.new_goldenfile("file2.txt").unwrap();
//! use goldenfile::Mint;
//! use std::io::Write;
//!
//! write!(file1, "Hello world!").unwrap();
//! write!(file2, "Foo bar!").unwrap();
//! }
//! let mut mint = Mint::new("tests/goldenfiles");
//! let mut file1 = mint.new_goldenfile("file1.txt").unwrap();
//! let mut file2 = mint.new_goldenfile("file2.txt").unwrap();
//!
//! writeln!(file1, "Hello world!").unwrap();
//! writeln!(file2, "Foo bar!").unwrap();
//! ```
//!
//! When the `Mint` goes out of scope, it compares the contents of each file
Expand Down
14 changes: 7 additions & 7 deletions src/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl Mint {
let abs_path = self.tempdir.path().to_path_buf().join(path.as_ref());
if let Some(abs_parent) = abs_path.parent() {
if abs_parent != self.tempdir.path() {
fs::create_dir_all(&abs_parent).unwrap_or_else(|err| {
fs::create_dir_all(abs_parent).unwrap_or_else(|err| {
panic!(
"Failed to create temporary subdirectory {:?}: {:?}",
abs_parent, err
Expand All @@ -102,9 +102,9 @@ impl Mint {
/// Called automatically when a Mint goes out of scope and
/// `UPDATE_GOLDENFILES!=1`.
pub fn check_goldenfiles(&self) {
for &(ref file, ref differ) in &self.files {
let old = self.path.join(&file);
let new = self.tempdir.path().join(&file);
for (file, differ) in &self.files {
let old = self.path.join(file);
let new = self.tempdir.path().join(file);
defer_on_unwind! {
eprintln!("note: run with `UPDATE_GOLDENFILES=1` to update goldenfiles");
eprintln!(
Expand All @@ -122,9 +122,9 @@ impl Mint {
/// Called automatically when a Mint goes out of scope and
/// `UPDATE_GOLDENFILES=1`.
pub fn update_goldenfiles(&self) {
for &(ref file, _) in &self.files {
let old = self.path.join(&file);
let new = self.tempdir.path().join(&file);
for (file, _) in &self.files {
let old = self.path.join(file);
let new = self.tempdir.path().join(file);

let empty = File::open(&new).unwrap().metadata().unwrap().len() == 0;
if self.create_empty || !empty {
Expand Down
15 changes: 0 additions & 15 deletions tests/readme_usage.rs

This file was deleted.

0 comments on commit 44f671c

Please sign in to comment.