Macro insta::glob

source ·
macro_rules! glob {
    ($base_path:expr, $glob:expr, $closure:expr) => { ... };
    ($glob:expr, $closure:expr) => { ... };
}
Available on crate feature glob only.
Expand description

Executes a closure for all input files matching a glob.

The closure is passed the path to the file. You can use std::fs::read_to_string or similar functions to load the file and process it.

use std::fs;

glob!("inputs/*.txt", |path| {
    let input = fs::read_to_string(path).unwrap();
    assert_snapshot!(input.to_uppercase());
});

The INSTA_GLOB_FILTER environment variable can be set to only execute certain files. The format of the filter is a semicolon separated filter. For instance by setting INSTA_GLOB_FILTER to foo-*txt;bar-*.txt only files starting with foo- or bar- end ending in .txt will be executed. When using cargo-insta the --glob-filter option can be used instead.

Another effect of the globbing system is that snapshot failures within the glob macro are deferred until the end of of it. In other words this means that each snapshot assertion within the glob! block are reported. It can be disabled by setting INSTA_GLOB_FAIL_FAST environment variable to 1.

A three-argument version of this macro allows specifying a base directory for the glob to start in. This allows globbing in arbitrary directories, including parent directories:

use std::fs;

glob!("../test_data", "inputs/*.txt", |path| {
    let input = fs::read_to_string(path).unwrap();
    assert_snapshot!(input.to_uppercase());
});