Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise an error on sufficiently different insta vs cargo-insta versions #509

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
4 changes: 2 additions & 2 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions cargo-insta/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ syn = { version = "2.0.8", features = ["full", "visit", "extra-traits"] }
ignore = "0.4.17"
uuid = { version = "1.0.0", features = ["v4"] }
tempfile = "3.5.0"

# Needs pinning in Cargo.lock because of MSRV
semver = {version = "1.0.7", features = ["serde"]}
lazy_static = "1.4.0"
Expand Down
43 changes: 43 additions & 0 deletions cargo-insta/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ use std::{collections::HashSet, fmt};
use std::{env, fs};
use std::{io, process};

use cargo_metadata::MetadataCommand;
use console::{set_colors_enabled, style, Key, Term};
use insta::Snapshot;
use insta::_cargo_insta_support::{
is_ci, SnapshotPrinter, SnapshotUpdate, TestRunner, ToolConfig, UnreferencedSnapshots,
};
use itertools::Itertools;
use lazy_static::lazy_static;
use semver::Version;
use serde::Serialize;
use uuid::Uuid;
Expand Down Expand Up @@ -1248,6 +1250,8 @@ pub(crate) fn run() -> Result<(), Box<dyn Error>> {
args.remove(1);
}

check_insta_cargo_insta_compat()?;

let opts = Opts::parse_from(args);

handle_color(opts.color);
Expand All @@ -1270,3 +1274,42 @@ pub(crate) fn run() -> Result<(), Box<dyn Error>> {
Command::PendingSnapshots(cmd) => pending_snapshots_cmd(cmd),
}
}

fn check_insta_cargo_insta_compat() -> Result<(), Box<dyn std::error::Error>> {
let insta_version = read_insta_version()?;
if insta_version.major != CARGO_INSTA_VERSION.major {
eprintln!(
"{}: insta version mismatch: cargo-insta is {}, but insta is {}",
style("error").bold().red(),
*CARGO_INSTA_VERSION,
insta_version
);
return Err(QuietExit(1).into());
}

if (insta_version.minor).abs_diff(CARGO_INSTA_VERSION.minor) > 10 {
eprintln!(
"{}: insta version {} is very different from cargo-insta version {}. This may raise an error in the future.",
style("error").bold().red(),
*CARGO_INSTA_VERSION,
insta_version
);
}

Ok(())
}

lazy_static! {
static ref CARGO_INSTA_VERSION: Version =
Version::parse(env!("CARGO_PKG_VERSION")).expect("Invalid cargo-insta version number");
}

fn read_insta_version() -> Result<Version, Box<dyn std::error::Error>> {
MetadataCommand::new()
.exec()?
.packages
.iter()
.find(|package| package.name == "insta")
.map(|package| package.version.clone())
.ok_or("insta not found in cargo metadata".into())
}
Loading