Skip to content

Commit 9dffbaf

Browse files
authored
Merge pull request #22 from nastevens/fix-missing-dirs
Return errors on first entry of WalkDir
2 parents ea5b7fc + 4441956 commit 9dffbaf

File tree

3 files changed

+43
-22
lines changed

3 files changed

+43
-22
lines changed

.travis.yml

+11-11
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,23 @@ rust:
77
- nightly
88
matrix:
99
include:
10-
- env: RUSTFMT
11-
rust: 1.25.0 # `stable`: Locking down for consistent behavior
10+
- name: check rustfmt
11+
rust: 1.38.0 # `stable`: Locking down for consistent behavior
1212
install:
13-
- rustup component add rustfmt-preview
13+
- rustup component add rustfmt
1414
script:
15-
- cargo fmt -- --write-mode=diff
16-
- env: RUSTFLAGS="-D warnings"
17-
rust: 1.25.0 # `stable`: Locking down for consistent behavior
18-
install:
15+
- cargo fmt --all -- --check
16+
- name: check with -D warnings
17+
env: RUSTFLAGS="-D warnings"
18+
rust: 1.38.0 # `stable`: Locking down for consistent behavior
1919
script:
2020
- cargo check --tests --all-features
21-
- env: CLIPPY_VERSION="0.0.179"
22-
rust: nightly-2018-01-12
21+
- name: check clippy
22+
rust: 1.38.0 # `stable`: Locking down for consistent behavior
2323
install:
24-
- travis_wait cargo install clippy --version $CLIPPY_VERSION || echo "clippy already installed"
24+
- rustup component add clippy
2525
script:
26-
- cargo clippy --all-features -- -D clippy
26+
- cargo clippy --all --all-features -- -D warnings
2727

2828
install:
2929
- rustc -Vv

src/lib.rs

+13-10
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
1414
extern crate walkdir;
1515

16+
use std::cmp::Ordering;
1617
use std::fs::File;
1718
use std::io::prelude::*;
1819
use std::path::Path;
19-
use std::cmp::Ordering;
2020

2121
use walkdir::{DirEntry, WalkDir};
2222

@@ -38,29 +38,32 @@ pub enum Error {
3838
/// assert!(dir_diff::is_different("dir/a", "dir/b").unwrap());
3939
/// ```
4040
pub fn is_different<A: AsRef<Path>, B: AsRef<Path>>(a_base: A, b_base: B) -> Result<bool, Error> {
41-
let mut a_walker = walk_dir(a_base);
42-
let mut b_walker = walk_dir(b_base);
41+
let mut a_walker = walk_dir(a_base)?;
42+
let mut b_walker = walk_dir(b_base)?;
4343

4444
for (a, b) in (&mut a_walker).zip(&mut b_walker) {
4545
let a = a?;
4646
let b = b?;
4747

48-
if a.depth() != b.depth() || a.file_type() != b.file_type()
48+
if a.depth() != b.depth()
49+
|| a.file_type() != b.file_type()
4950
|| a.file_name() != b.file_name()
5051
|| (a.file_type().is_file() && read_to_vec(a.path())? != read_to_vec(b.path())?)
5152
{
5253
return Ok(true);
5354
}
5455
}
5556

56-
Ok(!a_walker.next().is_none() || !b_walker.next().is_none())
57+
Ok(a_walker.next().is_some() || b_walker.next().is_some())
5758
}
5859

59-
fn walk_dir<P: AsRef<Path>>(path: P) -> std::iter::Skip<walkdir::IntoIter> {
60-
WalkDir::new(path)
61-
.sort_by(compare_by_file_name)
62-
.into_iter()
63-
.skip(1)
60+
fn walk_dir<P: AsRef<Path>>(path: P) -> Result<walkdir::IntoIter, std::io::Error> {
61+
let mut walkdir = WalkDir::new(path).sort_by(compare_by_file_name).into_iter();
62+
if let Some(Err(e)) = walkdir.next() {
63+
Err(e.into())
64+
} else {
65+
Ok(walkdir)
66+
}
6467
}
6568

6669
fn compare_by_file_name(a: &DirEntry, b: &DirEntry) -> Ordering {

tests/smoke.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
extern crate dir_diff;
22

3-
use std::path::Path;
43
use std::fs::create_dir;
54
use std::io::ErrorKind;
5+
use std::path::Path;
66

77
#[test]
88
fn easy_good() {
@@ -34,6 +34,24 @@ fn oneempty() {
3434
assert!(dir_diff::is_different("tests/oneempty/dir1", "tests/oneempty/dir2").unwrap());
3535
}
3636

37+
#[test]
38+
#[should_panic]
39+
fn firstmissing() {
40+
assert!(dir_diff::is_different("does_not_exist", "tests/easy/good/dir1").unwrap());
41+
}
42+
43+
#[test]
44+
#[should_panic]
45+
fn secondmissing() {
46+
assert!(dir_diff::is_different("tests/easy/good/dir1", "does_not_exist").unwrap());
47+
}
48+
49+
#[test]
50+
#[should_panic]
51+
fn bothmissing() {
52+
assert!(dir_diff::is_different("does_not_exist", "also_does_not_exist").unwrap());
53+
}
54+
3755
#[test]
3856
fn reflexive() {
3957
assert!(dir_diff::is_different("tests/reflexive/dir1", "tests/reflexive/dir2").unwrap());

0 commit comments

Comments
 (0)