about summary refs log tree commit diff
path: root/src/bootstrap/src/utils/helpers/tests.rs
blob: 676fe6cbd5fe59cb990404fd5609de64b459cad6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
use std::fs::{self, File};
use std::io::Write;
use std::path::PathBuf;

use crate::utils::helpers::{
    check_cfg_arg, extract_beta_rev, hex_encode, make, set_file_times, submodule_path_of,
    symlink_dir,
};
use crate::utils::tests::TestCtx;
use crate::{Config, Flags};

#[test]
fn test_make() {
    for (host, make_path) in vec![
        ("dragonfly", PathBuf::from("gmake")),
        ("netbsd", PathBuf::from("gmake")),
        ("freebsd", PathBuf::from("gmake")),
        ("openbsd", PathBuf::from("gmake")),
        ("linux", PathBuf::from("make")),
        // for checking the default
        ("_", PathBuf::from("make")),
    ] {
        assert_eq!(make(host), make_path);
    }
}

#[test]
fn test_beta_rev_parsing() {
    // single digit revision
    assert_eq!(extract_beta_rev("1.99.9-beta.7 (xxxxxx)"), Some("7".to_string()));
    // multiple digits
    assert_eq!(extract_beta_rev("1.99.9-beta.777 (xxxxxx)"), Some("777".to_string()));
    // nightly channel (no beta revision)
    assert_eq!(extract_beta_rev("1.99.9-nightly (xxxxxx)"), None);
    // stable channel (no beta revision)
    assert_eq!(extract_beta_rev("1.99.9 (xxxxxxx)"), None);
    // invalid string
    assert_eq!(extract_beta_rev("invalid"), None);
}

#[test]
fn test_string_to_hex_encode() {
    let input_string = "Hello, World!";
    let hex_string = hex_encode(input_string);
    assert_eq!(hex_string, "48656c6c6f2c20576f726c6421");
}

#[test]
fn test_check_cfg_arg() {
    assert_eq!(check_cfg_arg("bootstrap", None), "--check-cfg=cfg(bootstrap)");
    assert_eq!(
        check_cfg_arg("target_arch", Some(&["s360"])),
        "--check-cfg=cfg(target_arch,values(\"s360\"))"
    );
    assert_eq!(
        check_cfg_arg("target_os", Some(&["nixos", "nix2"])),
        "--check-cfg=cfg(target_os,values(\"nixos\",\"nix2\"))"
    );
}

#[test]
fn test_symlink_dir() {
    let config = TestCtx::new().config("check").no_dry_run().create_config();
    let tempdir = config.tempdir().join(".tmp-dir");
    let link_path = config.tempdir().join(".tmp-link");

    fs::create_dir_all(&tempdir).unwrap();
    symlink_dir(&config, &tempdir, &link_path).unwrap();

    let link_source = fs::read_link(&link_path).unwrap();
    assert_eq!(link_source, tempdir);

    fs::remove_dir(tempdir).unwrap();

    #[cfg(windows)]
    fs::remove_dir(link_path).unwrap();
    #[cfg(not(windows))]
    fs::remove_file(link_path).unwrap();
}

#[test]
fn test_set_file_times_sanity_check() {
    let config = TestCtx::new().config("check").create_config();
    let tempfile = config.tempdir().join(".tmp-file");

    {
        File::create(&tempfile).unwrap().write_all(b"dummy value").unwrap();
        assert!(tempfile.exists());
    }

    // This might only fail on Windows (if file is default read-only then we try to modify file
    // times).
    let unix_epoch = std::time::SystemTime::UNIX_EPOCH;
    let target_time = fs::FileTimes::new().set_accessed(unix_epoch).set_modified(unix_epoch);
    set_file_times(&tempfile, target_time).unwrap();

    let found_metadata = fs::metadata(tempfile).unwrap();
    assert_eq!(found_metadata.accessed().unwrap(), unix_epoch);
    assert_eq!(found_metadata.modified().unwrap(), unix_epoch)
}

#[test]
fn test_submodule_path_of() {
    let config = TestCtx::new().config("build").create_config();

    let build = crate::Build::new(config.clone());
    let builder = crate::core::builder::Builder::new(&build);
    assert_eq!(submodule_path_of(&builder, "invalid/path"), None);
    assert_eq!(submodule_path_of(&builder, "src/tools/cargo"), Some("src/tools/cargo".to_string()));
    assert_eq!(
        submodule_path_of(&builder, "src/llvm-project"),
        Some("src/llvm-project".to_string())
    );
    // Make sure subdirs are handled properly
    assert_eq!(
        submodule_path_of(&builder, "src/tools/cargo/random-subdir"),
        Some("src/tools/cargo".to_string())
    );
}