Skip to content

Commit ae26e81

Browse files
max-sixtyclaude
andauthored
Change diff line numbers to 1-based indexing (#799)
## Summary - Changed snapshot diff line numbers from 0-based to 1-based indexing - Line numbers now start at 1 instead of 0, matching text editor conventions - Added functional test to verify the 1-based indexing behavior ## Test plan - [x] Added new functional test `test_line_numbers_1_based` that verifies line numbers are displayed as 1-based - [x] All existing tests pass - [x] Manual verification shows diffs with line numbers starting at 1 🤖 Generated with [Claude Code](https://claude.ai/code) --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 26efb60 commit ae26e81

File tree

3 files changed

+64
-5
lines changed

3 files changed

+64
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All notable changes to insta and cargo-insta are documented here.
44

5+
## Unreleased
6+
7+
- Changed diff line numbers to 1-based indexing.
8+
59
## 1.43.2
610

711
- Preserve snapshot names with `INSTA_GLOB_FILTER`. #786

cargo-insta/tests/functional/main.rs

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
/// Note that the packages must have different names, or we'll see interference
4444
/// between the tests[^1].
4545
///
46-
/// [1]: That seems to be because they all share the same `target` directory, which
46+
/// [^1]: That seems to be because they all share the same `target` directory, which
4747
/// cargo will confuse for each other if they share the same name. I haven't
4848
/// worked out why — this is the case even if the files are the same between
4949
/// two tests but with different commands — and those files exist in different
@@ -804,3 +804,58 @@ src/
804804
stderr
805805
);
806806
}
807+
808+
#[test]
809+
fn test_line_numbers_1_based() {
810+
let test_project = TestFiles::new()
811+
.add_cargo_toml("test_line_numbers")
812+
.add_file(
813+
"src/lib.rs",
814+
r#"
815+
#[test]
816+
fn test_snapshot() {
817+
insta::assert_snapshot!("line1\nline2\nline3\nline4\nline5", @"line1\nmodified_line2\nline3\nline4\nline5");
818+
}
819+
"#
820+
.to_string(),
821+
)
822+
.create_project();
823+
824+
// Run test to trigger failure and capture diff output
825+
// Use --no-ignore and -- --nocapture to get the full diff output
826+
let output = test_project
827+
.insta_cmd()
828+
.args(["test", "--no-ignore", "--", "--nocapture"])
829+
.stderr(Stdio::piped())
830+
.stdout(Stdio::piped())
831+
.output()
832+
.unwrap();
833+
834+
// Check both stdout and stderr for the diff output
835+
let stdout = String::from_utf8_lossy(&output.stdout);
836+
let stderr = String::from_utf8_lossy(&output.stderr);
837+
let combined_output = format!("{}\n{}", stdout, stderr);
838+
839+
// Check that line numbers in the diff start at 1, not 0
840+
// The diff should show line numbers like "1 1 │" for the first line
841+
assert!(
842+
combined_output.contains(" 1 1 │ line1"),
843+
"Expected line numbers to start at 1, but got:\n{}",
844+
combined_output
845+
);
846+
847+
// Also check line 2 which has the modification
848+
assert!(
849+
combined_output.contains(" 2 │-modified_line2")
850+
|| combined_output.contains(" 2 │+line2"),
851+
"Expected line 2 to be numbered as 2, but got:\n{}",
852+
combined_output
853+
);
854+
855+
// And verify line 5 is numbered as 5
856+
assert!(
857+
combined_output.contains(" 5 5 │ line5"),
858+
"Expected line 5 to be numbered as 5, but got:\n{}",
859+
combined_output
860+
);
861+
}

insta/src/output.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ impl<'a> SnapshotPrinter<'a> {
236236
print!(
237237
"{:>5} {:>5} │{}",
238238
"",
239-
style(change.new_index().unwrap()).cyan().dim().bold(),
239+
style(change.new_index().unwrap() + 1).cyan().dim().bold(),
240240
style("+").green(),
241241
);
242242
for &(emphasized, change) in change.values() {
@@ -252,7 +252,7 @@ impl<'a> SnapshotPrinter<'a> {
252252
has_changes = true;
253253
print!(
254254
"{:>5} {:>5} │{}",
255-
style(change.old_index().unwrap()).cyan().dim(),
255+
style(change.old_index().unwrap() + 1).cyan().dim(),
256256
"",
257257
style("-").red(),
258258
);
@@ -268,8 +268,8 @@ impl<'a> SnapshotPrinter<'a> {
268268
ChangeTag::Equal => {
269269
print!(
270270
"{:>5} {:>5} │ ",
271-
style(change.old_index().unwrap()).cyan().dim(),
272-
style(change.new_index().unwrap()).cyan().dim().bold(),
271+
style(change.old_index().unwrap() + 1).cyan().dim(),
272+
style(change.new_index().unwrap() + 1).cyan().dim().bold(),
273273
);
274274
for &(_, change) in change.values() {
275275
let change = render_invisible(change, newlines_matter);

0 commit comments

Comments
 (0)