1
use std::env;
2
use std::error::Error;
3
use std::fs::create_dir_all;
4
use std::fs::remove_dir_all;
5
use std::path::Path;
6

            
7
use duct::cmd;
8
use which::which_in;
9

            
10
/// Runs a smoke test of the packaged tool binaries to ensure that basic functionality works.
11
///
12
/// `directory` is expected to be a release package directory laid out as produced by
13
/// `xtask package`, with an `examples/` directory as a sibling (i.e. `<directory>/../examples`).
14
pub(crate) fn test_tools(directory: &Path) -> Result<(), Box<dyn Error>> {
15
    // Create a unique temporary directory to perform the tests in, scoped to this process so
16
    // concurrent or repeated runs do not clash, and clean it up afterwards.
17
    let tmp_path = env::temp_dir().join(format!("merc-test-tools-{}", std::process::id()));
18
    create_dir_all(&tmp_path)?;
19

            
20
    let result = run_tool_tests(directory, &tmp_path);
21

            
22
    // Best-effort cleanup; do not mask the test result with a cleanup error.
23
    let _ = remove_dir_all(&tmp_path);
24

            
25
    result
26
}
27

            
28
fn run_tool_tests(directory: &Path, tmp_path: &Path) -> Result<(), Box<dyn Error>> {
29
    // Find the binaries
30
    let merc_lts = which_in("merc-lts", Some(directory), env::current_dir()?)?;
31

            
32
    // Copy some test files to the temporary directory.
33
    std::fs::copy(directory.join("../examples/lts/abp.aut"), tmp_path.join("abp.aut"))?;
34

            
35
    for algorithm in ["strong-bisim", "branching-bisim", "weak-bisim"] {
36
        cmd!(
37
            &merc_lts,
38
            "reduce",
39
            algorithm,
40
            "abp.aut",
41
            "--output",
42
            format!("abp.{algorithm}.aut")
43
        )
44
        .dir(tmp_path)
45
        .run()?;
46
    }
47

            
48
    Ok(())
49
}