Skip to content

Commit 5130610

Browse files
committed
Address copilot review
1 parent 1e3efab commit 5130610

File tree

1 file changed

+65
-37
lines changed

1 file changed

+65
-37
lines changed

gix-diff/tests/diff/blob/slider.rs

Lines changed: 65 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
use gix_diff::blob::Algorithm;
21
use gix_object::bstr::ByteSlice;
32
use gix_testtools::bstr::{BString, ByteVec};
43
use pretty_assertions::StrComparison;
5-
use std::ffi::OsStr;
6-
use std::path::Path;
74

85
#[test]
96
fn baseline_v1() -> gix_testtools::Result {
@@ -18,7 +15,13 @@ fn baseline_v1() -> gix_testtools::Result {
1815

1916
for entry in dir {
2017
let entry = entry?;
21-
let Some((file_name, algorithm, old_data, new_data)) = parse_dir_entry(&asset_dir, &entry.file_name()) else {
18+
let Some(baseline::DirEntry {
19+
file_name,
20+
algorithm,
21+
old_data,
22+
new_data,
23+
}) = baseline::parse_dir_entry(&asset_dir, &entry.file_name())?
24+
else {
2225
continue;
2326
};
2427

@@ -84,11 +87,20 @@ fn baseline_v2() -> gix_testtools::Result {
8487

8588
for entry in dir {
8689
let entry = entry?;
87-
let Some((file_name, algorithm, old_data, new_data)) = parse_dir_entry(&asset_dir, &entry.file_name()) else {
90+
let Some(baseline::DirEntry {
91+
file_name,
92+
algorithm,
93+
old_data,
94+
new_data,
95+
}) = baseline::parse_dir_entry(&asset_dir, &entry.file_name())?
96+
else {
8897
continue;
8998
};
9099

91-
let input = InternedInput::new(old_data.to_str().unwrap(), new_data.to_str().unwrap());
100+
let input = InternedInput::new(
101+
old_data.to_str().expect("BUG: we don't have non-ascii here"),
102+
new_data.to_str().expect("BUG: we don't have non-ascii here"),
103+
);
92104
let algorithm = match algorithm {
93105
gix_diff::blob::Algorithm::Myers => Algorithm::Myers,
94106
gix_diff::blob::Algorithm::Histogram => Algorithm::Histogram,
@@ -124,33 +136,6 @@ fn baseline_v2() -> gix_testtools::Result {
124136
Ok(())
125137
}
126138

127-
fn parse_dir_entry(asset_dir: &Path, file_name: &OsStr) -> Option<(String, Algorithm, Vec<u8>, Vec<u8>)> {
128-
let file_name = file_name.to_str().expect("ascii filename").to_owned();
129-
130-
if !file_name.ends_with(".baseline") {
131-
return None;
132-
}
133-
134-
let parts: Vec<_> = file_name.split('.').collect();
135-
let [name, algorithm, ..] = parts[..] else {
136-
unreachable!("BUG: Need file named '<name>.<algorithm>'")
137-
};
138-
let algorithm = match algorithm {
139-
"myers" => Algorithm::Myers,
140-
"histogram" => Algorithm::Histogram,
141-
other => unreachable!("'{other}' is not a supported algorithm"),
142-
};
143-
144-
let parts: Vec<_> = name.split('-').collect();
145-
let [old_blob_id, new_blob_id] = parts[..] else {
146-
unreachable!("BUG: name part of filename must be <old_blob_id>-<new_blob_id>");
147-
};
148-
149-
let old_data = std::fs::read(asset_dir.join(format!("{old_blob_id}.blob"))).unwrap();
150-
let new_data = std::fs::read(asset_dir.join(format!("{new_blob_id}.blob"))).unwrap();
151-
(file_name, algorithm, old_data, new_data).into()
152-
}
153-
154139
fn assert_diffs(diffs: &[(String, String, bool, String)]) {
155140
let total_diffs = diffs.len();
156141
let matching_diffs = diffs
@@ -181,11 +166,13 @@ fn assert_diffs(diffs: &[(String, String, bool, String)]) {
181166
}
182167

183168
mod baseline {
184-
use gix_object::bstr::{ByteSlice, ByteVec};
185-
use std::iter::Peekable;
186-
187169
use gix_diff::blob::unified_diff::{ConsumeHunk, HunkHeader};
170+
use gix_diff::blob::Algorithm;
188171
use gix_object::bstr::{self, BString};
172+
use gix_object::bstr::{ByteSlice, ByteVec};
173+
use std::ffi::OsStr;
174+
use std::iter::Peekable;
175+
use std::path::Path;
189176

190177
static START_OF_HEADER: &[u8; 4] = b"@@ -";
191178

@@ -251,7 +238,7 @@ mod baseline {
251238
}
252239

253240
impl Baseline<'_> {
254-
/// Fold all [`DiffHunk`]s we produce into a unified_diff string
241+
/// Converts all baseline [`DiffHunk`]s into a single unified diff format string.
255242
pub fn fold_to_unidiff(self) -> BString {
256243
self.fold(BString::default(), |mut acc, diff_hunk| {
257244
acc.push_str(diff_hunk.header.to_string().as_str());
@@ -356,4 +343,45 @@ mod baseline {
356343
.parse::<u32>()
357344
.expect("to be a number")
358345
}
346+
347+
pub struct DirEntry {
348+
pub file_name: String,
349+
pub algorithm: Algorithm,
350+
pub old_data: Vec<u8>,
351+
pub new_data: Vec<u8>,
352+
}
353+
354+
/// Returns `None` if the file isn't a baseline entry.
355+
pub fn parse_dir_entry(asset_dir: &Path, file_name: &OsStr) -> std::io::Result<Option<DirEntry>> {
356+
let file_name = file_name.to_str().expect("ascii filename").to_owned();
357+
358+
if !file_name.ends_with(".baseline") {
359+
return Ok(None);
360+
}
361+
362+
let parts: Vec<_> = file_name.split('.').collect();
363+
let [name, algorithm, ..] = parts[..] else {
364+
unreachable!("BUG: Need file named '<name>.<algorithm>'")
365+
};
366+
let algorithm = match algorithm {
367+
"myers" => Algorithm::Myers,
368+
"histogram" => Algorithm::Histogram,
369+
other => unreachable!("BUG: '{other}' is not a supported algorithm"),
370+
};
371+
372+
let parts: Vec<_> = name.split('-').collect();
373+
let [old_blob_id, new_blob_id] = parts[..] else {
374+
unreachable!("BUG: name part of filename must be '<old_blob_id>-<new_blob_id>'");
375+
};
376+
377+
let old_data = std::fs::read(asset_dir.join(format!("{old_blob_id}.blob")))?;
378+
let new_data = std::fs::read(asset_dir.join(format!("{new_blob_id}.blob")))?;
379+
Ok(DirEntry {
380+
file_name,
381+
algorithm,
382+
old_data,
383+
new_data,
384+
}
385+
.into())
386+
}
359387
}

0 commit comments

Comments
 (0)