summaryrefslogtreecommitdiffstats
path: root/tests/config.rs
blob: 441d6fe715e119a7bcf2dba4a893bae30ebb2b0c (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
120
121
122
123
124
use std::path::{Path, PathBuf};
use std::process::Command;

use aur_build::config::Config;

#[test]
fn a_relative_root_is_absolutized_once_and_never_doubled() {
    let rel = PathBuf::from("target").join("config-root-test").join("runtime");
    let _ = std::fs::remove_dir_all(&rel);

    let cfg = Config { root: rel.clone(), ..Config::default() };
    cfg.save().unwrap();
    let reloaded = Config::load(&rel).unwrap();

    assert!(
        reloaded.root.is_absolute(),
        "a saved root must come back absolute, not relative: {}",
        reloaded.root.display()
    );
    assert_eq!(reloaded.root, std::path::absolute(&rel).unwrap());
    assert!(
        !rel.join("runtime").exists(),
        "the root must never be joined onto itself: {}",
        rel.join("runtime").display()
    );
    assert!(reloaded.db_path().starts_with(&reloaded.root));
    assert!(reloaded.repo_arch_dir().starts_with(&reloaded.root));

    std::fs::remove_dir_all(&rel).unwrap();
}

#[test]
fn a_stale_relative_root_in_the_file_is_ignored() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(tmp.path().join("config.json"), "{\n  \"root\": \"runtime\"\n}\n").unwrap();
    let cfg = Config::load(tmp.path()).unwrap();
    assert_eq!(cfg.root, std::path::absolute(tmp.path()).unwrap());
    assert!(!cfg.root.join("runtime").exists());
}

#[test]
fn a_root_with_no_config_is_an_error_not_a_fresh_server() {
    let tmp = tempfile::tempdir().unwrap();
    let err = Config::load(tmp.path()).unwrap_err();
    assert!(
        err.to_string().contains("run `aur-build init`"),
        "the caller must be able to branch on \"there is no config yet\": {err}"
    );
}

#[test]
fn the_cli_refuses_a_root_that_was_never_initialized() {
    let tmp = tempfile::tempdir().unwrap();
    let out = Command::new(env!("CARGO_BIN_EXE_aur-build"))
        .args(["--root", &tmp.path().display().to_string(), "packages"])
        .output()
        .unwrap();
    assert!(!out.status.success(), "a root with no config must fail, not answer from an empty server");
    assert!(
        out.stdout.is_empty(),
        "nothing may be printed as though the server existed: {}",
        String::from_utf8_lossy(&out.stdout)
    );
    let err = String::from_utf8_lossy(&out.stderr);
    assert!(err.contains("init"), "{err}");
}

#[test]
fn init_still_runs_where_there_is_no_config_yet() {
    let tmp = tempfile::tempdir().unwrap();
    let root = tmp.path().join("runtime");
    let out = Command::new(env!("CARGO_BIN_EXE_aur-build"))
        .args(["--root", &root.display().to_string(), "init", "--repo", "aur"])
        .output()
        .unwrap();
    assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr));
    assert!(root.join("config.json").exists());
    assert!(root.join("aur-build.db").exists());

    let cfg = Config::load(&root).unwrap();
    assert_eq!(cfg.root, std::path::absolute(&root).unwrap());
    assert!(!root.join("runtime").exists(), "no doubled tree");
}

#[test]
fn an_absolute_root_is_used_verbatim() {
    let tmp = tempfile::tempdir().unwrap();
    let dir: &Path = tmp.path();
    std::fs::write(dir.join("config.json"), format!("{{\n  \"root\": \"{}\"\n}}\n", dir.display())).unwrap();
    assert_eq!(Config::load(dir).unwrap().root, dir);
}

#[test]
fn serve_refuses_a_remote_config_that_never_asserted_the_workspace() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(
        tmp.path().join("config.json"),
        r#"{ "sandbox": {"kind":"remote","listen":"127.0.0.1:0","token":"t"} }"#,
    )
    .unwrap();
    let out = Command::new(env!("CARGO_BIN_EXE_aur-build"))
        .args(["--root", &tmp.path().display().to_string(), "serve"])
        .output()
        .unwrap();
    assert!(!out.status.success(), "serve must refuse, not start healthy");
    let err = String::from_utf8_lossy(&out.stderr);
    assert!(err.contains("shared_workspace"), "{err}");
}

#[test]
fn the_cli_build_path_refuses_an_unusable_sandbox_config() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(
        tmp.path().join("config.json"),
        r#"{ "sandbox": {"kind":"remote","listen":"127.0.0.1:0","token":""} }"#,
    )
    .unwrap();
    let out = Command::new(env!("CARGO_BIN_EXE_aur-build"))
        .args(["--root", &tmp.path().display().to_string(), "packages"])
        .output()
        .unwrap();
    assert!(!out.status.success());
    assert!(String::from_utf8_lossy(&out.stderr).contains("token"));
}