Skip to content

Commit a51187d

Browse files
committed
allow run spellchecker via test tidy --extra-checks=spellcheck
1 parent 7e37d9f commit a51187d

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

@@ -215,6 +217,22 @@ fn check_impl(
215217
shellcheck_runner(&merge_args(&cfg_args, &file_args_shc))?;
216218
}
217219

220+
if spellcheck_all || spellcheck_fix {
221+
let config_path = root_path.join("typos.toml");
222+
let mut args =
223+
// sync target files with .github/workflows/ci.yml
224+
vec!["-c", config_path.as_os_str().to_str().unwrap(), "./compiler", "./library"];
225+
226+
if spellcheck_all {
227+
eprintln!("spellcheck files");
228+
spellcheck_runner(&args)?;
229+
} else if spellcheck_fix {
230+
eprintln!("spellcheck files and fix");
231+
args.push("--write-changes");
232+
spellcheck_runner(&args)?;
233+
}
234+
}
235+
218236
Ok(())
219237
}
220238

@@ -482,6 +500,25 @@ fn shellcheck_runner(args: &[&OsStr]) -> Result<(), Error> {
482500
if status.success() { Ok(()) } else { Err(Error::FailedCheck("shellcheck")) }
483501
}
484502

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

0 commit comments

Comments
 (0)