starmap-cli 0.4.1

The starmap command, which checks, explains, compares and runs an application's declared environment
Documentation
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
use starmap::Spec;

use super::*;

/// Returns the spec that `text` holds.
fn spec(text: &str) -> Spec {
    Spec::parse(text).unwrap()
}

/// A withheld secret with a 1Password bind.
const TOKEN: &str = r#"
[variables.SHELF_TOKEN]
purpose = "The token."
type = "string"
unset = "required"
show = "withheld"
source = "secret"
bind = { one-password = "op://Personal/shelf/token" }
"#;

#[test]
fn equal_specs_have_no_changes() {
    assert_eq!(changes(Some(&spec(TOKEN)), &spec(TOKEN)), Vec::new());
}

#[test]
fn a_spec_against_no_spec_counts_every_variable_as_added() {
    let changes = changes(None, &spec(TOKEN));
    assert_eq!(
        changes,
        vec![Change {
            change: Kind::Added,
            name: "SHELF_TOKEN".to_owned(),
            field: None,
            was: None,
            now: Some("string, withheld, secret".to_owned()),
        }]
    );
}

#[test]
fn a_changed_bind_and_purpose_are_one_change_each_in_field_order() {
    let now = TOKEN
        .replace("The token.", "The mirror's token.")
        .replace("shelf/token", "shelf/mirror");
    let fields: Vec<Option<String>> = changes(Some(&spec(TOKEN)), &spec(&now))
        .into_iter()
        .map(|change| change.field)
        .collect();
    assert_eq!(
        fields,
        vec![Some("bind".to_owned()), Some("purpose".to_owned())]
    );
}

#[test]
fn an_added_changed_or_removed_effect_is_a_change_of_the_effect() {
    let with = |effect: &str| {
        TOKEN.replace(
            "purpose = \"The token.\"\n",
            &format!("purpose = \"The token.\"\neffect = \"{effect}\"\n"),
        )
    };
    let change = |was: Option<&str>, now: Option<&str>| Change {
        change: Kind::Changed,
        name: "SHELF_TOKEN".to_owned(),
        field: Some("effect".to_owned()),
        was: was.map(str::to_owned),
        now: now.map(str::to_owned),
    };
    let (old, new) = (with("Uploads fail."), with("Uploads are skipped."));

    assert_eq!(
        changes(Some(&spec(TOKEN)), &spec(&old)),
        vec![change(None, Some("Uploads fail."))]
    );
    assert_eq!(
        changes(Some(&spec(&old)), &spec(&new)),
        vec![change(Some("Uploads fail."), Some("Uploads are skipped."))]
    );
    assert_eq!(
        changes(Some(&spec(&old)), &spec(TOKEN)),
        vec![change(Some("Uploads fail."), None)]
    );
}

#[test]
fn an_added_check_sentence_is_a_change_of_the_check() {
    let with = TOKEN.replace(
        "purpose = \"The token.\"\n",
        "purpose = \"The token.\"\ncheck = \"The token starts with tk_.\"\n",
    );

    assert_eq!(
        changes(Some(&spec(TOKEN)), &spec(&with)),
        vec![Change {
            change: Kind::Changed,
            name: "SHELF_TOKEN".to_owned(),
            field: Some("check".to_owned()),
            was: None,
            now: Some("The token starts with tk_.".to_owned()),
        }]
    );
}

/// A port setting, which a later spec turns into a socket address.
const LISTEN: &str = r#"
[variables.SHELF_LISTEN]
purpose = "Where the api process listens."
type = "port"
unset = "required"
show = "shown"
source = "setting"
"#;

#[test]
fn a_changed_pattern_is_a_change_of_the_type() {
    let four = LISTEN.replace("\"port\"", "{ string = { pattern = \"[0-9]{4}\" } }");
    let five = LISTEN.replace("\"port\"", "{ string = { pattern = \"[0-9]{5}\" } }");

    assert_eq!(
        changes(Some(&spec(&four)), &spec(&five)),
        vec![Change {
            change: Kind::Changed,
            name: "SHELF_LISTEN".to_owned(),
            field: Some("type".to_owned()),
            was: Some("string matching [0-9]{4}".to_owned()),
            now: Some("string matching [0-9]{5}".to_owned()),
        }]
    );
}

#[test]
fn a_socket_address_is_named_when_it_is_added_and_when_a_type_changes_to_it() {
    let socket = LISTEN.replace("\"port\"", "\"socket-address\"");

    let added = changes(None, &spec(&socket));
    assert_eq!(
        added
            .first()
            .and_then(|change| change.now.clone())
            .as_deref(),
        Some("ip socket address, shown, setting")
    );

    let changed = changes(Some(&spec(LISTEN)), &spec(&socket));
    assert_eq!(
        changed,
        vec![Change {
            change: Kind::Changed,
            name: "SHELF_LISTEN".to_owned(),
            field: Some("type".to_owned()),
            was: Some("port".to_owned()),
            now: Some("ip socket address".to_owned()),
        }]
    );
}

