From 97f671c1e3517d70d7b759704b720a0716e720cb Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Wed, 3 Sep 2025 08:11:44 -0700 Subject: [PATCH 1/2] Change diff line numbers to 1-based indexing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The diff output now shows line numbers starting at 1 instead of 0, making it consistent with how most text editors display line numbers. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- CHANGELOG.md | 4 ++ cargo-insta/tests/functional/main.rs | 55 ++++++++++++++++++++++++++++ insta/src/output.rs | 8 ++-- 3 files changed, 63 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d9913cb..563c58ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to insta and cargo-insta are documented here. +## Unreleased + +- Changed diff line numbers to 1-based indexing. + ## 1.43.2 - Preserve snapshot names with `INSTA_GLOB_FILTER`. #786 diff --git a/cargo-insta/tests/functional/main.rs b/cargo-insta/tests/functional/main.rs index d317faee..8b8ffee3 100644 --- a/cargo-insta/tests/functional/main.rs +++ b/cargo-insta/tests/functional/main.rs @@ -804,3 +804,58 @@ src/ stderr ); } + +#[test] +fn test_line_numbers_1_based() { + let test_project = TestFiles::new() + .add_cargo_toml("test_line_numbers") + .add_file( + "src/lib.rs", + r#" +#[test] +fn test_snapshot() { + insta::assert_snapshot!("line1\nline2\nline3\nline4\nline5", @"line1\nmodified_line2\nline3\nline4\nline5"); +} +"# + .to_string(), + ) + .create_project(); + + // Run test to trigger failure and capture diff output + // Use --no-ignore and -- --nocapture to get the full diff output + let output = test_project + .insta_cmd() + .args(["test", "--no-ignore", "--", "--nocapture"]) + .stderr(Stdio::piped()) + .stdout(Stdio::piped()) + .output() + .unwrap(); + + // Check both stdout and stderr for the diff output + let stdout = String::from_utf8_lossy(&output.stdout); + let stderr = String::from_utf8_lossy(&output.stderr); + let combined_output = format!("{}\n{}", stdout, stderr); + + // Check that line numbers in the diff start at 1, not 0 + // The diff should show line numbers like "1 1 │" for the first line + assert!( + combined_output.contains(" 1 1 │ line1"), + "Expected line numbers to start at 1, but got:\n{}", + combined_output + ); + + // Also check line 2 which has the modification + assert!( + combined_output.contains(" 2 │-modified_line2") + || combined_output.contains(" 2 │+line2"), + "Expected line 2 to be numbered as 2, but got:\n{}", + combined_output + ); + + // And verify line 5 is numbered as 5 + assert!( + combined_output.contains(" 5 5 │ line5"), + "Expected line 5 to be numbered as 5, but got:\n{}", + combined_output + ); +} diff --git a/insta/src/output.rs b/insta/src/output.rs index 2a5a96f5..c18448c2 100644 --- a/insta/src/output.rs +++ b/insta/src/output.rs @@ -236,7 +236,7 @@ impl<'a> SnapshotPrinter<'a> { print!( "{:>5} {:>5} │{}", "", - style(change.new_index().unwrap()).cyan().dim().bold(), + style(change.new_index().unwrap() + 1).cyan().dim().bold(), style("+").green(), ); for &(emphasized, change) in change.values() { @@ -252,7 +252,7 @@ impl<'a> SnapshotPrinter<'a> { has_changes = true; print!( "{:>5} {:>5} │{}", - style(change.old_index().unwrap()).cyan().dim(), + style(change.old_index().unwrap() + 1).cyan().dim(), "", style("-").red(), ); @@ -268,8 +268,8 @@ impl<'a> SnapshotPrinter<'a> { ChangeTag::Equal => { print!( "{:>5} {:>5} │ ", - style(change.old_index().unwrap()).cyan().dim(), - style(change.new_index().unwrap()).cyan().dim().bold(), + style(change.old_index().unwrap() + 1).cyan().dim(), + style(change.new_index().unwrap() + 1).cyan().dim().bold(), ); for &(_, change) in change.values() { let change = render_invisible(change, newlines_matter); From b9b9e4acfea4a2c443c657968b1725ac2bb40105 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Wed, 3 Sep 2025 08:21:51 -0700 Subject: [PATCH 2/2] Fix clippy doc_suspicious_footnotes warning MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Corrected footnote reference format from [1]: to [^1]: to match the footnote reference style. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- cargo-insta/tests/functional/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cargo-insta/tests/functional/main.rs b/cargo-insta/tests/functional/main.rs index 8b8ffee3..005da188 100644 --- a/cargo-insta/tests/functional/main.rs +++ b/cargo-insta/tests/functional/main.rs @@ -43,7 +43,7 @@ /// Note that the packages must have different names, or we'll see interference /// between the tests[^1]. /// -/// [1]: That seems to be because they all share the same `target` directory, which +/// [^1]: That seems to be because they all share the same `target` directory, which /// cargo will confuse for each other if they share the same name. I haven't /// worked out why — this is the case even if the files are the same between /// two tests but with different commands — and those files exist in different