#![allow(clippy::pedantic)]
use std::path::{Path, PathBuf};
use std::process::Command;
fn temp_workspace(prefix: &str) -> tempfile::TempDir {
tempfile::Builder::new()
.prefix(prefix)
.tempdir()
.expect("a temp workspace")
}
fn write(dir: &Path, name: &str, contents: &str) {
let path = dir.join(name);
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent).unwrap();
}
std::fs::write(path, contents).unwrap();
}
#[test]
fn checking_a_directory_visits_every_matching_file() {
let workspace = temp_workspace("lang_check_cli_dir");
write(workspace.path(), "top.md", "This sentance is wrong.\n");
write(
workspace.path(),
"nested/deep.markdown",
"Another mispeling here.\n",
);
write(
workspace.path(),
"ignored.tree",
"\\p{A third mispeling.}\n",
);
let output = Command::new(env!("CARGO_BIN_EXE_language-check"))
.current_dir(workspace.path())
.args(["check", ".", "--lang", "markdown", "--format", "json"])
.output()
.expect("the CLI runs");
assert!(output.status.success(), "{output:?}");
let diagnostics: Vec<serde_json::Value> =
serde_json::from_slice(&output.stdout).expect("valid JSON array");
let files: std::collections::HashSet<&str> = diagnostics
.iter()
.filter_map(|d| d["file"].as_str())
.collect();
assert!(
files.iter().any(|f| f.ends_with("top.md")),
"the .md file was not checked: {files:?}"
);
assert!(
files.iter().any(|f| f.ends_with("deep.markdown")),
"the nested .markdown file was not checked: {files:?}"
);
assert!(
!files.iter().any(|f| f.ends_with("ignored.tree")),
"a file of another language was checked: {files:?}"
);
}
#[test]
fn checking_a_directory_visits_every_format_not_just_one() {
let workspace = temp_workspace("lang_check_cli_formats");
write(workspace.path(), "notes.md", "A mispeling in Markdown.\n");
write(
workspace.path(),
"page.html",
"<h1>Title</h1>\n<p>A mispeling in HTML.</p>\n",
);
let output = Command::new(env!("CARGO_BIN_EXE_language-check"))
.current_dir(workspace.path())
.args(["check", ".", "--format", "json"])
.output()
.expect("the CLI runs");
assert!(output.status.success(), "{output:?}");
let diagnostics: Vec<serde_json::Value> =
serde_json::from_slice(&output.stdout).expect("valid JSON array");
let files: std::collections::BTreeSet<String> = diagnostics
.iter()
.filter_map(|d| d["file"].as_str().map(str::to_string))
.collect();
assert!(
files.iter().any(|f| f.ends_with("notes.md")),
"the Markdown file was not checked: {files:?}"
);
assert!(
files.iter().any(|f| f.ends_with("page.html")),
"the HTML file was not checked: {files:?}"
);
}
#[test]
fn an_explicit_lang_still_applies_to_every_file_in_a_directory() {
let workspace = temp_workspace("lang_check_cli_explicit");
write(workspace.path(), "a.md", "A mispeling here.\n");
write(workspace.path(), "b.md", "Another mispeling.\n");
let output = Command::new(env!("CARGO_BIN_EXE_language-check"))
.current_dir(workspace.path())
.args(["check", ".", "--lang", "markdown", "--format", "json"])
.output()
.expect("the CLI runs");
assert!(output.status.success(), "{output:?}");
let diagnostics: Vec<serde_json::Value> =
serde_json::from_slice(&output.stdout).expect("valid JSON array");
assert!(diagnostics.len() >= 2, "{diagnostics:?}");
}