Skip to content

Commit

Permalink
cargo-insta: use cargo integration tests (#412)
Browse files Browse the repository at this point in the history
  • Loading branch information
tamird authored Nov 16, 2023
1 parent fe939a8 commit bbdf17f
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 106 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[workspace]
members = ["cargo-insta", "cargo-insta/integration-tests"]
members = ["cargo-insta"]

[features]
default = ["colors"]
Expand Down
13 changes: 5 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@ build:
doc:
@RUSTC_BOOTSTRAP=1 RUSTDOCFLAGS="--cfg=docsrs" cargo doc --no-deps --all-features

test: cargotest cargo-insta-tests

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

cargotest:
@echo "CARGO TESTS"
Expand All @@ -21,7 +15,10 @@ cargotest:
@cargo test --all-features
@cargo test --no-default-features
@cargo test --features redactions -- --test-threads 1
@cargo test -p cargo-insta
@echo "CARGO-INSTA TESTS"
# Turn off CI flag so that cargo insta test behaves as we expect
# under normal operation
@CI=0 cargo test -p cargo-insta

check-minver:
@echo "MINVER CHECK"
Expand Down
3 changes: 3 additions & 0 deletions cargo-insta/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ syn = { version = "1.0.50", features = ["full", "visit", "extra-traits"] }
ignore = "0.4.17"
uuid = { version = "1.0.0", features = ["v4"] }
tempfile = "3.5.0"

[dev-dependencies]
walkdir = "2.3.1"
13 changes: 0 additions & 13 deletions cargo-insta/integration-tests/Cargo.toml

This file was deleted.

84 changes: 0 additions & 84 deletions cargo-insta/integration-tests/src/main.rs

This file was deleted.

90 changes: 90 additions & 0 deletions cargo-insta/tests/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
use std::env;
use std::fs;
use std::path::Path;
use std::process::{Command, Output};

use insta::{assert_snapshot, Settings};
use walkdir::WalkDir;

struct OnDrop<F: FnOnce()>(Option<F>);

impl<F: FnOnce()> Drop for OnDrop<F> {
fn drop(&mut self) {
let Self(f) = self;
f.take().unwrap()();
}
}

#[test]
fn main() {
const NO_RECURSION: &str = "CARGO_INSTA_INTEGRATION_TESTS_NO_RECURSION";

if env::var_os(NO_RECURSION).is_some() {
return;
}

// copy new tests over

// late-bind files as they're copied to ensure cargo doesn't try to run
// these tests on the next invocation
let copied = std::cell::RefCell::new(Vec::new());
let _on_drop = OnDrop(Some(|| {
let copied = copied.borrow();
let copied = copied
.iter()
.filter_map(|copied| fs::remove_file(copied).err().map(|err| (copied, err)))
.collect::<Vec<_>>();
assert!(copied.is_empty(), "{:?}", copied);
}));

const SRC: &str = "tests/test-input";
const DST: &str = "tests";
for entry in WalkDir::new(SRC) {
let entry = entry.unwrap();
let source = entry.path();
if source.is_dir() {
continue;
}
let relative_source = source.strip_prefix(SRC).unwrap();
let destination = Path::new(DST).join(relative_source);
fs::copy(source, &destination).unwrap();
copied.borrow_mut().push(destination);
}

// run tests and accept snapshots
let Output {
status,
stdout,
stderr,
} = Command::new(env!("CARGO_BIN_EXE_cargo-insta"))
.env(NO_RECURSION, "this value doesn't matter")
.arg("test")
.arg("--accept")
.arg("--no-ignore")
.output()
.unwrap();
use std::io::Write as _;
std::io::stdout().write_all(&stdout).unwrap();
std::io::stderr().write_all(&stderr).unwrap();
assert!(status.success());
let stdout = std::str::from_utf8(stdout.as_slice()).unwrap();
let stderr = std::str::from_utf8(stderr.as_slice()).unwrap();
assert!(stdout.contains("insta review finished"));
assert!(stdout.contains("accepted"));
assert!(stderr.contains("Compiling"));
assert!(stderr.contains("cargo-insta"));

// use insta itself to assert snapshots
for entry in WalkDir::new(SRC) {
let entry = entry.unwrap();
let filename = entry.path().strip_prefix(SRC).unwrap().to_str().unwrap();
if let Some(snapshot) = filename.strip_suffix(".rs") {
let gen_file = Path::new(DST).join(filename);
let mut settings = Settings::clone_current();
settings.set_input_file(&gen_file);
settings.bind(|| {
assert_snapshot!(snapshot, &fs::read_to_string(gen_file).unwrap());
});
}
}
}

0 comments on commit bbdf17f

Please sign in to comment.