1
//! Runs the Emacs client's ERT suites in batch mode; a missing `emacs` fails.
2

            
3
use std::path::{Path, PathBuf};
4
use std::process::Command;
5

            
6
2
fn repo_root() -> PathBuf {
7
2
    Path::new(env!("CARGO_MANIFEST_DIR")).join("..")
8
2
}
9

            
10
1
fn test_files(emacs_dir: &Path) -> anyhow::Result<Vec<PathBuf>> {
11
1
    let mut files = Vec::new();
12
1
    for entry in std::fs::read_dir(emacs_dir)? {
13
79
        let path = entry?.path();
14
79
        let is_test = path
15
79
            .file_name()
16
79
            .and_then(|name| name.to_str())
17
79
            .is_some_and(|name| name.ends_with("-tests.el"));
18
79
        if is_test {
19
38
            files.push(path);
20
41
        }
21
    }
22
1
    files.sort();
23
1
    Ok(files)
24
1
}
25

            
26
#[test]
27
1
fn emacs_ert_suites_pass() -> anyhow::Result<()> {
28
1
    let root = repo_root();
29
1
    let files = test_files(&root.join("emacs"))?;
30
1
    anyhow::ensure!(!files.is_empty(), "no emacs/*-tests.el files found");
31
1
    let mut command = Command::new("emacs");
32
1
    command
33
1
        .current_dir(&root)
34
1
        .args(["-Q", "-batch", "-L", "emacs", "-l", "ert"])
35
1
        .args(["--eval", "(setq load-prefer-newer t)"]);
36
1
    for file in &files {
37
38
        command.arg("-l").arg(file);
38
    }
39
1
    command.args(["-f", "ert-run-tests-batch-and-exit"]);
40
1
    let output = command
41
1
        .output()
42
        .map_err(|e| anyhow::anyhow!("cannot run emacs (it must be installed): {e}"))?;
43
1
    anyhow::ensure!(
44
1
        output.status.success(),
45
        "ERT failed ({}):\n{}\n{}",
46
        output.status,
47
        String::from_utf8_lossy(&output.stdout),
48
        String::from_utf8_lossy(&output.stderr)
49
    );
50
1
    let report = String::from_utf8_lossy(&output.stderr);
51
1
    anyhow::ensure!(ran_tests(&report) > 0, "ERT ran no tests:\n{report}");
52
1
    Ok(())
53
1
}
54

            
55
1
fn modules(emacs_dir: &Path) -> anyhow::Result<Vec<String>> {
56
1
    let mut names = Vec::new();
57
1
    for entry in std::fs::read_dir(emacs_dir)? {
58
79
        let path = entry?.path();
59
79
        let module = path
60
79
            .file_name()
61
79
            .and_then(|name| name.to_str())
62
79
            .filter(|name| name.ends_with(".el") && !name.ends_with("-tests.el"))
63
41
            .and_then(|name| name.strip_suffix(".el"))
64
41
            .filter(|name| !name.ends_with("-fixtures"));
65
79
        if let Some(module) = module {
66
37
            names.push(module.to_string());
67
42
        }
68
    }
69
1
    names.sort();
70
1
    Ok(names)
71
1
}
72

            
73
/// Every module loads alone in a fresh Emacs, so no require cycle can hide
74
/// behind the order the suites happen to load them in.
75
#[test]
76
1
fn every_emacs_module_loads_alone() -> anyhow::Result<()> {
77
1
    let root = repo_root();
78
1
    let names = modules(&root.join("emacs"))?;
79
1
    anyhow::ensure!(!names.is_empty(), "no emacs modules found");
80
1
    for name in &names {
81
37
        let output = Command::new("emacs")
82
37
            .current_dir(&root)
83
37
            .args(["-Q", "-batch", "-L", "emacs", "--eval"])
84
37
            .arg(format!("(require '{name})"))
85
37
            .output()
86
            .map_err(|e| anyhow::anyhow!("cannot run emacs (it must be installed): {e}"))?;
87
37
        anyhow::ensure!(
88
37
            output.status.success(),
89
            "(require '{name}) failed:\n{}",
90
            String::from_utf8_lossy(&output.stderr)
91
        );
92
    }
93
1
    Ok(())
94
1
}
95

            
96
/// The N of ERT's "Ran N tests" summary line, or 0 when it is missing.
97
1
fn ran_tests(report: &str) -> usize {
98
1
    report
99
1
        .lines()
100
616
        .find_map(|line| {
101
616
            line.strip_prefix("Ran ")?
102
                .split_whitespace()
103
                .next()?
104
                .parse()
105
                .ok()
106
616
        })
107
1
        .unwrap_or(0)
108
1
}