Skip to content

Commit

Permalink
cargo-insta: use cargo integration tests
Browse files Browse the repository at this point in the history
This allows us to leverage Cargo itself to build the cargo-insta binary,
reducing line count in the tests and in the Makefile.
  • Loading branch information
tamird committed Oct 2, 2023
1 parent ccaa6c5 commit bffefba
Show file tree
Hide file tree
Showing 13 changed files with 11 additions and 65 deletions.
19 changes: 5 additions & 14 deletions Makefile
Expand Up @@ -6,21 +6,12 @@ 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
@cd cargo-insta/integration-tests; CI=0 cargo run

cargotest:
test:
@echo "CARGO TESTS"
@rustup component add rustfmt 2> /dev/null
@cargo test
@cargo test --all-features
@cargo test --no-default-features
@cargo test --features redactions -- --test-threads 1
@env CI= cargo test
@env CI= cargo test --all-features
@env CI= cargo test --no-default-features
@env CI= cargo test --features redactions -- --test-threads 1

check-minver:
@echo "MINVER CHECK"
Expand Down
1 change: 0 additions & 1 deletion cargo-insta/.gitignore

This file was deleted.

3 changes: 3 additions & 0 deletions cargo-insta/Cargo.toml
Expand Up @@ -27,3 +27,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"
2 changes: 0 additions & 2 deletions cargo-insta/integration-tests/.gitignore

This file was deleted.

18 changes: 0 additions & 18 deletions cargo-insta/integration-tests/Cargo.toml

This file was deleted.

Expand Up @@ -3,34 +3,12 @@ use std::fs;
use std::path::Path;
use std::process::Command;

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

fn main() {
// copy new tests over
fs::remove_dir_all("tests").ok();
CopyBuilder::new("test-input", "tests")
.overwrite(true)
.run()
.unwrap();

// delete old build artifacts
Command::new("cargo")
.arg("clean")
.arg("--package=integration-tests")
.status()
.unwrap();

// make sure cargo-insta is built
Command::new("cargo")
.arg("build")
.current_dir("..")
.status()
.unwrap();

// run tests and accept snapshots
Command::new("../target/debug/cargo-insta")
Command::new(env!("CARGO_BIN_EXE_cargo-insta"))
.arg("test")
.arg("--accept")
.arg("--no-ignore")
Expand All @@ -46,11 +24,10 @@ fn main() {
.unwrap()
.to_str()
.unwrap();
if filename.ends_with(".rs") {
if let Some(snapshot) = filename.strip_suffix(".rs") {
let gen_file = Path::new("tests").join(filename);
let mut settings = Settings::clone_current();
settings.set_input_file(&gen_file);
let snapshot = &filename[..filename.len() - 3];
settings.bind(|| {
assert_snapshot!(snapshot, &fs::read_to_string(gen_file).unwrap());
});
Expand Down
6 changes: 1 addition & 5 deletions insta/src/utils.rs
Expand Up @@ -8,11 +8,7 @@ use std::{

/// Are we running in in a CI environment?
pub fn is_ci() -> bool {
match env::var("CI").ok().as_deref() {
Some("false") | Some("0") | Some("") => false,
None => env::var("TF_BUILD").is_ok(),
Some(_) => true,
}
env::var_os("CI").is_some() || env::var_os("TF_BUILD").is_some()
}

#[cfg(feature = "colors")]
Expand Down

0 comments on commit bffefba

Please sign in to comment.