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

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

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

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

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

            
38
51
    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
51
        let expected_output = if expected_output_path.exists() {
45
40
            fs::read_to_string(&expected_output_path)?
46
        } else {
47
11
            String::new()
48
        };
49
51
        let expected_error = if expected_error_path.exists() {
50
27
            fs::read_to_string(&expected_error_path)?
51
        } else {
52
24
            String::new()
53
        };
54

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

            
59
51
    Ok(())
60
51
}
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
3
fn assert_conjure_present() {
78
3
    conjure_cp_cli::find_conjure::conjure_executable().unwrap();
79
3
}
80

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