Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: mitsuhiko/insta
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 1.5.0
Choose a base ref
...
head repository: mitsuhiko/insta
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 1.5.1
Choose a head ref
  • 3 commits
  • 12 files changed
  • 1 contributor

Commits on Jan 3, 2021

  1. vscode 1.0.6

    mitsuhiko committed Jan 3, 2021
    Copy the full SHA
    efe84ab View commit details
  2. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    9728295 View commit details
  3. 1.5.1

    mitsuhiko committed Jan 3, 2021
    Copy the full SHA
    978ef7f View commit details
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 1.5.1

* Fixed glob not working correctly.
* Fail by default if glob is not returning any matches. Fixes #151.

## 1.5.0

* Add `pending-snapshots` parameter to `cargo-insta`.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "insta"
version = "1.5.0"
version = "1.5.1"
license = "Apache-2.0"
authors = ["Armin Ronacher <armin.ronacher@active-4.com>"]
description = "A snapshot testing library for Rust"
4 changes: 2 additions & 2 deletions cargo-insta/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions cargo-insta/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cargo-insta"
version = "1.5.0"
version = "1.5.1"
license = "Apache-2.0"
authors = ["Armin Ronacher <armin.ronacher@active-4.com>"]
description = "A review tool for the insta snapshot testing library for Rust"
@@ -11,7 +11,7 @@ edition = "2018"
readme = "README.md"

[dependencies]
insta = { version = "1.5.0", path = "..", features = ["redactions"] }
insta = { version = "1.5.1", path = "..", features = ["redactions"] }
console = "0.14.0"
clap = { version = "2.33.3", default-features = false }
difference = "2.0.0"
11 changes: 9 additions & 2 deletions src/glob.rs
Original file line number Diff line number Diff line change
@@ -14,20 +14,27 @@ pub fn glob_exec<F: FnMut(&Path)>(base: &Path, pattern: &str, mut f: F) {
.compile_matcher();

let walker = WalkDir::new(base).follow_links(true);
let mut glob_found_matches = false;
let mut settings = Settings::clone_current();

for file in walker {
let file = file.unwrap();
let path = file.path();
if !glob.is_match(path) {
let stripped_path = path.strip_prefix(base).unwrap_or(path);
if !glob.is_match(stripped_path) {
continue;
}

let mut settings = Settings::clone_current();
settings.set_input_file(&path);
settings.set_snapshot_suffix(path.file_name().unwrap().to_str().unwrap());

glob_found_matches = true;
settings.bind(|| {
f(path);
});
}

if !glob_found_matches && !settings.allow_empty_glob() {
panic!("the glob! macro did not match any files.");
}
}
24 changes: 23 additions & 1 deletion src/settings.rs
Original file line number Diff line number Diff line change
@@ -21,6 +21,8 @@ lazy_static! {
prepend_module_to_snapshot: true,
#[cfg(feature = "redactions")]
redactions: Redactions::default(),
#[cfg(feature = "glob")]
allow_empty_glob: false,
});
}
thread_local!(static CURRENT_SETTINGS: RefCell<Settings> = RefCell::new(Settings::new()));
@@ -52,6 +54,8 @@ pub struct ActualSettings {
pub prepend_module_to_snapshot: bool,
#[cfg(feature = "redactions")]
pub redactions: Redactions,
#[cfg(feature = "glob")]
pub allow_empty_glob: bool,
}

/// Configures how insta operates at test time.
@@ -125,7 +129,7 @@ impl Settings {
self.inner.sort_maps
}

/// Disbales prepending of modules to the snapshot filename.
/// Disables prepending of modules to the snapshot filename.
///
/// By default the filename of a snapshot is `<module>__<name>.snap`.
/// Setting this flag to `false` changes the snapshot filename to just
@@ -141,6 +145,24 @@ impl Settings {
self.inner.prepend_module_to_snapshot
}

/// Allows the `glob!` macro to succeed if it matches no files.
///
/// By default the glob macro will fail the test if it does not find
/// any files to prevent accidental typos. This can be disabled when
/// fixtures should be conditional.
///
/// The default value is `false`.
#[cfg(feature = "glob")]
pub fn set_allow_empty_glob(&mut self, value: bool) {
self._private_inner_mut().allow_empty_glob = value;
}

/// Returns the current value for the empty glob setting.
#[cfg(feature = "glob")]
pub fn allow_empty_glob(&self) -> bool {
self.inner.allow_empty_glob
}

/// Sets the snapshot suffix.
///
/// The snapshot suffix is added to all snapshot names with an `@` sign
6 changes: 6 additions & 0 deletions tests/snapshots/test_glob__basic_globbing@goodbye.txt.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: tests/test_glob.rs
expression: "&contents"
input_file: tests/inputs/goodbye.txt
---
"Contents of goodbye"
6 changes: 6 additions & 0 deletions tests/snapshots/test_glob__basic_globbing@hello.txt.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: tests/test_glob.rs
expression: "&contents"
input_file: tests/inputs/hello.txt
---
"Contents of hello"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: tests/test_glob.rs
expression: "&contents"
input_file: tests/inputs/goodbye.txt
---
"Contents of goodbye"
6 changes: 6 additions & 0 deletions tests/snapshots/test_glob__globs_follow_links@hello.txt.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: tests/test_glob.rs
expression: "&contents"
input_file: tests/inputs/hello.txt
---
"Contents of hello"
8 changes: 8 additions & 0 deletions tests/test_glob.rs
Original file line number Diff line number Diff line change
@@ -15,3 +15,11 @@ fn test_globs_follow_links() {
insta::assert_json_snapshot!(&contents);
});
}

#[test]
#[should_panic(expected = "the glob! macro did not match any files.")]
fn test_empty_glob_fails() {
insta::glob!("nonexistent", |_| {
// nothing
});
}
4 changes: 2 additions & 2 deletions vscode-insta/package.json
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
"name": "insta",
"displayName": "insta snapshots",
"description": "Syntax support for insta snapshots",
"version": "1.0.5",
"version": "1.0.6",
"publisher": "mitsuhiko",
"license": "Apache-2.0",
"author": {
@@ -201,4 +201,4 @@
"watch": "tsc -watch -p ./",
"postinstall": "node ./node_modules/vscode/bin/install"
}
}
}