-
-
Notifications
You must be signed in to change notification settings - Fork 397
Add script to generate tests cases for gix-diff
#2197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Byron
merged 4 commits into
GitoxideLabs:main
from
cruessler:add-tests-for-slider-problem
Nov 2, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| ## How to run diff-slider tests | ||
|
|
||
| The idea is to use https://github.com/mhagger/diff-slider-tools to create slider information for use to generate a test which invokes our own implementation and compares it to Git itself. | ||
| Follow these instructions to set it up. | ||
|
|
||
| 1. DIFF_SLIDER_TOOLS=/your/anticipated/path/to/diff-slider-tools | ||
| 2. `git clone https://github.com/mhagger/diff-slider-tools $DIFF_SLIDER_TOOLS` | ||
| 3. `pushd $DIFF_SLIDER_TOOLS` | ||
| 4. Follow [these instructions](https://github.com/mhagger/diff-slider-tools/blob/b59ed13d7a2a6cfe14a8f79d434b6221cc8b04dd/README.md?plain=1#L122-L146) to generate a file containing the slider information. Be sure to set the `repo` variable as it's used in later script invocations. | ||
| - Note that `get-corpus` must be run with `./get-corpus`. | ||
| - You can use an existing repository, for instance via `repo=git-human`, so there is no need to find your own repository to test. | ||
| - The script suite is very slow, and it's recommended to only set a range of commits, or use a small repository for testing. | ||
|
|
||
| Finally, run the `internal-tools` program to turn that file into a fixture called `make_diff_for_sliders_repo.sh`. | ||
|
|
||
| ```shell | ||
| # run inside `gitoxide` | ||
| popd | ||
| cargo run --package internal-tools -- \ | ||
| create-diff-cases \ | ||
| --sliders-file $DIFF_SLIDER_TOOLS/corpus/$repo.sliders \ | ||
| --worktree-dir $DIFF_SLIDER_TOOLS/corpus/$repo.git/ \ | ||
| --destination-dir gix-diff/tests/fixtures/ | ||
| ``` | ||
|
|
||
| Finally, run `cargo test -p gix-diff-tests sliders -- --nocapture` to execute the actual tests to compare. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| pub(crate) mod pipeline; | ||
| mod platform; | ||
| mod slider; | ||
| mod unified_diff; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,253 @@ | ||
| use gix_diff::blob::intern::TokenSource; | ||
| use gix_diff::blob::unified_diff::ContextSize; | ||
| use gix_diff::blob::{Algorithm, UnifiedDiff}; | ||
| use gix_testtools::bstr::{BString, ByteVec}; | ||
|
|
||
| #[test] | ||
| fn baseline() -> gix_testtools::Result { | ||
| let worktree_path = gix_testtools::scripted_fixture_read_only_standalone("make_diff_for_sliders_repo.sh")?; | ||
| let asset_dir = worktree_path.join("assets"); | ||
|
|
||
| let dir = std::fs::read_dir(&worktree_path)?; | ||
|
|
||
| let mut count = 0; | ||
| for entry in dir { | ||
| let entry = entry?; | ||
| let file_name = entry.file_name().into_string().expect("to be string"); | ||
|
|
||
| if !file_name.ends_with(".baseline") { | ||
| continue; | ||
| } | ||
| count += 1; | ||
|
|
||
| let parts: Vec<_> = file_name.split('.').collect(); | ||
| let [name, algorithm, ..] = parts[..] else { | ||
| unreachable!() | ||
| }; | ||
| let algorithm = match algorithm { | ||
| "myers" => Algorithm::Myers, | ||
| "histogram" => Algorithm::Histogram, | ||
| _ => unreachable!(), | ||
| }; | ||
|
|
||
| let parts: Vec<_> = name.split('-').collect(); | ||
| let [old_blob_id, new_blob_id] = parts[..] else { | ||
| unreachable!(); | ||
| }; | ||
|
|
||
| let old_data = std::fs::read(asset_dir.join(format!("{old_blob_id}.blob")))?; | ||
| let new_data = std::fs::read(asset_dir.join(format!("{new_blob_id}.blob")))?; | ||
|
|
||
| let interner = gix_diff::blob::intern::InternedInput::new( | ||
| tokens_for_diffing(old_data.as_slice()), | ||
| tokens_for_diffing(new_data.as_slice()), | ||
| ); | ||
|
|
||
| let actual = gix_diff::blob::diff( | ||
| algorithm, | ||
| &interner, | ||
| UnifiedDiff::new( | ||
| &interner, | ||
| baseline::DiffHunkRecorder::new(), | ||
| ContextSize::symmetrical(3), | ||
| ), | ||
| )?; | ||
|
|
||
| let baseline_path = worktree_path.join(file_name); | ||
| let baseline = std::fs::read(baseline_path)?; | ||
| let baseline = baseline::Baseline::new(&baseline); | ||
|
|
||
| let actual = actual | ||
| .iter() | ||
| .fold(BString::default(), |mut acc, diff_hunk| { | ||
| acc.push_str(diff_hunk.header.to_string().as_str()); | ||
| acc.push(b'\n'); | ||
|
|
||
| acc.extend_from_slice(&diff_hunk.lines); | ||
|
|
||
| acc | ||
| }) | ||
| .to_string(); | ||
|
|
||
| let baseline = baseline | ||
| .fold(BString::default(), |mut acc, diff_hunk| { | ||
| acc.push_str(diff_hunk.header.to_string().as_str()); | ||
| acc.push(b'\n'); | ||
|
|
||
| acc.extend_from_slice(&diff_hunk.lines); | ||
|
|
||
| acc | ||
| }) | ||
| .to_string(); | ||
|
|
||
| pretty_assertions::assert_eq!(actual, baseline); | ||
| } | ||
|
|
||
| if count == 0 { | ||
| eprintln!("Slider baseline isn't setup - look at ./gix-diff/tests/README.md for instructions"); | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| fn tokens_for_diffing(data: &[u8]) -> impl TokenSource<Token = &[u8]> { | ||
| gix_diff::blob::sources::byte_lines(data) | ||
| } | ||
|
|
||
| mod baseline { | ||
| use gix_object::bstr::ByteSlice; | ||
| use std::iter::Peekable; | ||
|
|
||
| use gix_diff::blob::unified_diff::{ConsumeHunk, HunkHeader}; | ||
| use gix_object::bstr::{self, BString}; | ||
|
|
||
| static START_OF_HEADER: &[u8; 4] = b"@@ -"; | ||
|
|
||
| #[derive(Debug, PartialEq)] | ||
| pub struct DiffHunk { | ||
| pub header: HunkHeader, | ||
| pub lines: BString, | ||
| } | ||
|
|
||
| pub struct DiffHunkRecorder { | ||
| inner: Vec<DiffHunk>, | ||
| } | ||
|
|
||
| impl DiffHunkRecorder { | ||
| pub fn new() -> Self { | ||
| Self { inner: Vec::new() } | ||
| } | ||
| } | ||
|
|
||
| impl ConsumeHunk for DiffHunkRecorder { | ||
| type Out = Vec<DiffHunk>; | ||
|
|
||
| fn consume_hunk( | ||
| &mut self, | ||
| header: HunkHeader, | ||
| lines: &[(gix_diff::blob::unified_diff::DiffLineKind, &[u8])], | ||
| ) -> std::io::Result<()> { | ||
| let mut buf = Vec::new(); | ||
|
|
||
| for &(kind, line) in lines { | ||
| buf.push(kind.to_prefix() as u8); | ||
| buf.extend_from_slice(line); | ||
| buf.push(b'\n'); | ||
| } | ||
|
|
||
| let diff_hunk = DiffHunk { | ||
| header, | ||
| lines: buf.into(), | ||
| }; | ||
|
|
||
| self.inner.push(diff_hunk); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| fn finish(self) -> Self::Out { | ||
| self.inner | ||
| } | ||
| } | ||
|
|
||
| type Lines<'a> = Peekable<bstr::Lines<'a>>; | ||
|
|
||
| pub struct Baseline<'a> { | ||
| lines: Lines<'a>, | ||
| } | ||
|
|
||
| impl<'a> Baseline<'a> { | ||
| pub fn new(content: &'a [u8]) -> Baseline<'a> { | ||
| let mut lines = content.lines().peekable(); | ||
| skip_header(&mut lines); | ||
| Baseline { lines } | ||
| } | ||
| } | ||
|
|
||
| impl Iterator for Baseline<'_> { | ||
| type Item = DiffHunk; | ||
|
|
||
| fn next(&mut self) -> Option<Self::Item> { | ||
| let mut hunk_header = None; | ||
| let mut hunk_lines = Vec::new(); | ||
|
|
||
| while let Some(line) = self.lines.next() { | ||
| if line.starts_with(START_OF_HEADER) { | ||
| assert!(hunk_header.is_none(), "should not overwrite existing hunk_header"); | ||
| hunk_header = parse_hunk_header(line).ok(); | ||
|
|
||
| continue; | ||
| } | ||
|
|
||
| match line[0] { | ||
| b' ' | b'+' | b'-' => { | ||
| hunk_lines.extend_from_slice(line); | ||
| hunk_lines.push(b'\n'); | ||
| } | ||
| _ => unreachable!("BUG: expecting unified diff format"), | ||
| } | ||
|
|
||
| match self.lines.peek() { | ||
| Some(next_line) if next_line.starts_with(START_OF_HEADER) => break, | ||
| None => break, | ||
| _ => {} | ||
| } | ||
| } | ||
|
|
||
| hunk_header.map(|hunk_header| DiffHunk { | ||
| header: hunk_header, | ||
| lines: hunk_lines.into(), | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| fn skip_header(lines: &mut Lines) { | ||
| // diff --git a/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa b/bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb | ||
| // index ccccccc..ddddddd 100644 | ||
| // --- a/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | ||
| // +++ b/bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb | ||
|
|
||
| let line = lines.next().expect("line to be present"); | ||
| assert!(line.starts_with(b"diff --git ")); | ||
|
|
||
| let line = lines.next().expect("line to be present"); | ||
| assert!(line.starts_with(b"index ")); | ||
|
|
||
| let line = lines.next().expect("line to be present"); | ||
| assert!(line.starts_with(b"--- ")); | ||
|
|
||
| let line = lines.next().expect("line to be present"); | ||
| assert!(line.starts_with(b"+++ ")); | ||
| } | ||
|
|
||
| /// Parse diff hunk headers that conform to the unified diff hunk header format. | ||
| /// | ||
| /// The parser is very primitive and relies on the fact that `+18` is parsed as `18`. This | ||
| /// allows us to split the input on ` ` and `,` only. | ||
| /// | ||
| /// @@ -18,6 +18,7 @@ abc def ghi | ||
| /// @@ -{before_hunk_start},{before_hunk_len} +{after_hunk_start},{after_hunk_len} @@ | ||
| fn parse_hunk_header(line: &[u8]) -> gix_testtools::Result<HunkHeader> { | ||
| let line = line.strip_prefix(START_OF_HEADER).unwrap(); | ||
|
|
||
| let parts: Vec<_> = line.split(|b| *b == b' ' || *b == b',').collect(); | ||
| let [before_hunk_start, before_hunk_len, after_hunk_start, after_hunk_len, ..] = parts[..] else { | ||
| unreachable!() | ||
| }; | ||
|
|
||
| Ok(HunkHeader { | ||
| before_hunk_start: parse_number(before_hunk_start), | ||
| before_hunk_len: parse_number(before_hunk_len), | ||
| after_hunk_start: parse_number(after_hunk_start), | ||
| after_hunk_len: parse_number(after_hunk_len), | ||
| }) | ||
| } | ||
|
|
||
| fn parse_number(bytes: &[u8]) -> u32 { | ||
| bytes | ||
| .to_str() | ||
| .expect("to be a valid UTF-8 string") | ||
| .parse::<u32>() | ||
| .expect("to be a number") | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| # The slider assets that are needed for that slider fixture script to work and build the fixture. | ||
| /assets/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| # The auto-generated sliders fixtures. For now it's experimental, but we may store it later once it's all working. | ||
| /make_diff_for_sliders_repo.tar |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| #!/usr/bin/env bash | ||
| set -eu -o pipefail | ||
|
|
||
| # This script is a placeholder for a script generated by the | ||
| # `create-diff-cases` subcommand of `internal-tools`. The placeholder does | ||
| # nothing except making the associated test trivially pass. | ||
| # | ||
| # See the `gix-diff/tests/README.md` file for more information. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I love to keep this info, but it feels it would be more accessible in
gix-diff/README.md(instead of a seemingly fake shell script).Or am I missing something, maybe the placeholder is needed for something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And in that file, could you provide a snipped, behind
<details><summary>…that I can use as a diff slider example?That way I don't have to repeat all these steps and… run
python… (yes, I am snobby ;)), just to try it out.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I can tell,
make_diff_for_sliders_repo.shneeds to be present for this line not to fail: https://github.com/cruessler/gitoxide/blob/dd5127f54ce4e055e6a87719b4962482cd82090d/gix-diff/tests/diff/blob/slider.rs#L257. I just deleted the script locally which made the associated test fail with a file not found message.What do you mean by “diff slider example”? If you want the test to run on one single diff in order to compare the git baseline against
gix-diff, you would need two.blobfiles inassetsas well as the corresponding code in the script to generate the.baselinefiles (one for each of the 2 diff algorithms currently being tested). While that, in an of itself, would be easy to add, the resulting test would most likely fail asgix-diffdoesn’t yet match the git baseline in most cases as far as I can tell. Which is why I didn’t even try in the first place. 😄There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apart from that, I was also wondering where the best place for the info that currently lives in the script is. Feel free to move it to a more appropriate place, the
README.mdseems like a good choice to me!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This clearly shows that I don't fully understand the setup yet. What I read made me think the slider file is all that's needed and the test only depends on that, but that's not the case as it doesn't include the actual blobs. It just refers to them by ID.
So I will be back with this tomorrow probably and spend the time that I probably tried to safe today 😅.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That kind of feedback is already very valuable! 😄 I’m happy to provide more context, hoping to make this a bit more self-contained.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually think you already did everything you could and I will use my ignorance and naiveté towards that matter to make it work for other readers with a similar level of ignorance and naiveté :D. So I think I am predestined to do that 😁.