Skip to content

Commit

Permalink
Merge branch 'master' into insta-workspace-root
Browse files Browse the repository at this point in the history
  • Loading branch information
max-sixty committed Sep 27, 2024
2 parents 9fb8f34 + 90e429b commit 5dc6856
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 12 deletions.
1 change: 1 addition & 0 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 @@ -35,3 +35,4 @@ clap = { workspace=true }
walkdir = "2.3.1"
similar= "2.2.1"
itertools = "0.10.0"
termcolor = "1.1.2"
39 changes: 30 additions & 9 deletions cargo-insta/tests/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
/// Integration tests which allow creating a full repo, running `cargo-insta`
/// and then checking the output.
///
/// Often we want to see output from the test commands we run here; for example
/// a `dbg!` statement we add while debugging. Cargo by default hides the output
/// of passing tests.
/// - Like any test, to forward the output of an outer test (i.e. one of the
/// `#[test]`s in this file) to the terminal, pass `--nocapture` to the test
/// runner, like `cargo insta test -- --nocapture`.
/// - To forward the output of an inner test (i.e. the test commands we create
/// and run within an outer test) to the output of an outer test, pass
/// `--nocapture` in the command we create; for example `.args(["test",
/// "--accept", "--", "--nocapture"])`. We then also need to pass
/// `--nocapture` to the outer test to forward that to the terminal.
///
/// We can write more docs if that would be helpful. For the moment one thing to
/// be aware of: it seems the packages must have different names, or we'll see
/// interference between the tests.
Expand All @@ -20,6 +32,7 @@ use std::path::{Path, PathBuf};
use std::process::Command;
use std::{env, fs::remove_dir_all};

use console::style;
use ignore::WalkBuilder;
use insta::assert_snapshot;
use itertools::Itertools;
Expand Down Expand Up @@ -67,18 +80,26 @@ fn target_dir() -> PathBuf {
}

fn assert_success(output: &std::process::Output) {
// Print stderr. Cargo test hides this when tests are successful, but if a
// test successfully exectues a command but then fails (e.g. on a snapshot),
// we would otherwise lose any output from the command such as `dbg!`
// statements.
eprint!("{}", String::from_utf8_lossy(&output.stderr));
eprint!("{}", String::from_utf8_lossy(&output.stdout));
// Color the inner output so we can tell what's coming from there vs our own
// output

// Should we also do something like indent them? Or add a prefix?
let stdout = format!("{}", style(String::from_utf8_lossy(&output.stdout)).green());
let stderr = format!("{}", style(String::from_utf8_lossy(&output.stderr)).red());

assert!(
output.status.success(),
"Tests failed: {}\n{}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
stdout,
stderr
);

// Print stdout & stderr. Cargo test hides this when tests are successful, but if an
// test function in this file successfully executes an inner test command
// but then fails (e.g. on a snapshot), we would otherwise lose any output
// from that inner command, such as `dbg!` statements.
eprint!("{}", stdout);
eprint!("{}", stderr);
}

fn assert_failure(output: &std::process::Output) {
Expand Down Expand Up @@ -243,7 +264,7 @@ fn test_json_snapshot() {

let output = test_project
.cmd()
.args(["test", "--accept"])
.args(["test", "--accept", "--", "--nocapture"])
.output()
.unwrap();

Expand Down
2 changes: 1 addition & 1 deletion insta/src/content/yaml/vendored/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ fn escape_str(wr: &mut dyn fmt::Write, v: &str) -> Result<(), fmt::Error> {
}

impl<'a> YamlEmitter<'a> {
pub fn new(writer: &'a mut dyn fmt::Write) -> YamlEmitter {
pub fn new(writer: &'a mut dyn fmt::Write) -> YamlEmitter<'a> {
YamlEmitter {
writer,
best_indent: 2,
Expand Down
2 changes: 1 addition & 1 deletion insta/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ impl std::str::FromStr for UnreferencedSnapshots {
}
}

/// Memoizes a snapshot file in the reference file.
/// Memoizes a snapshot file in the reference file, as part of removing unreferenced snapshots.
pub fn memoize_snapshot_file(snapshot_file: &Path) {
if let Ok(path) = env::var("INSTA_SNAPSHOT_REFERENCES_FILE") {
let mut f = fs::OpenOptions::new()
Expand Down
2 changes: 1 addition & 1 deletion insta/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ pub fn assert_snapshot(
let new_snapshot =
ctx.new_snapshot(SnapshotContents::new(new_snapshot_value.into(), kind), expr);

// memoize the snapshot file if requested.
// memoize the snapshot file if requested, as part of potentially removing unreferenced snapshots
if let Some(ref snapshot_file) = ctx.snapshot_file {
memoize_snapshot_file(snapshot_file);
}
Expand Down

0 comments on commit 5dc6856

Please sign in to comment.