1
use pretty_assertions::assert_eq;
2
use std::borrow::Cow;
3
use std::env;
4
use std::error::Error;
5
use std::fs;
6
use std::path::PathBuf;
7
use std::process::Command;
8

            
9
21
pub fn custom_test(test_dir: &str) -> Result<(), Box<dyn Error>> {
10
21
    let accept = env::var("ACCEPT").unwrap_or("false".to_string()) == "true";
11

            
12
    // Convert test directory to a PathBuf
13
21
    let test_path = PathBuf::from(test_dir);
14
21
    assert!(
15
21
        test_path.exists(),
16
        "Test directory not found: {test_path:?}"
17
    );
18

            
19
    // Get paths
20
21
    let script_path = test_path.join("run.sh");
21
21
    assert!(
22
21
        script_path.exists(),
23
        "Test script not found: {script_path:?}"
24
    );
25
21
    let expected_output_path = test_path.join("stdout.expected");
26
21
    let expected_error_path = test_path.join("stderr.expected");
27

            
28
    // Execute the test script in the correct directory
29
21
    let output = Command::new("sh")
30
21
        .arg("run.sh")
31
21
        .current_dir(test_path)
32
21
        .output()?;
33

            
34
    // Convert captured output/error to string
35
21
    let actual_output = String::from_utf8_lossy(&output.stdout);
36
21
    let actual_error = String::from_utf8_lossy(&output.stderr);
37

            
38
21
    if accept {
39
        // Overwrite expected files
40
        update_file(expected_output_path, actual_output)?;
41
        update_file(expected_error_path, actual_error)?;
42
    } else {
43
        // Compare results
44
21
        let expected_output = if expected_output_path.exists() {
45
14
            fs::read_to_string(&expected_output_path)?
46
        } else {
47
7
            String::new()
48
        };
49
21
        let expected_error = if expected_error_path.exists() {
50
13
            fs::read_to_string(&expected_error_path)?
51
        } else {
52
8
            String::new()
53
        };
54

            
55
21
        assert_eq!(expected_error, actual_error, "Standard error mismatch");
56
21
        assert_eq!(expected_output, actual_output, "Standard output mismatch");
57
    }
58

            
59
21
    Ok(())
60
21
}
61

            
62
fn update_file(
63
    expected_file_path: PathBuf,
64
    actual_output: Cow<'_, str>,
65
) -> Result<(), Box<dyn Error>> {
66
    if expected_file_path.exists() {
67
        fs::remove_file(&expected_file_path)?;
68
    }
69
    if !actual_output.trim().is_empty() {
70
        fs::File::create(&expected_file_path)?;
71
        fs::write(&expected_file_path, actual_output.as_bytes())?;
72
    }
73
    Ok(())
74
}
75

            
76
#[test]
77
1
fn assert_conjure_present() {
78
1
    conjure_cp_cli::find_conjure::conjure_executable().unwrap();
79
1
}
80

            
81
include!(concat!(env!("OUT_DIR"), "/gen_tests_custom.rs"));