aboutsummaryrefslogtreecommitdiffstats
path: root/src/config/file.rs
blob: b534f938580ba1d5d279c48acca583376e1f49cc (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
//! Configuration files (`config::file`), read by the node's options: TOML (`.toml`) for
//! people, JSON (`.json`) for generated configuration, with the same keys.

use std::path::{Path, PathBuf};

use anyhow::Result;

use super::{Layer, SCHEMA};

/// The settings `path` makes, its includes resolved.
fn load(path: &Path) -> Result<Layer> {
    ::config::file::load(path, &SCHEMA)
}

mod tests {
    use super::*;
    use crate::files::scratch_dir;
    use serde_json::json;

    fn write(dir: &Path, name: &str, text: &str) -> PathBuf {
        let path = dir.join(name);
        std::fs::write(&path, text).unwrap();
        path
    }

    #[test]
    fn includes_tables_and_importers() {
        let dir = scratch_dir("config-includes");
        write(&dir, "key-path", "/run/secrets/swarm-key\n");
        write(
            &dir,
            "public.toml",
            "neighbours = 8\nalias = \"public\"\n[mycelium]\npeers = [\"tls://a:9651\"]\n",
        );
        write(
            &dir,
            "secrets.json",
            r#"{ "swarm-key-file": { "file": "key-path" } }"#,
        );
        let main = write(
            &dir,
            "main.toml",
            "include = [\"public.toml\", \"secrets.json\", { path = \"missing.toml\", optional = true }]\nalias = \"main\"\n",
        );
        let layer = load(&main).unwrap();
        assert_eq!(layer["neighbours"].value, json!(8));
        assert_eq!(layer["alias"].value, json!("main"));
        assert_eq!(layer["mycelium-peers"].value, json!(["tls://a:9651"]));
        assert_eq!(
            layer["swarm-key-file"].value,
            json!("/run/secrets/swarm-key")
        );
        assert!(layer["swarm-key-file"].origin.is_imported());
        assert!(layer["alias"].origin.to_string().ends_with("main.toml"));
    }

    #[test]
    fn unset() {
        let dir = scratch_dir("config-unset");
        write(
            &dir,
            "base.toml",
            "alias = \"base\"\nneighbours = 8\nkeepalive = \"30s\"\n",
        );
        let toml = write(
            &dir,
            "local.toml",
            "include = \"base.toml\"\nunset = [\"alias\"]\n",
        );
        let layer = load(&toml).unwrap();
        assert!(layer["alias"].value.is_null() && layer["alias"].origin.unset);
        assert_eq!(layer["neighbours"].value, json!(8));
        let json = write(
            &dir,
            "local.json",
            r#"{ "include": "base.toml", "neighbours": null }"#,
        );
        let layer = load(&json).unwrap();
        assert!(layer["neighbours"].origin.unset);
        assert_eq!(layer["keepalive"].value, json!("30s"));
        let unknown = write(&dir, "bad.toml", "unset = [\"nope\"]\n");
        assert!(load(&unknown).is_err());
    }

    #[test]
    fn errors() {
        let dir = scratch_dir("config-errors");
        let unknown = write(&dir, "unknown.toml", "nieghbours = 3\n");
        assert!(load(&unknown).is_err());
        let cycle = write(&dir, "cycle.toml", "include = [\"cycle.toml\"]\n");
        assert!(load(&cycle).is_err());
        let missing = write(&dir, "missing.toml", "include = [\"nope.toml\"]\n");
        assert!(load(&missing).is_err());
        let format = write(&dir, "config.yaml", "alias: x\n");
        assert!(load(&format).is_err());
        let gone = write(&dir, "gone.toml", "include-optional = [\"x.toml\"]\n");
        assert!(load(&gone).is_err());
    }

    /// Includes are loaded in the order listed, optional ones too: the later wins.
    #[test]
    fn includes_in_order() {
        let dir = scratch_dir("config-include-order");
        write(&dir, "local.toml", "alias = \"local\"\n");
        write(&dir, "secrets.toml", "alias = \"secrets\"\n");
        let local_last = write(
            &dir,
            "a.toml",
            "include = [\"secrets.toml\", { path = \"local.toml\", optional = true }]\n",
        );
        assert_eq!(load(&local_last).unwrap()["alias"].value, json!("local"));
        let secrets_last = write(
            &dir,
            "b.toml",
            "include = [{ path = \"local.toml\", optional = true }, \"secrets.toml\"]\n",
        );
        assert_eq!(
            load(&secrets_last).unwrap()["alias"].value,
            json!("secrets")
        );
    }
}