Lines
90.36 %
Functions
86.67 %
Branches
100 %
//! Runs the Emacs client's ERT suites in batch mode; a missing `emacs` fails.
use std::path::{Path, PathBuf};
use std::process::Command;
fn repo_root() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR")).join("..")
}
fn test_files(emacs_dir: &Path) -> anyhow::Result<Vec<PathBuf>> {
let mut files = Vec::new();
for entry in std::fs::read_dir(emacs_dir)? {
let path = entry?.path();
let is_test = path
.file_name()
.and_then(|name| name.to_str())
.is_some_and(|name| name.ends_with("-tests.el"));
if is_test {
files.push(path);
files.sort();
Ok(files)
#[test]
fn emacs_ert_suites_pass() -> anyhow::Result<()> {
let root = repo_root();
let files = test_files(&root.join("emacs"))?;
anyhow::ensure!(!files.is_empty(), "no emacs/*-tests.el files found");
let mut command = Command::new("emacs");
command
.current_dir(&root)
.args(["-Q", "-batch", "-L", "emacs", "-l", "ert"])
.args(["--eval", "(setq load-prefer-newer t)"]);
for file in &files {
command.arg("-l").arg(file);
command.args(["-f", "ert-run-tests-batch-and-exit"]);
let output = command
.output()
.map_err(|e| anyhow::anyhow!("cannot run emacs (it must be installed): {e}"))?;
anyhow::ensure!(
output.status.success(),
"ERT failed ({}):\n{}\n{}",
output.status,
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);
let report = String::from_utf8_lossy(&output.stderr);
anyhow::ensure!(ran_tests(&report) > 0, "ERT ran no tests:\n{report}");
Ok(())
fn modules(emacs_dir: &Path) -> anyhow::Result<Vec<String>> {
let mut names = Vec::new();
let module = path
.filter(|name| name.ends_with(".el") && !name.ends_with("-tests.el"))
.and_then(|name| name.strip_suffix(".el"))
.filter(|name| !name.ends_with("-fixtures"));
if let Some(module) = module {
names.push(module.to_string());
names.sort();
Ok(names)
/// Every module loads alone in a fresh Emacs, so no require cycle can hide
/// behind the order the suites happen to load them in.
fn every_emacs_module_loads_alone() -> anyhow::Result<()> {
let names = modules(&root.join("emacs"))?;
anyhow::ensure!(!names.is_empty(), "no emacs modules found");
for name in &names {
let output = Command::new("emacs")
.args(["-Q", "-batch", "-L", "emacs", "--eval"])
.arg(format!("(require '{name})"))
"(require '{name}) failed:\n{}",
/// The N of ERT's "Ran N tests" summary line, or 0 when it is missing.
fn ran_tests(report: &str) -> usize {
report
.lines()
.find_map(|line| {
line.strip_prefix("Ran ")?
.split_whitespace()
.next()?
.parse()
.ok()
})
.unwrap_or(0)