Skip to content

Conversation

@cruessler
Copy link
Contributor

This is a companion to #2287. It updates the diff slider test to use imara-diff 0.2. In my non-representative sample of ~3000 diffs from gitoxide, it matches the git baseline in ~95 % of cases.

❯ cargo test --package gix-diff-tests slider -- --nocapture
[…]

thread 'blob::slider::baseline' (23361) panicked at gix-diff/tests/diff/blob/slider.rs:83:5:
assertion `left == right` failed: matching diffs 2930 == total diffs 3094 [94.70 %]

use gix_diff::blob::intern::TokenSource;
use gix_diff::blob::unified_diff::ContextSize;
use gix_diff::blob::{Algorithm, UnifiedDiff};
use gix_diff::blob::v2::{Algorithm, BasicLineDiffPrinter, Diff, InternedInput, UnifiedDiffConfig};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's interesting that v2 is available at all as it shouldn't be available by default.
Let me fix that

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, seems like this code should not compile, it's fine on main.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about that, I didn't realise that this is a test in gix-diff-tests, which enables the feature. It was a long day yesterday 😅.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries, I also could have provided that context. :-)

@cruessler cruessler marked this pull request as ready for review December 14, 2025 12:38
@cruessler
Copy link
Contributor Author

I’ve re-added the v1 slider test that I had updated in this PR’s first commit. Now, there’s both the baseline_v1 and the baseline_v2 test, so they can be easily compared.

- deduplicate a little, and do away with blanket `unreachable!()`.
@Byron Byron requested a review from Copilot December 16, 2025 08:27
Copy link
Member

@Byron Byron left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's great to have! I think we should also think about having a minimal default set of slider tests that we keep, at some point, so we have some CI protections for this. But it's a note for the future, when it settles a bit more.

Besides that, I asked Copilot to take a look, but would also love it if you could try it once more as I didn't have a slider setup to actually run the code.

But if you give the green light, I will happily merge this.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the diff slider test to support imara-diff 0.2 by adding a new v2 API test alongside the existing v1 test. The changes refactor common logic into helper functions to reduce duplication and support both API versions.

Key changes:

  • Renames existing baseline() test to baseline_v1() and adds new baseline_v2() test for the v2 API
  • Extracts common parsing logic into parse_dir_entry() helper function
  • Moves diff assertion logic to assert_diffs() helper function
  • Adds fold_to_unidiff() method to the Baseline type for cleaner code reuse

Comment on lines +149 to +150
let old_data = std::fs::read(asset_dir.join(format!("{old_blob_id}.blob"))).unwrap();
let new_data = std::fs::read(asset_dir.join(format!("{new_blob_id}.blob"))).unwrap();
Copy link

Copilot AI Dec 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The .unwrap() calls violate the project's error handling guidelines. According to the coding guidelines, tests should avoid .unwrap() in favor of .expect() with meaningful context or ?. These file reads should either be propagated with ? or use .expect("meaningful context") to explain why the files are expected to exist.

Copilot generated this review using guidance from repository custom instructions.
continue;
};

let input = InternedInput::new(old_data.to_str().unwrap(), new_data.to_str().unwrap());
Copy link

Copilot AI Dec 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The .unwrap() calls on .to_str() are problematic. The project guidelines prohibit .unwrap() in favor of .expect() with context or proper error handling with ?. Additionally, this assumes the blob data is valid UTF-8, which may not always be true for binary files or files with different encodings. This could cause the test to panic instead of handling non-UTF-8 data gracefully.

Copilot generated this review using guidance from repository custom instructions.
Comment on lines +92 to +96
let algorithm = match algorithm {
gix_diff::blob::Algorithm::Myers => Algorithm::Myers,
gix_diff::blob::Algorithm::Histogram => Algorithm::Histogram,
gix_diff::blob::Algorithm::MyersMinimal => Algorithm::MyersMinimal,
};
Copy link

Copilot AI Dec 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The algorithm mapping is duplicated between parse_dir_entry (lines 138-142) and this section. Since parse_dir_entry already returns the Algorithm enum from the v1 API, this remapping to v2 Algorithm could be extracted into a helper function to avoid duplication and make the relationship between v1 and v2 algorithms clearer.

Copilot uses AI. Check for mistakes.
Comment on lines +98 to +99
let mut diff = Diff::compute(algorithm, &input);
diff.postprocess_lines(&input);
Copy link

Copilot AI Dec 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable name diff is shadowed after mutation (line 98 creates it, line 99 mutates it). Consider renaming to clarify the mutation, or combine the operations if the intermediate state isn't needed.

Copilot uses AI. Check for mistakes.
}

impl Baseline<'_> {
/// Fold all [`DiffHunk`]s we produce into a unified_diff string
Copy link

Copilot AI Dec 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The doc comment uses backticks around DiffHunk and refers to "we produce" but doesn't fully explain what this method does or when it should be used. Consider clarifying that this method converts the baseline hunks into a unified diff format string for comparison purposes.

Suggested change
/// Fold all [`DiffHunk`]s we produce into a unified_diff string
/// Converts all baseline [`DiffHunk`]s into a single unified diff format string.
///
/// This method is typically used to produce a unified diff representation of the baseline hunks,
/// suitable for comparison with other diff outputs in tests or analysis.

Copilot uses AI. Check for mistakes.
Comment on lines +136 to +146
unreachable!("BUG: Need file named '<name>.<algorithm>'")
};
let algorithm = match algorithm {
"myers" => Algorithm::Myers,
"histogram" => Algorithm::Histogram,
other => unreachable!("'{other}' is not a supported algorithm"),
};

let parts: Vec<_> = name.split('-').collect();
let [old_blob_id, new_blob_id] = parts[..] else {
unreachable!("BUG: name part of filename must be <old_blob_id>-<new_blob_id>");
Copy link

Copilot AI Dec 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message format is inconsistent. Line 136 uses "BUG: Need file named", line 141 uses single quotes around the variable without "BUG:", and line 146 uses "BUG: name part of filename must be". Consider using a consistent format for all unreachable error messages, such as starting all with "BUG:" prefix.

Copilot uses AI. Check for mistakes.
@cruessler
Copy link
Contributor Author

That's great to have! I think we should also think about having a minimal default set of slider tests that we keep, at some point, so we have some CI protections for this. But it's a note for the future, when it settles a bit more.

Yep, having a set of default tests definitely is on my todo list as well.

Besides that, I asked Copilot to take a look, but would also love it if you could try it once more as I didn't have a slider setup to actually run the code.

I just re-ran the tests with your updates on my machine and did not observe any differences to the previous state.

Just for context: in case you were wondering, I intentionally did not de-duplicate the tests right away, in order to facilitate review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants