fix(parse): Don't skip binary files when explicitly requested

Fixes #35
This commit is contained in:
Ed Page 2019-07-31 21:01:09 -06:00
parent 6142941cfc
commit 750005e971
3 changed files with 10 additions and 8 deletions

View file

@ -15,7 +15,7 @@ fn process_empty(b: &mut test::Bencher) {
let corrections = typos::BuiltIn::new(); let corrections = typos::BuiltIn::new();
let parser = typos::tokens::Parser::new(); let parser = typos::tokens::Parser::new();
let checks = typos::checks::CheckSettings::new().build(&corrections, &parser); 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(); temp.close().unwrap();
} }
@ -29,7 +29,7 @@ fn process_no_tokens(b: &mut test::Bencher) {
let corrections = typos::BuiltIn::new(); let corrections = typos::BuiltIn::new();
let parser = typos::tokens::Parser::new(); let parser = typos::tokens::Parser::new();
let checks = typos::checks::CheckSettings::new().build(&corrections, &parser); 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(); temp.close().unwrap();
} }
@ -43,7 +43,7 @@ fn process_single_token(b: &mut test::Bencher) {
let corrections = typos::BuiltIn::new(); let corrections = typos::BuiltIn::new();
let parser = typos::tokens::Parser::new(); let parser = typos::tokens::Parser::new();
let checks = typos::checks::CheckSettings::new().build(&corrections, &parser); 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(); temp.close().unwrap();
} }
@ -57,7 +57,7 @@ fn process_sherlock(b: &mut test::Bencher) {
let corrections = typos::BuiltIn::new(); let corrections = typos::BuiltIn::new();
let parser = typos::tokens::Parser::new(); let parser = typos::tokens::Parser::new();
let checks = typos::checks::CheckSettings::new().build(&corrections, &parser); 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(); temp.close().unwrap();
} }
@ -71,7 +71,7 @@ fn process_code(b: &mut test::Bencher) {
let corrections = typos::BuiltIn::new(); let corrections = typos::BuiltIn::new();
let parser = typos::tokens::Parser::new(); let parser = typos::tokens::Parser::new();
let checks = typos::checks::CheckSettings::new().build(&corrections, &parser); 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(); temp.close().unwrap();
} }
@ -85,7 +85,7 @@ fn process_corpus(b: &mut test::Bencher) {
let corrections = typos::BuiltIn::new(); let corrections = typos::BuiltIn::new();
let parser = typos::tokens::Parser::new(); let parser = typos::tokens::Parser::new();
let checks = typos::checks::CheckSettings::new().build(&corrections, &parser); 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(); temp.close().unwrap();
} }

View file

@ -111,6 +111,7 @@ impl<'d, 'p> Checks<'d, 'p> {
pub fn check_file( pub fn check_file(
&self, &self,
path: &std::path::Path, path: &std::path::Path,
explicit: bool,
report: report::Report, report: report::Report,
) -> Result<bool, failure::Error> { ) -> Result<bool, failure::Error> {
let mut typos_found = false; let mut typos_found = false;
@ -121,7 +122,7 @@ impl<'d, 'p> Checks<'d, 'p> {
let mut buffer = Vec::new(); let mut buffer = Vec::new();
File::open(path)?.read_to_end(&mut buffer)?; 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 { let msg = report::BinaryFile {
path, path,
non_exhaustive: (), non_exhaustive: (),

View file

@ -290,10 +290,11 @@ fn run() -> Result<i32, failure::Error> {
for entry in walk.build() { for entry in walk.build() {
let entry = entry?; let entry = entry?;
if entry.file_type().map(|t| t.is_file()).unwrap_or(true) { 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())? { if checks.check_filename(entry.path(), options.format.report())? {
typos_found = true; 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; typos_found = true;
} }
} }