Skip to content

Commit f3f9b89

Browse files
committed
Compare an empty directory properly.
At the same time, compare a directory with stuff in it to a directory with stuff missing correctly. Fixes #1.
1 parent bb729a6 commit f3f9b89

File tree

4 files changed

+16
-1
lines changed

4 files changed

+16
-1
lines changed

src/lib.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ pub fn is_different<A: AsRef<Path>, B: AsRef<Path>>(a_base: A, b_base: B) -> Res
5050
// and then join that with b to get the path in b
5151
let b = b_base.join(no_prefix);
5252

53+
println!("comparing {} and {}", a.display(), b.display());
54+
5355
if a.is_dir() {
5456
if b.is_dir() {
5557
// can't compare the contents of directories, so just continue
@@ -60,8 +62,15 @@ pub fn is_different<A: AsRef<Path>, B: AsRef<Path>>(a_base: A, b_base: B) -> Res
6062
}
6163
}
6264

65+
// file a is guaranteed to exist...
6366
let a_text = read_to_vec(a)?;
64-
let b_text = read_to_vec(b)?;
67+
68+
// but file b is not. If we have any kind of error when loading
69+
// it up, that's a positive result, not an actual error.
70+
let b_text = match read_to_vec(b) {
71+
Ok(contents) => contents,
72+
Err(_) => return Ok(true),
73+
};
6574

6675
if a_text != b_text {
6776
return Ok(true);

tests/oneempty/dir1/.gitkeep

Whitespace-only changes.

tests/oneempty/dir2/test.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
testing testing

tests/smoke.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,9 @@ fn binary_bad() {
2323
#[test]
2424
fn fileanddir() {
2525
assert!(dir_diff::is_different("tests/fileanddir/dir1", "tests/fileanddir/dir2").unwrap());
26+
}
27+
28+
#[test]
29+
fn oneempty() {
30+
assert!(dir_diff::is_different("tests/oneempty/dir1", "tests/oneempty/dir2").unwrap());
2631
}

0 commit comments

Comments
 (0)