Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle failing to read code file more gracefully #36

Merged
merged 2 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Where `[PATHS]...` is a list of files or directory to check.
### Example usage

```console
$ unicop example-files/homoglyph.js example-files/invisible.js
$ unicop example-files/homoglyph.js example-files/invisible.js example-files/not-utf-8-file.ts
? failed
× found disallowed character LATIN LETTER RETROFLEX CLICK in identifier
╭─[example-files/homoglyph.js:4:18]
Expand All @@ -71,7 +71,10 @@ $ unicop example-files/homoglyph.js example-files/invisible.js
· ╰── HANGUL JUNGSEONG FILLER
6 │ ];
╰────
Scanned 486 unicode code points in 2 files, resulting in 3 rule violations
Error while scanning example-files/not-utf-8-file.ts: Failed to read file (stream did not contain valid UTF-8)
Scanned 486 unicode code points in 2 files, resulting in:
3 rule violations
1 other error
faern marked this conversation as resolved.
Show resolved Hide resolved

```

Expand Down
Binary file added example-files/not-utf-8-file.ts
Binary file not shown.
67 changes: 54 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::HashMap;
use std::fmt;
use std::fs;
use std::io;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -170,6 +171,7 @@ fn main() -> anyhow::Result<()> {
};

let mut num_files_scanned: u64 = 0;
let mut num_errors: u64 = 0;
let mut global_scan_stats = ScanStats {
num_unicode_code_points: 0,
num_rule_violations: 0,
Expand All @@ -181,11 +183,18 @@ fn main() -> anyhow::Result<()> {
Ok(entry) if entry.file_type().is_file() => {
let entry_path = entry.path();
dispatcher.user_config = get_user_config(entry_path)?;
if let Some(scan_stats) = check_file(&dispatcher, entry_path) {
num_files_scanned += 1;
global_scan_stats.num_unicode_code_points +=
scan_stats.num_unicode_code_points;
global_scan_stats.num_rule_violations += scan_stats.num_rule_violations;
match check_file(&dispatcher, entry_path) {
Ok(Some(scan_stats)) => {
num_files_scanned += 1;
global_scan_stats.num_unicode_code_points +=
scan_stats.num_unicode_code_points;
global_scan_stats.num_rule_violations += scan_stats.num_rule_violations;
}
Ok(None) => (),
Err(e) => {
num_errors += 1;
eprintln!("Error while scanning {}: {e}", entry_path.display());
}
}
}
Ok(_) => {}
Expand All @@ -194,25 +203,57 @@ fn main() -> anyhow::Result<()> {
}

println!(
"Scanned {} unicode code points in {} files, resulting in {} rule violations",
global_scan_stats.num_unicode_code_points,
num_files_scanned,
"Scanned {} unicode code points in {} files, resulting in:",
global_scan_stats.num_unicode_code_points, num_files_scanned,
);
println!(
"\t{} rule violations",
global_scan_stats.num_rule_violations,
);
if global_scan_stats.num_rule_violations > 0 {
match num_errors {
1 => println!("\t1 other error"),
_ => println!("\t{num_errors} other errors"),
}
if global_scan_stats.num_rule_violations > 0 || num_errors > 0 {
std::process::exit(1);
}
Ok(())
}

#[derive(Debug)]
enum ScanError {
/// Failed to read the source code file
ReadFile(io::Error),
}

impl fmt::Display for ScanError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use ScanError::*;
match self {
ReadFile(ref e) => write!(f, "Failed to read file ({e})"),
}
}
}

impl std::error::Error for ScanError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use ScanError::*;
match &self {
ReadFile(e) => e.source(),
}
}
}

/// Scans a single file at `path` using the rules defined in `dispatcher`.
///
/// If the file was actually scanned (matched a language in the rule dispatcher),
/// then stats about the scan are returned.
fn check_file(dispatcher: &RuleDispatcher, path: &Path) -> Option<ScanStats> {
let lang = dispatcher.language(path)?;
fn check_file(dispatcher: &RuleDispatcher, path: &Path) -> Result<Option<ScanStats>, ScanError> {
let Some(lang) = dispatcher.language(path) else {
return Ok(None);
};
let filename = path.display().to_string();
let src = fs::read_to_string(path).unwrap();
let src = fs::read_to_string(path).map_err(ScanError::ReadFile)?;
let named_source = NamedSource::new(&filename, src.clone());
let mut parser = tree_sitter::Parser::new();
parser
Expand Down Expand Up @@ -275,7 +316,7 @@ fn check_file(dispatcher: &RuleDispatcher, path: &Path) -> Option<ScanStats> {
print!("{:?}", report);
}

Some(scan_stats)
Ok(Some(scan_stats))
}

/// Statistics about unicop scans.
Expand Down