Skip to content

Commit e706f8c

Browse files
committed
allow run spellchecker via test tidy --extra-checks=spellcheck
1 parent 1687137 commit e706f8c

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/tools/tidy/src/ext_tool_checks.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ fn check_impl(
7272
let shell_lint = lint_args.contains(&"shell:lint") || shell_all;
7373
let cpp_all = lint_args.contains(&"cpp");
7474
let cpp_fmt = lint_args.contains(&"cpp:fmt") || cpp_all;
75+
let spellcheck_all = lint_args.contains(&"spellcheck");
76+
let spellcheck_fix = lint_args.contains(&"spellcheck:fix");
7577

7678
let mut py_path = None;
7779

@@ -224,6 +226,22 @@ fn check_impl(
224226
shellcheck_runner(&merge_args(&cfg_args, &file_args_shc))?;
225227
}
226228

229+
if spellcheck_all || spellcheck_fix {
230+
let config_path = root_path.join("typos.toml");
231+
let mut args =
232+
// sync target files with .github/workflows/ci.yml
233+
vec!["-c", config_path.as_os_str().to_str().unwrap(), "./compiler", "./library"];
234+
235+
if spellcheck_all {
236+
eprintln!("spellcheck files");
237+
spellcheck_runner(&args)?;
238+
} else if spellcheck_fix {
239+
eprintln!("spellcheck files and fix");
240+
args.push("--write-changes");
241+
spellcheck_runner(&args)?;
242+
}
243+
}
244+
227245
Ok(())
228246
}
229247

@@ -491,6 +509,25 @@ fn shellcheck_runner(args: &[&OsStr]) -> Result<(), Error> {
491509
if status.success() { Ok(()) } else { Err(Error::FailedCheck("shellcheck")) }
492510
}
493511

512+
/// Check that spellchecker is installed then run it at the given path
513+
fn spellcheck_runner(args: &[&str]) -> Result<(), Error> {
514+
match Command::new("typos").arg("--version").status() {
515+
Ok(_) => (),
516+
Err(e) if e.kind() == io::ErrorKind::NotFound => {
517+
return Err(Error::MissingReq(
518+
"typos",
519+
"spellcheck file checks",
520+
// sync version with .github/workflows/ci.yml
521+
Some("install tool via `cargo install typos-cli@1.28.2`".to_owned()),
522+
));
523+
}
524+
Err(e) => return Err(e.into()),
525+
}
526+
527+
let status = Command::new("typos").args(args).status()?;
528+
if status.success() { Ok(()) } else { Err(Error::FailedCheck("typos")) }
529+
}
530+
494531
/// Check git for tracked files matching an extension
495532
fn find_with_extension(
496533
root_path: &Path,

0 commit comments

Comments
 (0)