Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
57 changes: 56 additions & 1 deletion cargo-insta/tests/functional/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
);
}
8 changes: 4 additions & 4 deletions insta/src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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(),
);
Expand All @@ -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);
Expand Down
Loading