1
use std::error::Error;
2

            
3
pub use duct::cmd;
4

            
5
#[allow(clippy::ptr_arg)]
6
fn add_target_flag(_arguments: &mut Vec<String>) {
7
    #[cfg(target_os = "linux")]
8
    {
9
        _arguments.push("--target".to_string());
10
        _arguments.push("x86_64-unknown-linux-gnu".to_string());
11
    }
12

            
13
    #[cfg(target_os = "macos")]
14
    {
15
        _arguments.push("--target".to_string());
16
        _arguments.push("x86_64-apple-darwin".to_string());
17
    }
18
}
19

            
20
///
21
/// Run the tests with the address sanitizer enabled to detect memory issues in unsafe and C++ code.
22
///
23
/// This only works under Linux and MacOS currently and requires the nightly toolchain.
24
///
25
pub fn address_sanitizer(cargo_arguments: Vec<String>) -> Result<(), Box<dyn Error>> {
26
    let mut arguments: Vec<String> = vec![
27
        "nextest".to_string(),
28
        "run".to_string(),
29
        "-Zbuild-std".to_string(),
30
        "--no-fail-fast".to_string(),
31
        "--features".to_string(),
32
        "unstable".to_string(),
33
    ];
34

            
35
    add_target_flag(&mut arguments);
36
    arguments.extend(cargo_arguments);
37

            
38
    cmd("cargo", arguments)
39
        .env("RUSTFLAGS", "-Zsanitizer=address")
40
        .env("CFLAGS", "-fsanitize=address")
41
        .env("CXXFLAGS", "-fsanitize=address")
42
        .run()?;
43
    println!("ok.");
44

            
45
    Ok(())
46
}
47

            
48
///
49
/// Run the tests with the thread sanitizer enabled to detect data race conditions.
50
///
51
/// This only works under Linux and MacOS currently and requires the nightly toolchain.
52
///
53
pub fn thread_sanitizer(cargo_arguments: Vec<String>) -> Result<(), Box<dyn Error>> {
54
    let mut arguments: Vec<String> = vec![
55
        "nextest".to_string(),
56
        "run".to_string(),
57
        "-Zbuild-std".to_string(),
58
        "--no-fail-fast".to_string(),
59
        "--features".to_string(),
60
        "unstable".to_string(),
61
    ];
62

            
63
    add_target_flag(&mut arguments);
64
    arguments.extend(cargo_arguments);
65

            
66
    cmd("cargo", arguments)
67
        .env("RUSTFLAGS", "-Zsanitizer=thread")
68
        .env("CFLAGS", "-fsanitize=thread")
69
        .env("CXXFLAGS", "-fsanitize=thread")
70
        .run()?;
71
    println!("ok.");
72

            
73
    Ok(())
74
}