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..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 @@ -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);