#[test]
fn turning_a_cache_off_is_a_change_of_the_cache_and_not_of_the_bind() {
    let now = TOKEN.replace(
        r#"bind = { one-password = "op://Personal/shelf/token" }"#,
        r#"bind = { one-password = "op://Personal/shelf/token", cache = false }"#,
    );
    let changes = changes(Some(&spec(TOKEN)), &spec(&now));
    assert_eq!(
        changes,
        vec![Change {
            change: Kind::Changed,
            name: "SHELF_TOKEN".to_owned(),
            field: Some("cache".to_owned()),
            was: Some("allowed".to_owned()),
            now: Some("never".to_owned()),
        }]
    );
}

#[test]
fn a_new_reference_with_cache_off_is_a_change_of_the_bind_and_the_cache() {
    let now = TOKEN.replace(
        r#"bind = { one-password = "op://Personal/shelf/token" }"#,
        r#"bind = { one-password = "op://Personal/shelf/key", cache = false }"#,
    );
    let fields: Vec<_> = changes(Some(&spec(TOKEN)), &spec(&now))
        .into_iter()
        .map(|change| change.field)
        .collect();
    assert_eq!(fields, [Some("bind".to_owned()), Some("cache".to_owned())]);
}

#[test]
fn writing_cache_true_is_no_change() {
    let now = TOKEN.replace(
        r#"bind = { one-password = "op://Personal/shelf/token" }"#,
        r#"bind = { one-password = "op://Personal/shelf/token", cache = true }"#,
    );
    assert_eq!(changes(Some(&spec(TOKEN)), &spec(&now)), Vec::new());
}

/// A withheld key whose type is 32 bytes as base64.
const KEY: &str = r#"
[variables.SHELF_KEY]
purpose = "The key."
type = { bytes = { encoding = "base64", length = 32 } }
unset = "required"
show = "withheld"
source = "secret"
"#;

#[test]
fn an_added_bytes_variable_names_the_type_word_bytes() {
    assert_eq!(
        changes(None, &spec(KEY))
            .into_iter()
            .map(|change| change.now)
            .collect::<Vec<_>>(),
        [Some("bytes, withheld, secret".to_owned())]
    );
}

#[test]
fn a_changed_encoding_or_length_is_a_change_of_the_type() {
    let hex = KEY.replace("\"base64\"", "\"hex\"");
    let longer = KEY.replace("length = 32", "min-length = 64");

    for (now, text) in [
        (hex, "32 bytes as hex"),
        (longer, "at least 64 bytes as base64"),
    ] {
        assert_eq!(
            changes(Some(&spec(KEY)), &spec(&now)),
            vec![Change {
                change: Kind::Changed,
                name: "SHELF_KEY".to_owned(),
                field: Some("type".to_owned()),
                was: Some("32 bytes as base64".to_owned()),
                now: Some(text.to_owned()),
            }]
        );
    }
}

#[test]
fn a_changed_scheme_list_is_a_change_of_the_type() {
    let https = LISTEN.replace("\"port\"", "{ url = { schemes = [\"https\"] } }");
    let both = LISTEN.replace("\"port\"", "{ url = { schemes = [\"https\", \"http\"] } }");

    assert_eq!(
        changes(Some(&spec(&https)), &spec(&both)),
        vec![Change {
            change: Kind::Changed,
            name: "SHELF_LISTEN".to_owned(),
            field: Some("type".to_owned()),
            was: Some("url (https)".to_owned()),
            now: Some("url (https, http)".to_owned()),
        }]
    );
}

#[test]
fn reading_a_boolean_by_its_presence_is_a_change_of_the_type() {
    let plain = LISTEN
        .replace("\"port\"", "\"boolean\"")
        .replace("\"required\"", "\"optional\"");
    let presence = plain.replace("\"boolean\"", "\"boolean\"\npresence = true");

    assert_eq!(
        changes(Some(&spec(&plain)), &spec(&presence)),
        vec![Change {
            change: Kind::Changed,
            name: "SHELF_LISTEN".to_owned(),
            field: Some("type".to_owned()),
            was: Some("boolean".to_owned()),
            now: Some("boolean (true when set to any value)".to_owned()),
        }]
    );
}

