From 750005e9716534db2742ae4bd3c04cdca8d34ade Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 31 Jul 2019 21:01:09 -0600 Subject: [PATCH] fix(parse): Don't skip binary files when explicitly requested Fixes #35 --- benches/file.rs | 12 ++++++------ src/checks.rs | 3 ++- src/main.rs | 3 ++- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/benches/file.rs b/benches/file.rs index 0783e86..27a7c31 100644 --- a/benches/file.rs +++ b/benches/file.rs @@ -15,7 +15,7 @@ fn process_empty(b: &mut test::Bencher) { let corrections = typos::BuiltIn::new(); let parser = typos::tokens::Parser::new(); let checks = typos::checks::CheckSettings::new().build(&corrections, &parser); - b.iter(|| checks.check_file(sample_path.path(), typos::report::print_silent)); + b.iter(|| checks.check_file(sample_path.path(), true, typos::report::print_silent)); temp.close().unwrap(); } @@ -29,7 +29,7 @@ fn process_no_tokens(b: &mut test::Bencher) { let corrections = typos::BuiltIn::new(); let parser = typos::tokens::Parser::new(); let checks = typos::checks::CheckSettings::new().build(&corrections, &parser); - b.iter(|| checks.check_file(sample_path.path(), typos::report::print_silent)); + b.iter(|| checks.check_file(sample_path.path(), true, typos::report::print_silent)); temp.close().unwrap(); } @@ -43,7 +43,7 @@ fn process_single_token(b: &mut test::Bencher) { let corrections = typos::BuiltIn::new(); let parser = typos::tokens::Parser::new(); let checks = typos::checks::CheckSettings::new().build(&corrections, &parser); - b.iter(|| checks.check_file(sample_path.path(), typos::report::print_silent)); + b.iter(|| checks.check_file(sample_path.path(), true, typos::report::print_silent)); temp.close().unwrap(); } @@ -57,7 +57,7 @@ fn process_sherlock(b: &mut test::Bencher) { let corrections = typos::BuiltIn::new(); let parser = typos::tokens::Parser::new(); let checks = typos::checks::CheckSettings::new().build(&corrections, &parser); - b.iter(|| checks.check_file(sample_path.path(), typos::report::print_silent)); + b.iter(|| checks.check_file(sample_path.path(), true, typos::report::print_silent)); temp.close().unwrap(); } @@ -71,7 +71,7 @@ fn process_code(b: &mut test::Bencher) { let corrections = typos::BuiltIn::new(); let parser = typos::tokens::Parser::new(); let checks = typos::checks::CheckSettings::new().build(&corrections, &parser); - b.iter(|| checks.check_file(sample_path.path(), typos::report::print_silent)); + b.iter(|| checks.check_file(sample_path.path(), true, typos::report::print_silent)); temp.close().unwrap(); } @@ -85,7 +85,7 @@ fn process_corpus(b: &mut test::Bencher) { let corrections = typos::BuiltIn::new(); let parser = typos::tokens::Parser::new(); let checks = typos::checks::CheckSettings::new().build(&corrections, &parser); - b.iter(|| checks.check_file(sample_path.path(), typos::report::print_silent)); + b.iter(|| checks.check_file(sample_path.path(), true, typos::report::print_silent)); temp.close().unwrap(); } diff --git a/src/checks.rs b/src/checks.rs index 308d724..4c8b475 100644 --- a/src/checks.rs +++ b/src/checks.rs @@ -111,6 +111,7 @@ impl<'d, 'p> Checks<'d, 'p> { pub fn check_file( &self, path: &std::path::Path, + explicit: bool, report: report::Report, ) -> Result { let mut typos_found = false; @@ -121,7 +122,7 @@ impl<'d, 'p> Checks<'d, 'p> { let mut buffer = Vec::new(); File::open(path)?.read_to_end(&mut buffer)?; - if !self.binary && buffer.find_byte(b'\0').is_some() { + if !explicit && !self.binary && buffer.find_byte(b'\0').is_some() { let msg = report::BinaryFile { path, non_exhaustive: (), diff --git a/src/main.rs b/src/main.rs index 8673f7e..456a054 100644 --- a/src/main.rs +++ b/src/main.rs @@ -290,10 +290,11 @@ fn run() -> Result { for entry in walk.build() { let entry = entry?; if entry.file_type().map(|t| t.is_file()).unwrap_or(true) { + let explicit = entry.depth() == 0; if checks.check_filename(entry.path(), options.format.report())? { typos_found = true; } - if checks.check_file(entry.path(), options.format.report())? { + if checks.check_file(entry.path(), explicit, options.format.report())? { typos_found = true; } }