Skip to content

Commit

Permalink
[target-spec-miette] switch fixtures to snapbox
Browse files Browse the repository at this point in the history
I like `cargo insta`, but snapbox's SVGs are awesome and worth recommending.
  • Loading branch information
sunshowers committed Jan 4, 2025
1 parent 10066d2 commit fe3e11b
Show file tree
Hide file tree
Showing 29 changed files with 555 additions and 112 deletions.
88 changes: 68 additions & 20 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.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ guppy-workspace-hack = "0.1.0"
include_dir = "0.7.4"
insta = "1.41.1"
miette = "7.4.0"
snapbox = { version = "0.6.21", features = ["term-svg"] }

[workspace.package]
# Note: we commit to supporting the last 6 months of Rust releases. This
Expand Down
2 changes: 1 addition & 1 deletion target-spec-miette/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ target-spec = { version = "3.3.1", path = "../target-spec" }

[dev-dependencies]
datatest-stable.workspace = true
insta.workspace = true
miette = { workspace = true, features = ["fancy"] }
snapbox.workspace = true
target-spec = { version = "3.3.1", path = "../target-spec", features = [
"custom",
] }
Expand Down
17 changes: 5 additions & 12 deletions target-spec-miette/tests/datatest-snapshot/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,21 @@
//! These tests live here because they depend on target-spec with the custom
//! feature enabled, as well as target-spec-miette.
use crate::helpers::bind_insta_settings;
use crate::helpers::snapbox_assert_ansi;
use datatest_stable::Utf8Path;
use target_spec::TargetFeatures;
use target_spec_miette::IntoMietteDiagnostic;

pub(crate) fn custom_invalid(path: &Utf8Path, contents: String) -> datatest_stable::Result<()> {
std::env::set_var("CLICOLOR_FORCE", "1");

let (_guard, insta_prefix) =
bind_insta_settings(path, "../datatest-snapshot/snapshots/custom-invalid");

let error = target_spec::Platform::new_custom("my-target", &contents, TargetFeatures::none())
.expect_err("expected input to fail");

let diagnostic = error.into_diagnostic();
insta::assert_snapshot!(
format!("{insta_prefix}-display"),
// This displays fancy output. Note the use of assert_snapshot, not
// assert_debug_snapshot, since the latter uses the pretty-printed Debug
// (which doesn't do what you would expect with miette/anyhow etc).
format!("{:?}", miette::Report::new_boxed(diagnostic)),
);

// Use Debug output on the report to get the nicely formatted output.
let output = format!("{:?}", miette::Report::new_boxed(diagnostic));

snapbox_assert_ansi("custom-invalid", path, output);
Ok(())
}
15 changes: 4 additions & 11 deletions target-spec-miette/tests/datatest-snapshot/expr.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
// Copyright (c) The cargo-guppy Contributors
// SPDX-License-Identifier: MIT OR Apache-2.0

use crate::helpers::bind_insta_settings;
use crate::helpers::snapbox_assert_ansi;
use datatest_stable::Utf8Path;
use target_spec_miette::IntoMietteDiagnostic;

pub(crate) fn expr_invalid(path: &Utf8Path, contents: String) -> datatest_stable::Result<()> {
std::env::set_var("CLICOLOR_FORCE", "1");

let (_guard, insta_prefix) =
bind_insta_settings(path, "../datatest-snapshot/snapshots/expr-invalid");

let error = target_spec::TargetSpec::new(contents.trim_end().to_owned())
.expect_err("expected input to fail");

let diagnostic = error.into_diagnostic();
insta::assert_snapshot!(
format!("{insta_prefix}-display"),
// This displays fancy output. Note the use of assert_snapshot, not
// assert_debug_snapshot, since the latter uses the pretty-printed Debug
// (which doesn't do what you would expect with miette/anyhow etc).
format!("{:?}", miette::Report::new_boxed(diagnostic)),
);
// Use Debug output on the report to get the nicely formatted output.
let output = format!("{:?}", miette::Report::new_boxed(diagnostic));

snapbox_assert_ansi("expr-invalid", path, output);
Ok(())
}
40 changes: 26 additions & 14 deletions target-spec-miette/tests/datatest-snapshot/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,33 @@
// SPDX-License-Identifier: MIT OR Apache-2.0

use datatest_stable::Utf8Path;
use insta::internals::SettingsBindDropGuard;
use snapbox::{data::DataFormat, Data};
use std::path::PathBuf;

/// Binds insta settings for a test, and returns the prefix to use for snapshots.
pub(crate) fn bind_insta_settings<'a>(
path: &'a Utf8Path,
snapshot_path: &str,
) -> (SettingsBindDropGuard, &'a str) {
let mut settings = insta::Settings::clone_current();
// Make insta suitable for datatest-stable use.
settings.set_input_file(path);
settings.set_snapshot_path(snapshot_path);
settings.set_prepend_module_to_snapshot(false);
pub(crate) fn snapbox_assert_ansi(test_name: &str, path: &Utf8Path, actual_ansi: String) {
// Currently assuming all files are in a single directory.
let b = snapbox::Assert::new().action_env("SNAPSHOTS");
let file_name = path.file_name().unwrap();

let guard = settings.bind_to_scope();
let insta_prefix = path.file_name().unwrap();
// Store SVG and ANSI snapshots. Use the binary representation to ensure
// that no post-processing of text happens.
b.eq(
Data::binary(actual_ansi.clone()).coerce_to(DataFormat::TermSvg),
Data::read_from(
&snapshot_path(test_name, file_name, "svg"),
Some(DataFormat::TermSvg),
),
);
b.eq(
Data::binary(actual_ansi),
Data::read_from(&snapshot_path(test_name, file_name, "ansi"), None),
);
}

(guard, insta_prefix)
fn snapshot_path(test_name: &str, file_name: &str, ext: &str) -> PathBuf {
snapbox::utils::current_dir!()
.join("snapshots")
.join(test_name)
.join(file_name)
.with_extension(ext)
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
---
source: target-spec-miette/tests/datatest-snapshot/custom.rs
expression: "format!(\"{:?}\", miette::Report::new_boxed(diagnostic))"
snapshot_kind: text
---

× error deserializing custom target JSON for `my-target`
╭─[2:13]
1 │ {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
---
source: target-spec-miette/tests/datatest-snapshot/custom.rs
expression: "format!(\"{:?}\", miette::Report::new_boxed(diagnostic))"
snapshot_kind: text
---

× error deserializing custom target JSON for `my-target`
╭─[32:29]
31 │ },
Expand Down
Loading

0 comments on commit fe3e11b

Please sign in to comment.