#![allow(clippy::pedantic)]
use std::path::PathBuf;
use std::process::Command;
fn temp_workspace(prefix: &str) -> PathBuf {
let dir = std::env::temp_dir().join(format!(
"{prefix}_{}_{}",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos()
));
std::fs::create_dir_all(&dir).unwrap();
dir
}
fn severities(config: &str) -> Vec<String> {
let workspace = temp_workspace("lang_check_json_severity");
std::fs::write(workspace.join(".languagecheck.yaml"), config).unwrap();
std::fs::write(workspace.join("doc.md"), "A recieve typo.\n").unwrap();
let output = Command::new(env!("CARGO_BIN_EXE_language-check"))
.current_dir(&workspace)
.args(["check", "doc.md", "--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 found = diagnostics
.iter()
.filter_map(|d| d["severity"].as_str().map(str::to_string))
.collect();
std::fs::remove_dir_all(&workspace).ok();
found
}
#[test]
fn a_rule_set_to_error_is_reported_as_error() {
let reported =
severities("engines:\n harper: true\nrules:\n spelling.typo:\n severity: error\n");
assert_eq!(
reported,
vec!["error"],
"the config asked for error and the JSON said otherwise"
);
}
#[test]
fn a_rule_set_to_info_is_reported_as_information() {
let reported =
severities("engines:\n harper: true\nrules:\n spelling.typo:\n severity: info\n");
assert_eq!(reported, vec!["information"]);
}
#[test]
fn a_rule_left_alone_keeps_its_category_default() {
let reported = severities("engines:\n harper: true\n");
assert_eq!(reported, vec!["warning"]);
}