#[test]
fn declaring_an_empty_value_a_value_is_a_change_of_the_empty_field_alone() {
    let value = LISTEN.replace("\"port\"", "\"port\"\nempty = \"value\"");
    let unset = LISTEN.replace("\"port\"", "\"port\"\nempty = \"unset\"");

    assert_eq!(
        changes(Some(&spec(LISTEN)), &spec(&value)),
        vec![Change {
            change: Kind::Changed,
            name: "SHELF_LISTEN".to_owned(),
            field: Some("empty".to_owned()),
            was: Some("an empty value counts as unset".to_owned()),
            now: Some("an empty value is a value".to_owned()),
        }]
    );
    assert_eq!(changes(Some(&spec(LISTEN)), &spec(&unset)), Vec::new());
}

/// A log level setting whose type is the named enumeration `log-level`.
const LOG_LEVEL: &str = r#"
[enumerations.log-level]
debug = "Logs every request."
info = "Logs each request once."

[variables.SHELF_LOG_LEVEL]
purpose = "How much the api process logs."
type = { enumeration = "log-level" }
unset = "required"
show = "shown"
source = "setting"
"#;

/// Returns a change of `field` of `SHELF_LOG_LEVEL` from `was` to `now`.
fn log_level_change(field: &str, was: &str, now: &str) -> Change {
    Change {
        change: Kind::Changed,
        name: "SHELF_LOG_LEVEL".to_owned(),
        field: Some(field.to_owned()),
        was: Some(was.to_owned()),
        now: Some(now.to_owned()),
    }
}

#[test]
fn a_changed_description_is_a_change_of_the_values() {
    let now = LOG_LEVEL.replace("Logs every request.", "Logs every request and its timing.");

    assert_eq!(
        changes(Some(&spec(LOG_LEVEL)), &spec(&now)),
        vec![log_level_change(
            "values",
            "debug: Logs every request.; info: Logs each request once.",
            "debug: Logs every request and its timing.; info: Logs each request once.",
        )]
    );
}

#[test]
fn a_change_from_an_inline_list_to_a_named_enumeration_is_a_change_of_the_type() {
    let inline = LOG_LEVEL.replace(
        "{ enumeration = \"log-level\" }",
        "{ enumeration = [\"debug\", \"info\"] }",
    );

    let fields: Vec<Option<String>> = changes(Some(&spec(&inline)), &spec(LOG_LEVEL))
        .into_iter()
        .map(|change| change.field)
        .collect();

    assert_eq!(
        fields,
        vec![Some("type".to_owned()), Some("values".to_owned())]
    );
}

#[test]
fn a_renamed_enumeration_is_a_change_of_the_type_alone() {
    let renamed = LOG_LEVEL.replace("log-level", "verbosity");

    assert_eq!(
        changes(Some(&spec(LOG_LEVEL)), &spec(&renamed)),
        vec![log_level_change(
            "type",
            "log-level: one of debug, info",
            "verbosity: one of debug, info",
        )]
    );
}

/// Two optional settings, which `group` may put in a group.
fn mirror(group: &str) -> String {
    format!(
        "{group}\n\
         [variables.SHELF_MIRROR_ACCOUNT]\npurpose = \"The account.\"\ntype = \"string\"\n\
         unset = \"optional\"\nshow = \"shown\"\nsource = \"setting\"\n\n\
         [variables.SHELF_MIRROR_KEY]\npurpose = \"The key.\"\ntype = \"string\"\n\
         unset = \"optional\"\nshow = \"shown\"\nsource = \"setting\"\n"
    )
}

/// The group `name` of both settings.
fn group(name: &str) -> String {
    format!("[groups.{name}]\nvariables = [\"SHELF_MIRROR_ACCOUNT\", \"SHELF_MIRROR_KEY\"]\n")
}

/// Returns a change of the group of `variable` from `was` to `now`.
fn group_change(variable: &str, was: Option<&str>, now: Option<&str>) -> Change {
    Change {
        change: Kind::Changed,
        name: variable.to_owned(),
        field: Some("group".to_owned()),
        was: was.map(str::to_owned),
        now: now.map(str::to_owned),
    }
}

#[test]
fn a_variable_that_joins_a_group_is_a_change_of_the_group() {
    assert_eq!(
        changes(Some(&spec(&mirror(""))), &spec(&mirror(&group("mirror")))),
        vec![
            group_change("SHELF_MIRROR_ACCOUNT", None, Some("mirror")),
            group_change("SHELF_MIRROR_KEY", None, Some("mirror")),
        ]
    );
}

#[test]
fn a_renamed_group_is_a_change_of_the_group_of_each_member() {
    assert_eq!(
        changes(
            Some(&spec(&mirror(&group("mirror")))),
            &spec(&mirror(&group("upstream")))
        ),
        vec![
            group_change("SHELF_MIRROR_ACCOUNT", Some("mirror"), Some("upstream")),
            group_change("SHELF_MIRROR_KEY", Some("mirror"), Some("upstream")),
        ]
    );
}