From f61b60a141fe1ff4d98fec8d179c34675c70e8c4 Mon Sep 17 00:00:00 2001 From: Thomas Otto Date: Sat, 6 Jul 2024 09:20:21 +0200 Subject: [PATCH] tests: add insta for snapshot testing Insta makes writing new tests or performing changes which update the expected value(s) easier. The new reference data can be reviewed and inserted/updated automatically by `cargo insta review` (after `cargo install cargo-insta`, but that tool is optional). The snapshots are stored inline using the `@""` syntax and not in separate files because the delta test output is small and designed to be human readable. See https://insta.rs/#hello-snapshot-testing and https://docs.rs/insta/1.39.0/insta/ --- Cargo.lock | 19 +++++++++++++++++++ Cargo.toml | 9 +++++++++ src/features/side_by_side.rs | 17 ++++++++++------- src/tests/integration_test_utils.rs | 6 +++++- 4 files changed, 43 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 260c6a798..beb49b57c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -515,6 +515,7 @@ dependencies = [ "dirs", "git2", "grep-cli", + "insta", "itertools", "lazy_static", "palette", @@ -642,6 +643,18 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "insta" +version = "1.39.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "810ae6042d48e2c9e9215043563a58a80b877bc863228a74cf10c49d4620a6f5" +dependencies = [ + "console", + "lazy_static", + "linked-hash-map", + "similar", +] + [[package]] name = "itertools" version = "0.10.5" @@ -1155,6 +1168,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "24188a676b6ae68c3b2cb3a01be17fbf7240ce009799bb56d5b1409051e78fde" +[[package]] +name = "similar" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa42c91313f1d05da9b26f267f931cf178d4aba455b4c4622dd7355eb80c6640" + [[package]] name = "siphasher" version = "0.3.11" diff --git a/Cargo.toml b/Cargo.toml index d55c68e91..d5be556e7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -67,5 +67,14 @@ version = "0.29.0" default-features = false features = [] +[dev-dependencies] +insta = { version = "1.*", features = ["colors"] } + [profile.test] opt-level = 2 + +[profile.dev.package.insta] +opt-level = 3 + +[profile.dev.package.similar] +opt-level = 3 diff --git a/src/features/side_by_side.rs b/src/features/side_by_side.rs index a81823591..8db8d115c 100644 --- a/src/features/side_by_side.rs +++ b/src/features/side_by_side.rs @@ -594,16 +594,19 @@ pub mod tests { use crate::features::line_numbers::tests::*; use crate::options::theme; use crate::tests::integration_test_utils::{make_config_from_args, run_delta, DeltaTest}; + use insta::assert_snapshot; #[test] - fn test_two_minus_lines() { - DeltaTest::with_args(&["--side-by-side", "--width", "40"]) + fn test_two_fitting_minus_lines() { + // rustfmt ignores the assert macro arguments, so do the setup outside + let result = DeltaTest::with_args(&["--side-by-side", "--width", "40"]) .with_input(TWO_MINUS_LINES_DIFF) - .expect_after_header( - r#" - │ 1 │a = 1 │ │ - │ 2 │b = 23456 │ │"#, - ); + .skip_header(); + assert_snapshot!(result, @r###" + │ 1 │a = 1 │ │ + │ 2 │b = 23456 │ │ + "### + ); } #[test] diff --git a/src/tests/integration_test_utils.rs b/src/tests/integration_test_utils.rs index 09e445e92..1b2f1dfc1 100644 --- a/src/tests/integration_test_utils.rs +++ b/src/tests/integration_test_utils.rs @@ -6,7 +6,7 @@ use std::io::{BufReader, Write}; use std::path::Path; use bytelines::ByteLines; -use itertools; +use itertools::Itertools; use crate::ansi; use crate::cli; @@ -264,6 +264,10 @@ impl DeltaTestOutput { self.expect_after_skip(crate::config::HEADER_LEN, expected) } + pub fn skip_header(self) -> String { + self.output.lines().skip(config::HEADER_LEN).join("\n") + } + pub fn expect_contains(self, expected: &str) -> Self { assert!( self.output.contains(expected),