grex-core 1.1.0

Core library for grex, the nested meta-repo manager: manifest, lockfile, scheduler, pack model, plugin traits.
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
//! Integration: `grex_core::pack::parse` round-trip, validation, and error
//! coverage for M3 Stage A. Each test maps onto one acceptance item in
//! `openspec/feat-grex/spec.md`.

#![allow(clippy::too_many_lines)]

use grex_core::pack::{
    parse, Action, ChildRef, Combiner, EnvScope, ExecOnFail, OsKind, PackParseError, PackType,
    Predicate, RequireOnFail, SymlinkKind, MAX_REQUIRE_DEPTH,
};

// ---------- fixtures ----------

const F_DECLARATIVE: &str = include_str!("fixtures/pack-declarative.yaml");
const F_META: &str = include_str!("fixtures/pack-meta.yaml");
const F_SCRIPTED: &str = include_str!("fixtures/pack-scripted.yaml");
const F_REQUIRE_NESTED: &str = include_str!("fixtures/pack-require-nested.yaml");
const F_EXEC_SHELL: &str = include_str!("fixtures/pack-exec-shell.yaml");

// ---------- round-trip + meta ----------

#[test]
fn parse_declarative_round_trip() {
    let pack = parse(F_DECLARATIVE).expect("declarative fixture must parse");
    assert_eq!(pack.schema_version.as_str(), "1");
    assert_eq!(pack.name, "warp-cfg");
    assert_eq!(pack.r#type, PackType::Declarative);
    assert_eq!(pack.version.as_deref(), Some("0.2.0"));
    assert!(pack.children.is_empty());
    assert_eq!(pack.actions.len(), 3);
    matches!(pack.actions[0], Action::Require(_));
    matches!(pack.actions[1], Action::When(_));
    matches!(pack.actions[2], Action::Exec(_));
    let teardown = pack.teardown.as_ref().expect("explicit teardown expected");
    assert_eq!(teardown.len(), 1);
    matches!(teardown[0], Action::Rmdir(_));
}

#[test]
fn parse_meta_children_effective_path() {
    let pack = parse(F_META).expect("meta fixture must parse");
    assert_eq!(pack.r#type, PackType::Meta);
    assert_eq!(pack.children.len(), 3);
    // `.git` suffix stripped for default.
    assert_eq!(pack.children[0].effective_path(), "foo");
    assert!(pack.children[0].path.is_none());
    // Explicit override wins.
    assert_eq!(pack.children[1].effective_path(), "bar-override");
    assert_eq!(pack.children[1].r#ref.as_deref(), Some("v1.2.0"));
    // HTTPS URL with no `.git` suffix and no override.
    assert_eq!(pack.children[2].effective_path(), "baz");
}

#[test]
fn parse_scripted_minimal() {
    let pack = parse(F_SCRIPTED).expect("scripted fixture must parse");
    assert_eq!(pack.r#type, PackType::Scripted);
    assert!(pack.actions.is_empty());
    assert!(pack.children.is_empty());
    assert!(pack.teardown.is_none());
}

// ---------- schema_version ----------

#[test]
fn schema_version_must_be_quoted_string_one() {
    assert!(parse("schema_version: \"1\"\nname: ok\ntype: meta\n").is_ok());
    let err = parse("schema_version: 1\nname: ok\ntype: meta\n").unwrap_err();
    // Stringified bare int fails via the custom Deserialize → serde_yaml::Error.
    assert!(
        matches!(err, PackParseError::Inner(_)),
        "bare int must fail at SchemaVersion deserialize, got {err:?}"
    );
    let err = parse("schema_version: \"2\"\nname: ok\ntype: meta\n").unwrap_err();
    assert!(matches!(err, PackParseError::InvalidSchemaVersion { .. }), "{err:?}");
}

// ---------- name regex ----------

#[test]
fn invalid_name_rejected() {
    // Per pack-spec.md §Validation, the regex is ^[a-z][a-z0-9-]*$:
    // the first character must be a lowercase letter (digits allowed only
    // in later positions).
    for bad in ["NAME", "with_underscore", "", "-leading", "has space", "123-bad", "9to5"] {
        let yaml = format!("schema_version: \"1\"\nname: {bad:?}\ntype: meta\n");
        let err = parse(&yaml).unwrap_err();
        assert!(matches!(err, PackParseError::InvalidName { .. }), "name {bad:?} must fail");
    }
    // Positive controls: each exercises a boundary of the regex.
    for good in ["ok", "a", "warp-cfg", "a9", "pack-2"] {
        let yaml = format!("schema_version: \"1\"\nname: {good}\ntype: meta\n");
        parse(&yaml).unwrap_or_else(|e| panic!("good name {good:?} must parse, got {e:?}"));
    }
}

// ---------- empty collections ----------

#[test]
fn empty_actions_valid() {
    let yaml = "schema_version: \"1\"\nname: ok\ntype: declarative\nactions: []\n";
    let pack = parse(yaml).expect("empty actions must parse");
    assert!(pack.actions.is_empty());
}

#[test]
fn empty_children_valid() {
    let yaml = "schema_version: \"1\"\nname: ok\ntype: meta\nchildren: []\n";
    let pack = parse(yaml).expect("empty children must parse");
    assert!(pack.children.is_empty());
}

#[test]
fn teardown_omitted_vs_empty() {
    let omitted =
        parse("schema_version: \"1\"\nname: ok\ntype: declarative\n").expect("omitted teardown");
    assert!(omitted.teardown.is_none());
    let empty =
        parse("schema_version: \"1\"\nname: ok\ntype: declarative\nteardown: []\n").unwrap();
    assert_eq!(empty.teardown.as_deref(), Some(&[][..]));
}

// ---------- ordering + serialize ----------

#[test]
fn action_order_preserved() {
    let pack = parse(F_DECLARATIVE).unwrap();
    assert!(matches!(pack.actions[0], Action::Require(_)));
    assert!(matches!(pack.actions[1], Action::When(_)));
    assert!(matches!(pack.actions[2], Action::Exec(_)));
}

// ---------- YAML anchor/alias rejection ----------

#[test]
fn yaml_anchors_rejected() {
    let yaml = "
schema_version: \"1\"
name: ok
type: declarative
actions:
  - &sym
    symlink: { src: a, dst: b }
  - *sym
";
    let err = parse(yaml).unwrap_err();
    assert!(matches!(err, PackParseError::YamlAliasRejected), "{err:?}");
}

// ---------- unknown top-level ----------

#[test]
fn unknown_top_level_key_x_prefix_allowed() {
    let yaml = "schema_version: \"1\"\nname: ok\ntype: meta\nx-custom: whatever\n";
    let pack = parse(yaml).expect("x-* must be accepted");
    assert!(pack.extensions.contains_key("x-custom"));
    let yaml_bad = "schema_version: \"1\"\nname: ok\ntype: meta\ncustom: whatever\n";
    let err = parse(yaml_bad).unwrap_err();
    assert!(matches!(err, PackParseError::UnknownTopLevelKey { ref key } if key == "custom"));
}

// ---------- action-entry shape errors ----------

#[test]
fn unknown_action_key_rejected() {
    let yaml = "schema_version: \"1\"\nname: ok\ntype: declarative\nactions:\n  - notavar: {}\n";
    let err = parse(yaml).unwrap_err();
    match err {
        PackParseError::UnknownActionKey { key } => assert_eq!(key, "notavar"),
        other => panic!("expected UnknownActionKey, got {other:?}"),
    }
}

#[test]
fn empty_action_entry_rejected() {
    let yaml = "schema_version: \"1\"\nname: ok\ntype: declarative\nactions:\n  - {}\n";
    let err = parse(yaml).unwrap_err();
    assert!(matches!(err, PackParseError::EmptyActionEntry), "{err:?}");
}

#[test]
fn multiple_action_keys_rejected() {
    let yaml = "schema_version: \"1\"
name: ok
type: declarative
actions:
  - symlink: { src: a, dst: b }
    env: { name: N, value: V }
";
    let err = parse(yaml).unwrap_err();
    match err {
        PackParseError::MultipleActionKeys { keys } => {
            assert!(keys.contains(&"symlink".to_string()));
            assert!(keys.contains(&"env".to_string()));
        }
        other => panic!("expected MultipleActionKeys, got {other:?}"),
    }
}

// ---------- exec XOR ----------

#[test]
fn exec_cmd_shell_mutex_false() {
    let yaml = "schema_version: \"1\"
name: ok
type: declarative
actions:
  - exec:
      shell: false
      cmd_shell: \"oops\"
";
    let err = parse(yaml).unwrap_err();
    assert!(matches!(err, PackParseError::ExecCmdMutex { shell: false, .. }), "{err:?}");
}

#[test]
fn exec_cmd_shell_mutex_true() {
    let yaml = "schema_version: \"1\"
name: ok
type: declarative
actions:
  - exec:
      shell: true
      cmd: [\"echo\", \"x\"]
";
    let err = parse(yaml).unwrap_err();
    assert!(matches!(err, PackParseError::ExecCmdMutex { shell: true, .. }), "{err:?}");
}

#[test]
fn exec_cmd_shell_positive_both_paths() {
    // Array form.
    let array = parse(
        "schema_version: \"1\"
name: ok
type: declarative
actions:
  - exec: { cmd: [\"git\", \"status\"], shell: false }
",
    )
    .unwrap();
    match &array.actions[0] {
        Action::Exec(e) => {
            assert!(!e.shell);
            assert_eq!(e.cmd.as_ref().unwrap().len(), 2);
            assert!(e.cmd_shell.is_none());
        }
        _ => panic!("expected exec"),
    }
    // Shell form fixture.
    let shell = parse(F_EXEC_SHELL).unwrap();
    match &shell.actions[0] {
        Action::Exec(e) => {
            assert!(e.shell);
            assert_eq!(e.cmd_shell.as_deref(), Some("echo hello world"));
            assert_eq!(e.on_fail, ExecOnFail::Ignore);
        }
        _ => panic!("expected exec"),
    }
}

// ---------- on_fail set split (BUG 1) ----------

#[test]
fn require_on_fail_skip_accepted() {
    let ok = parse(
        "schema_version: \"1\"
name: ok
type: declarative
actions:
  - require:
      all_of: [{ os: windows }]
      on_fail: skip
",
    )
    .unwrap();
    match &ok.actions[0] {
        Action::Require(r) => assert_eq!(r.on_fail, RequireOnFail::Skip),
        _ => panic!("expected require"),
    }
    let err = parse(
        "schema_version: \"1\"
name: ok
type: declarative
actions:
  - require:
      all_of: [{ os: windows }]
      on_fail: ignore
",
    )
    .unwrap_err();
    assert!(matches!(err, PackParseError::Inner(_)), "ignore must reject on require: {err:?}");
}

#[test]
fn exec_on_fail_ignore_accepted() {
    let ok = parse(F_EXEC_SHELL).unwrap();
    match &ok.actions[0] {
        Action::Exec(e) => assert_eq!(e.on_fail, ExecOnFail::Ignore),
        _ => panic!("expected exec"),
    }
    let err = parse(
        "schema_version: \"1\"
name: ok
type: declarative
actions:
  - exec:
      shell: true
      cmd_shell: \"ls\"
      on_fail: skip
",
    )
    .unwrap_err();
    assert!(matches!(err, PackParseError::Inner(_)), "skip must reject on exec: {err:?}");
}

// ---------- predicate nesting ----------

#[test]
fn require_nested_depth2() {
    let pack = parse(F_REQUIRE_NESTED).expect("nested require fixture must parse");
    let Action::Require(req) = &pack.actions[0] else {
        panic!("expected require action");
    };
    assert_eq!(req.on_fail, RequireOnFail::Warn);
    let Combiner::AllOf(outer) = &req.combiner else {
        panic!("expected all_of top combiner");
    };
    // Outer has 2 preds: a nested any_of and os=windows.
    assert_eq!(outer.len(), 2);
    let Predicate::AnyOf(inner) = &outer[0] else {
        panic!("expected any_of nested");
    };
    // Inner any_of has cmd_available and a nested none_of (depth 2 reachable).
    assert_eq!(inner.len(), 2);
    assert!(matches!(inner[0], Predicate::CmdAvailable(_)));
    assert!(matches!(inner[1], Predicate::NoneOf(_)));
    assert!(matches!(outer[1], Predicate::Os(OsKind::Windows)));
}

#[test]
fn require_depth_exceeded() {
    // Build a predicate tree where the top-level combiner already sits at
    // depth 1 and subsequent all_of nests dig deeper. We want the inner
    // predicate depth to exceed MAX_REQUIRE_DEPTH (32).
    let inner_leaf = "{ os: windows }";
    let mut nested = inner_leaf.to_string();
    for _ in 0..(MAX_REQUIRE_DEPTH + 2) {
        nested = format!("{{ all_of: [{nested}] }}");
    }
    let yaml = format!(
        "schema_version: \"1\"
name: ok
type: declarative
actions:
  - require:
      all_of: [{nested}]
",
    );
    let err = parse(&yaml).unwrap_err();
    assert!(
        matches!(err, PackParseError::RequireDepthExceeded { .. }),
        "expected RequireDepthExceeded, got {err:?}"
    );
}

// ---------- reg_key BUG 3 ----------

#[test]
fn reg_key_legacy_form_rejected() {
    let yaml = "schema_version: \"1\"
name: ok
type: declarative
actions:
  - require:
      all_of:
        - reg_key: \"HKLM\\\\Software\\\\X!Val\"
      on_fail: error
";
    let err = parse(yaml).unwrap_err();
    match err {
        PackParseError::InvalidPredicate { detail } => {
            assert!(detail.contains("reg_key"), "error should cite reg_key, got {detail:?}");
        }
        other => panic!("expected InvalidPredicate, got {other:?}"),
    }
}

#[test]
fn reg_key_map_form_accepted() {
    let yaml = "schema_version: \"1\"
name: ok
type: declarative
actions:
  - require:
      all_of:
        - reg_key: { path: \"HKLM\\\\Software\\\\X\", name: \"Val\" }
      on_fail: error
";
    let pack = parse(yaml).unwrap();
    let Action::Require(req) = &pack.actions[0] else { panic!("require") };
    let Combiner::AllOf(preds) = &req.combiner else { panic!("all_of") };
    match &preds[0] {
        Predicate::RegKey { path, name } => {
            assert_eq!(path, "HKLM\\Software\\X");
            assert_eq!(name.as_deref(), Some("Val"));
        }
        other => panic!("expected RegKey, got {other:?}"),
    }
}

// ---------- action default fields ----------

#[test]
fn symlink_defaults_applied() {
    let yaml = "schema_version: \"1\"
name: ok
type: declarative
actions:
  - symlink: { src: files/a, dst: \"$HOME/a\" }
";
    let pack = parse(yaml).unwrap();
    let Action::Symlink(s) = &pack.actions[0] else { panic!("symlink") };
    assert!(!s.backup);
    assert!(s.normalize);
    assert_eq!(s.kind, SymlinkKind::Auto);
}

#[test]
fn env_defaults_applied() {
    let yaml = "schema_version: \"1\"
name: ok
type: declarative
actions:
  - env: { name: FOO, value: bar }
";
    let pack = parse(yaml).unwrap();
    let Action::Env(e) = &pack.actions[0] else { panic!("env") };
    assert_eq!(e.scope, EnvScope::User);
}

// ---------- missing required ----------

#[test]
fn missing_required_field_src_in_symlink() {
    let yaml = "schema_version: \"1\"
name: ok
type: declarative
actions:
  - symlink: { dst: \"$HOME/a\" }
";
    let err = parse(yaml).unwrap_err();
    match err {
        PackParseError::Inner(e) => {
            let msg = e.to_string();
            assert!(msg.contains("src"), "error should cite `src` field, got {msg:?}");
        }
        other => panic!("expected serde inner error, got {other:?}"),
    }
}

// ---------- pack_type surface ----------

#[test]
fn pack_type_variants_all_parse() {
    for (ty, expect) in [
        ("meta", PackType::Meta),
        ("declarative", PackType::Declarative),
        ("scripted", PackType::Scripted),
    ] {
        let yaml = format!("schema_version: \"1\"\nname: ok\ntype: {ty}\n");
        let pack = parse(&yaml).unwrap_or_else(|e| panic!("type {ty} must parse: {e:?}"));
        assert_eq!(pack.r#type, expect);
    }
}

#[test]
fn pack_type_uppercase_rejected() {
    let err = parse("schema_version: \"1\"\nname: ok\ntype: Meta\n").unwrap_err();
    assert!(matches!(err, PackParseError::Inner(_)), "{err:?}");
}

// ---------- round-trip via re-serialize ----------

#[test]
fn child_ref_round_trip() {
    // Separate from the whole-manifest round-trip: ChildRef is the one
    // shape we round-trip structurally (manifest has parse-only top-level).
    let pack = parse(F_META).unwrap();
    let s = serde_yaml::to_string(&pack.children).unwrap();
    let back: Vec<ChildRef> = serde_yaml::from_str(&s).unwrap();
    assert_eq!(back, pack.children);
}