Code Monkey home page Code Monkey logo

expectorate's Introduction

Expectorate

This is a little library to validate expected output saved in files. It makes it easy to update that content when it should be updated to match the new results.

Validating Output

Say you have a function compose() that produces a string and you have a file named lyrics.txt that contain the expected output. You can compare the actual output like this:

    let actual: &str = compose();
    assert_content("lyrics.txt", actual);

If the output doesn't match, you'll see output like this:

White means that the content matches. Red means that content from the file was missing. Green means that content not in the file was added.

If we want to accept the changes from compose() we'd simply run with EXPECTORATE=overwrite. Assuming lyrics.txt is checked in, git diff will show you something like this:

diff --git a/examples/lyrics.txt b/examples/lyrics.txt
index e4104c1..ea6beaf 100644
--- a/examples/lyrics.txt
+++ b/examples/lyrics.txt
@@ -1,5 +1,2 @@
-No one hits like Gaston
-Matches wits like Gaston
-In a spitting match nobody spits like Gaston
+In a testing match nobody tests like Gaston
 I'm especially good at expectorating
-Ten points for Gaston

Predicates (feature: predicates)

Expectorate can be used in places where you might use the predicates crate. If you're using predicates::path::eq_file you can instead use expectorate::eq_file or expectorate::eq_file_or_panic. Populate or update the specified file as above.

expectorate's People

Contributors

ahl avatar david-crespo avatar dependabot[bot] avatar plasticcaz avatar raycar5 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

smklein

expectorate's Issues

EXPECTORATE=overwrite should not change mtime of file if contents match

Currently, EXPECTORATE=overwrite unconditionally overwrites the file, whether the contents match or not:

expectorate/src/lib.rs

Lines 75 to 78 in 6b83e7f

if overwrite {
if let Err(e) = fs::write(path, actual.as_ref()) {
panic!("unable to write to {}: {}", path.display(), e);
}

This approach has a problem: it causes the mtime of the overwritten file to be bumped. If the file is depended on by other crates (e.g. an OpenAPI spec), it can cause spurious rebuilds.

Instead, I believe we should always check the contents of the file on disk, and then if they differ, we should:

  • if EXPECTORATE=overwrite is set, overwrite the file
  • otherwise, fail

This would make sure that the mtime of the file stays the same if the contents match.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.