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.26.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.28.0
Choose a head ref
  • 14 commits
  • 33 files changed
  • 3 contributors

Commits on Feb 1, 2023

  1. Copy the full SHA
    7b5a7f8 View commit details
  2. Added changelog entry

    mitsuhiko committed Feb 1, 2023
    Copy the full SHA
    51f33e0 View commit details
  3. Copy the full SHA
    7c09822 View commit details

Commits on Feb 2, 2023

  1. Copy the full SHA
    218d2d8 View commit details
  2. Copy the full SHA
    4884685 View commit details

Commits on Feb 15, 2023

  1. Copy the full SHA
    e5c5dfa View commit details
  2. Copy the full SHA
    48f8f77 View commit details
  3. Copy the full SHA
    48f044e View commit details
  4. Pin CSV to 1.1.6 (#349)

    mitsuhiko authored Feb 15, 2023
    Copy the full SHA
    da63687 View commit details
  5. Copy the full SHA
    64ba457 View commit details
  6. Copy the full SHA
    9d70fac View commit details
  7. Added changelog entry

    mitsuhiko committed Feb 15, 2023
    Copy the full SHA
    d84e3f3 View commit details

Commits on Feb 16, 2023

  1. Copy the full SHA
    7c00b0b View commit details
  2. 1.28.0

    mitsuhiko committed Feb 16, 2023
    Copy the full SHA
    5c7e704 View commit details
Showing with 696 additions and 255 deletions.
  1. +19 −0 CHANGELOG.md
  2. +2 −2 Cargo.toml
  3. +3 −1 Makefile
  4. +2 −2 cargo-insta/Cargo.lock
  5. +2 −2 cargo-insta/Cargo.toml
  6. +67 −30 cargo-insta/src/cli.rs
  7. +10 −2 cargo-insta/src/inline.rs
  8. +1 −1 src/content/serialization.rs
  9. +9 −1 src/glob.rs
  10. +3 −3 src/lib.rs
  11. +53 −4 src/macros.rs
  12. +213 −183 src/output.rs
  13. +21 −0 src/redaction.rs
  14. +101 −17 src/runtime.rs
  15. +1 −5 src/settings.rs
  16. +6 −2 src/utils.rs
  17. +53 −0 tests/glob_submodule/mod.rs
  18. +6 −0 .../glob_submodule/snapshots/test_glob__glob_submodule__basic_globbing_absolute_dir@goodbye.txt.snap
  19. +6 −0 tests/glob_submodule/snapshots/test_glob__glob_submodule__basic_globbing_absolute_dir@hello.txt.snap
  20. +7 −0 .../snapshots/test_glob__glob_submodule__basic_globbing_nested_parent_dir_base_path@a__file.txt.snap
  21. +7 −0 .../snapshots/test_glob__glob_submodule__basic_globbing_nested_parent_dir_base_path@b__file.txt.snap
  22. +7 −0 ...submodule/snapshots/test_glob__glob_submodule__basic_globbing_nested_parent_glob@a__file.txt.snap
  23. +7 −0 ...submodule/snapshots/test_glob__glob_submodule__basic_globbing_nested_parent_glob@b__file.txt.snap
  24. +6 −0 tests/glob_submodule/snapshots/test_glob__glob_submodule__basic_globbing_parent_dir@goodbye.txt.snap
  25. +6 −0 tests/glob_submodule/snapshots/test_glob__glob_submodule__basic_globbing_parent_dir@hello.txt.snap
  26. +6 −0 ...ule/snapshots/test_glob__glob_submodule__globs_follow_links_parent_dir_base_path@goodbye.txt.snap
  27. +6 −0 ...odule/snapshots/test_glob__glob_submodule__globs_follow_links_parent_dir_base_path@hello.txt.snap
  28. +6 −0 ...ubmodule/snapshots/test_glob__glob_submodule__globs_follow_links_parent_dir_glob@goodbye.txt.snap
  29. +6 −0 ..._submodule/snapshots/test_glob__glob_submodule__globs_follow_links_parent_dir_glob@hello.txt.snap
  30. +8 −0 tests/snapshots/test_redaction__rounded_redaction.snap
  31. +22 −0 tests/test_allow_duplicates.rs
  32. +2 −0 tests/test_glob.rs
  33. +22 −0 tests/test_redaction.rs
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -2,6 +2,25 @@

All notable changes to insta and cargo-insta are documented here.

## 1.28.0

- Added `allow_duplicates!` to enable multiple assertions for a
single snapshot. (#346)
- Ensure that expressions formatted with `rustfmt` use unix newlines.
- Added a way to disable diffing in review. (#348)
- Added three argument version of `glob!` to set a different base
path. (#347)
- Added `rounded_redaction` to truncate floating point values. (#350)

## 1.27.0

- Fix an issue where the inline snapshot patcher could panic in
certain situations. (#341)
- `cargo insta test` now correctly detects CI environments like
`cargo test` does. In that case it will by fail rather than
create snapshot update files. (#345)
- Added `cargo insta test --check` to force check runs. (#345)

## 1.26.0

- Make canonicalization in `glob!` optional to better support WASI.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "insta"
version = "1.26.0"
version = "1.28.0"
license = "Apache-2.0"
authors = ["Armin Ronacher <armin.ronacher@active-4.com>"]
description = "A snapshot testing library for Rust"
@@ -47,7 +47,7 @@ yaml = ["serde"]
_cargo_insta_internal = []

[dependencies]
dep_csv = { package = "csv", version = "1.1.4", optional = true }
dep_csv = { package = "csv", version = "=1.1.6", optional = true }
console = { version = "0.15.4", optional = true, default-features = false }
pest = { version = "2.1.3", optional = true }
pest_derive = { version = "2.1.0", optional = true }
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -10,7 +10,9 @@ test: cargotest cargo-insta-tests

cargo-insta-tests:
@echo "CARGO-INSTA INTEGRATION TESTS"
@cd cargo-insta/integration-tests; cargo run
# Turn off CI flag so that cargo insta test behaves as we expect
# under normal operation
@cd cargo-insta/integration-tests; CI=0 cargo run

cargotest:
@echo "CARGO TESTS"
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.26.0"
version = "1.28.0"
license = "Apache-2.0"
authors = ["Armin Ronacher <armin.ronacher@active-4.com>"]
description = "A review tool for the insta snapshot testing library for Rust"
@@ -12,7 +12,7 @@ edition = "2018"
readme = "README.md"

[dependencies]
insta = { version = "=1.26.0", path = "..", features = ["json", "yaml", "redactions", "_cargo_insta_internal"] }
insta = { version = "=1.28.0", path = "..", features = ["json", "yaml", "redactions", "_cargo_insta_internal"] }
console = "0.15.4"
structopt = { version = "0.3.26", default-features = false }
serde = { version = "1.0.117", features = ["derive"] }
97 changes: 67 additions & 30 deletions cargo-insta/src/cli.rs
Original file line number Diff line number Diff line change
@@ -8,8 +8,7 @@ use std::{io, process};
use console::{set_colors_enabled, style, Key, Term};
use insta::Snapshot;
use insta::_cargo_insta_support::{
is_ci, print_snapshot, print_snapshot_diff, SnapshotUpdate, TestRunner, ToolConfig,
UnreferencedSnapshots,
is_ci, SnapshotPrinter, SnapshotUpdate, TestRunner, ToolConfig, UnreferencedSnapshots,
};
use serde::Serialize;
use structopt::clap::AppSettings;
@@ -32,16 +31,19 @@ use crate::walk::{find_snapshots, make_deletion_walker, make_snapshot_walker, Fi
global_setting = AppSettings::DontCollapseArgsInUsage
)]
pub struct Opts {
/// Coloring: auto, always, never
#[structopt(long, global = true, value_name = "WHEN")]
pub color: Option<String>,
/// Coloring
#[structopt(long, global = true, value_name = "WHEN", default_value="auto", possible_values=&["auto", "always", "never"])]
pub color: String,

#[structopt(subcommand)]
pub command: Command,
}

#[derive(StructOpt, Debug)]
#[structopt(bin_name = "cargo insta")]
#[structopt(
bin_name = "cargo insta",
after_help = "For the online documentation of the latest version, see https://insta.rs/docs/cli/."
)]
pub enum Command {
/// Interactively review snapshots
#[structopt(name = "review", alias = "verify")]
@@ -164,15 +166,18 @@ pub struct TestCommand {
/// Accept all new (previously unseen).
#[structopt(long)]
pub accept_unseen: bool,
/// Instructs the test command to just assert.
#[structopt(long)]
pub check: bool,
/// Do not reject pending snapshots before run.
#[structopt(long)]
pub keep_pending: bool,
/// Update all snapshots even if they are still matching.
#[structopt(long)]
pub force_update_snapshots: bool,
/// Controls what happens with unreferenced snapshots.
#[structopt(long)]
pub unreferenced: Option<String>,
#[structopt(long, default_value="ignore", possible_values=&["ignore", "warn", "reject", "delete", "auto"])]
pub unreferenced: String,
/// Delete unreferenced snapshots after the test run.
#[structopt(long, hidden = true)]
pub delete_unreferenced_snapshots: bool,
@@ -183,8 +188,8 @@ pub struct TestCommand {
#[structopt(short = "Q", long)]
pub no_quiet: bool,
/// Picks the test runner.
#[structopt(long)]
pub test_runner: Option<String>,
#[structopt(long, default_value="auto", possible_values=&["auto", "cargo-test", "nextest"])]
pub test_runner: String,
/// Options passed to cargo test
// Sets raw to true so that `--` is required
#[structopt(name = "CARGO_TEST_ARGS", raw(true))]
@@ -222,6 +227,7 @@ fn query_snapshot(
n: usize,
snapshot_file: Option<&Path>,
show_info: &mut bool,
show_diff: &mut bool,
) -> Result<Operation, Box<dyn Error>> {
loop {
term.clear_screen()?;
@@ -240,7 +246,12 @@ fn query_snapshot(
pkg_version,
);

print_snapshot_diff(workspace_root, new, old, snapshot_file, line, *show_info);
let mut printer = SnapshotPrinter::new(workspace_root, old, new);
printer.set_snapshot_file(snapshot_file);
printer.set_line(line);
printer.set_show_info(*show_info);
printer.set_show_diff(*show_diff);
printer.print();

println!();
println!(
@@ -264,6 +275,12 @@ fn query_snapshot(
if *show_info { "hide" } else { "show" },
style("toggles extended snapshot info").dim()
);
println!(
" {} {} diff {}",
style("d").cyan().bold(),
if *show_diff { "hide" } else { "show" },
style("toggle snapshot diff").dim()
);

loop {
match term.read_key()? {
@@ -274,6 +291,10 @@ fn query_snapshot(
*show_info = !*show_info;
break;
}
Key::Char('d') => {
*show_diff = !*show_diff;
break;
}
_ => {}
}
}
@@ -415,6 +436,7 @@ fn process_snapshots(
let mut skipped = vec![];
let mut num = 0;
let mut show_info = true;
let mut show_diff = true;

for (snapshot_container, package) in snapshot_containers.iter_mut() {
let target_file = snapshot_container.target_file().to_path_buf();
@@ -447,6 +469,7 @@ fn process_snapshots(
snapshot_count,
snapshot_file.as_ref().map(|x| x.as_path()),
&mut show_info,
&mut show_diff,
)?,
};
match op {
@@ -498,7 +521,12 @@ fn process_snapshots(
fn test_run(mut cmd: TestCommand, color: &str) -> Result<(), Box<dyn Error>> {
let loc = handle_target_args(&cmd.target_args)?;
match loc.tool_config.snapshot_update() {
SnapshotUpdate::Auto | SnapshotUpdate::New | SnapshotUpdate::No => {}
SnapshotUpdate::Auto => {
if is_ci() {
cmd.check = true;
}
}
SnapshotUpdate::New | SnapshotUpdate::No => {}
SnapshotUpdate::Always => {
if !cmd.accept && !cmd.accept_unseen && !cmd.review {
cmd.review = false;
@@ -514,6 +542,12 @@ fn test_run(mut cmd: TestCommand, color: &str) -> Result<(), Box<dyn Error>> {
}
}

// --check always implies --no-force-pass as otherwise this command does not
// make a lot of sense.
if cmd.check {
cmd.no_force_pass = true
}

// the tool config can also indicate that --accept-unseen should be picked
// automatically unless instructed otherwise.
if loc.tool_config.auto_accept_unseen() && !cmd.accept && !cmd.review {
@@ -525,22 +559,18 @@ fn test_run(mut cmd: TestCommand, color: &str) -> Result<(), Box<dyn Error>> {

// Legacy command
if cmd.delete_unreferenced_snapshots {
cmd.unreferenced = Some("delete".into());
cmd.unreferenced = "delete".into();
}

let test_runner = match cmd.test_runner {
Some(ref test_runner) => test_runner
.parse()
.map_err(|_| err_msg("invalid test runner preference"))?,
None => loc.tool_config.test_runner(),
};
let test_runner = cmd
.test_runner
.parse()
.map_err(|_| err_msg("invalid test runner preference"))?;

let unreferenced = match cmd.unreferenced {
Some(ref value) => value
.parse()
.map_err(|_| err_msg("invalid value for --unreferenced"))?,
None => loc.tool_config.test_unreferenced(),
};
let unreferenced = cmd
.unreferenced
.parse()
.map_err(|_| err_msg("invalid value for --unreferenced"))?;

let (mut proc, snapshot_ref_file, prevents_doc_run) =
prepare_test_runner(test_runner, unreferenced, &cmd, color, &[], None)?;
@@ -798,7 +828,11 @@ fn prepare_test_runner<'snapshot_ref>(
}
proc.env(
"INSTA_UPDATE",
if cmd.accept_unseen { "unseen" } else { "new" },
match (cmd.check, cmd.accept_unseen) {
(true, _) => "no",
(_, true) => "unseen",
(_, false) => "new",
},
);
if cmd.force_update_snapshots {
proc.env("INSTA_FORCE_UPDATE_SNAPSHOTS", "1");
@@ -860,7 +894,11 @@ fn prepare_test_runner<'snapshot_ref>(
fn show_cmd(cmd: ShowCommand) -> Result<(), Box<dyn Error>> {
let loc = handle_target_args(&cmd.target_args)?;
let snapshot = Snapshot::from_file(&cmd.path)?;
print_snapshot(&loc.workspace_root, &snapshot, Some(&cmd.path), None, true);
let mut printer = SnapshotPrinter::new(&loc.workspace_root, None, &snapshot);
printer.set_snapshot_file(Some(&cmd.path));
printer.set_show_info(true);
printer.set_show_diff(false);
printer.print();
Ok(())
}

@@ -988,8 +1026,7 @@ pub fn run() -> Result<(), Box<dyn Error>> {

let opts = Opts::from_iter(args);

let color = opts.color.as_ref().map(|x| x.as_str()).unwrap_or("auto");
handle_color(color)?;
handle_color(&opts.color)?;
match opts.command {
Command::Review(ref cmd) | Command::Accept(ref cmd) | Command::Reject(ref cmd) => {
process_snapshots(
@@ -1004,7 +1041,7 @@ pub fn run() -> Result<(), Box<dyn Error>> {
},
)
}
Command::Test(cmd) => test_run(cmd, color),
Command::Test(cmd) => test_run(cmd, &opts.color),
Command::Show(cmd) => show_cmd(cmd),
Command::PendingSnapshots(cmd) => pending_snapshots_cmd(cmd),
}
12 changes: 10 additions & 2 deletions cargo-insta/src/inline.rs
Original file line number Diff line number Diff line change
@@ -57,10 +57,18 @@ impl FilePatcher {
pub fn add_snapshot_macro(&mut self, line: usize) -> bool {
match self.find_snapshot_macro(line) {
Some(snapshot) => {
assert!(self
// this can happen if multiple snapshots were added in one
// iteration of a loop. In that case we want to ignore the
// duplicate
//
// See https://github.com/mitsuhiko/insta/issues/340
if self
.inline_snapshots
.last()
.map_or(true, |x| x.end.0 <= line));
.map_or(false, |x| x.end.0 > line)
{
return false;
}
self.inline_snapshots.push(snapshot);
true
}
2 changes: 1 addition & 1 deletion src/content/serialization.rs
Original file line number Diff line number Diff line change
@@ -125,7 +125,7 @@ impl Serialize for Content {
Content::Map(ref entries) => {
use serde::ser::SerializeMap;
let mut map = serializer.serialize_map(Some(entries.len()))?;
for &(ref k, ref v) in entries {
for (k, v) in entries {
map.serialize_entry(k, v)?;
}
map.end()
10 changes: 9 additions & 1 deletion src/glob.rs
Original file line number Diff line number Diff line change
@@ -39,6 +39,15 @@ lazy_static::lazy_static! {
}

pub fn glob_exec<F: FnMut(&Path)>(manifest_dir: &str, base: &Path, pattern: &str, mut f: F) {
// If settings.allow_empty_glob() == true and `base` doesn't exist, skip
// everything. This is necessary as `base` is user-controlled via `glob!/3`
// and may not exist.
let mut settings = Settings::clone_current();

if settings.allow_empty_glob() && !base.exists() {
return;
}

let glob = GlobBuilder::new(pattern)
.case_insensitive(true)
.literal_separator(true)
@@ -48,7 +57,6 @@ pub fn glob_exec<F: FnMut(&Path)>(manifest_dir: &str, base: &Path, pattern: &str

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

GLOB_STACK.lock().unwrap().push(GlobCollector {
failed: 0,
Loading