Skip to content

Commit dda555a

Browse files
committed
test: add binary patch to compat and replay tests
Now both tests require `binary` Cargo feature.
1 parent e0d0b87 commit dda555a

File tree

25 files changed

+309
-48
lines changed

25 files changed

+309
-48
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ jobs:
3030
- run: rustup toolchain install ${{ matrix.rust }} --profile minimal
3131
- run: cargo +${{ matrix.rust }} check --all-targets --all-features
3232
- run: cargo +${{ matrix.rust }} test
33+
- run: cargo +${{ matrix.rust }} test -F binary
3334

3435
lint:
3536
runs-on: ubuntu-latest

.github/workflows/replay.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ jobs:
6767
exit 1
6868
fi
6969
- run: rustup toolchain install stable --profile minimal
70-
- run: cargo test --release --test replay -- --ignored --nocapture
70+
- run: cargo test --release --test replay -F binary -- --ignored --nocapture
7171
env:
7272
DIFFY_TEST_REPO: ${{ inputs.repo_url == '' && '.' || 'target/test-repo' }}
7373
DIFFY_TEST_COMMITS: ${{ inputs.commits }}

Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,11 @@ snapbox = { version = "0.6.24", features = ["dir"] }
2727
[[example]]
2828
name = "patch_formatter"
2929
required-features = ["color"]
30+
31+
[[test]]
32+
name = "compat"
33+
required-features = ["binary"]
34+
35+
[[test]]
36+
name = "replay"
37+
required-features = ["binary"]

tests/compat/common.rs

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::{
99
};
1010

1111
use diffy::{
12-
binary::BinaryPatch,
12+
binary::{BinaryPatch, BinaryPatchParseError},
1313
patch_set::{FileOperation, ParseOptions, PatchKind, PatchSet, PatchSetParseError},
1414
};
1515

@@ -268,13 +268,15 @@ fn print_patch_version() {
268268
pub enum TestError {
269269
Parse(PatchSetParseError),
270270
Apply(diffy::ApplyError),
271+
Binary(BinaryPatchParseError),
271272
}
272273

273274
impl std::fmt::Display for TestError {
274275
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
275276
match self {
276277
TestError::Parse(e) => write!(f, "parse error: {e}"),
277278
TestError::Apply(e) => write!(f, "apply error: {e}"),
279+
TestError::Binary(e) => write!(f, "binary patch error: {e}"),
278280
}
279281
}
280282
}
@@ -352,31 +354,40 @@ pub fn apply_diffy(
352354
}
353355
};
354356

357+
let read_original = || {
358+
if let Some(name) = original_name {
359+
let original_path = in_dir.join(name);
360+
fs::read(&original_path).unwrap_or_default()
361+
} else {
362+
Vec::new()
363+
}
364+
};
365+
366+
let write_modified = |result: &[u8]| {
367+
let result_path = output_dir.join(target_name);
368+
if let Some(parent) = result_path.parent() {
369+
fs::create_dir_all(parent).unwrap();
370+
}
371+
fs::write(&result_path, result).unwrap();
372+
};
373+
355374
match file_patch.patch() {
356375
PatchKind::Text(patch) => {
357-
let original = if let Some(name) = original_name {
358-
let original_path = in_dir.join(name);
359-
fs::read_to_string(&original_path).unwrap_or_else(|e| {
360-
panic!("failed to read {}: {e}", original_path.display())
361-
})
362-
} else {
363-
String::new()
364-
};
376+
let original = String::from_utf8(read_original()).unwrap();
365377

366378
let result = diffy::apply(&original, patch).map_err(TestError::Apply)?;
367379

368-
let result_path = output_dir.join(target_name);
369-
if let Some(parent) = result_path.parent() {
370-
fs::create_dir_all(parent).unwrap();
371-
}
372-
fs::write(&result_path, result.as_bytes()).unwrap();
380+
write_modified(result.as_bytes());
373381
}
374382
PatchKind::Binary(BinaryPatch::Marker) => {
375-
// Dont do anything if it is just a binary patch marker.[
383+
// Dont do anything if it is just a binary patch marker.
376384
}
377-
PatchKind::Binary(_) => {
378-
// Binary patch application requires the `binary` feature.
379-
// Will be wired up when that feature is added.
385+
PatchKind::Binary(patch) => {
386+
let original = read_original();
387+
388+
let result = patch.apply(&original).map_err(TestError::Binary)?;
389+
390+
write_modified(&result);
380391
}
381392
}
382393
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
diff --git a/image.png b/image.png
2+
index 0dd1608e45a9c4d35bfc1e6f266a796364aa8754..bccac03558b00545e7ea8ced4a3a1ee232cc185a 100644
3+
GIT binary patch
4+
literal 10
5+
UcmV+l0QLWgP)<h;3K|Lk0S%l1s{jB1
6+
7+
literal 8
8+
ScmV+j0QdiiP)<h;3K|LuM**q;
9+
10+
diff --git a/text.txt b/text.txt
11+
index 33194a0a6f3f99e366d606c24d9b1ab0e0086e69..b66ba06d315d46280bb09d54614cc52d1677809f 100644
12+
--- a/text.txt
13+
+++ b/text.txt
14+
@@ -1 +1 @@
15+
-old content
16+
+new content
Lines changed: 2 additions & 0 deletions
Loading
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
old content
10 Bytes
Loading
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
new content
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
diff --git a/large.bin b/large.bin
2+
index ffba9ca51637158e8f46f0a2a9014372778eeef4..a8a025a84f32c7552841b683361d03d3e171294d 100644
3+
GIT binary patch
4+
delta 15
5+
ZcmV+q0Pz2SD1a!CWCZ{J|NpUQm=imI2nhfH
6+
7+
delta 15
8+
ZcmV+q0Pz2SD1a!CWCQ_9%OJ66m=h@q1w#M;
9+

0 commit comments

Comments
 (0)