alba-core 0.2.1

Core domain model for Alba: beams, Beamfiles, evaluation, and the beam graph
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
//! Integration tests for [`alba_core::load_project`]: multi-file loading,
//! import namespacing, and the errors this crate's loader owns (a missing
//! import, an import cycle, a duplicate alias). Uses real files under a
//! [`tempfile::tempdir`] rather than [`alba_core::load_str`], since
//! resolving `import` paths relative to the importing file's directory is
//! the behavior under test.

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

use alba_core::{SourceId, load_project};

/// Writes `content` to `path`, creating any missing parent directories
/// first, so a test fixture can lay out a small tree of Beamfiles (e.g.
/// `api/Beamfile`) with a single call per file.
fn write(path: PathBuf, content: &str) {
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent).unwrap();
    }
    std::fs::write(path, content).unwrap();
}

#[test]
fn imports_namespace_beams_and_resolve_local_needs() {
    let dir = tempfile::tempdir().unwrap();
    write(
        dir.path().join("Beamfile"),
        "import \"api/Beamfile\" as api\nbeam all { needs [api:test] run \"echo ok\" }",
    );
    write(
        dir.path().join("api/Beamfile"),
        "beam build { run \"echo b\" }\nbeam test { needs [build] run \"echo t\" }",
    );
    let (project, _) = load_project(&dir.path().join("Beamfile")).unwrap();
    let ids: Vec<&str> = project.beams.iter().map(|b| b.id.0.as_str()).collect();
    assert!(ids.contains(&"api:build") && ids.contains(&"api:test") && ids.contains(&"all"));
    let api_test = project.beams.iter().find(|b| b.id.0 == "api:test").unwrap();
    assert_eq!(api_test.needs[0].value.0, "api:build"); // local need namespaced
}

#[test]
fn import_cycle_is_reported_with_chain() {
    // a/Beamfile imports ../b/Beamfile, b imports ../a/Beamfile.
    let dir = tempfile::tempdir().unwrap();
    write(
        dir.path().join("a/Beamfile"),
        "import \"../b/Beamfile\" as b\nbeam x { needs [b:y] run \"echo a\" }",
    );
    write(
        dir.path().join("b/Beamfile"),
        "import \"../a/Beamfile\" as a\nbeam y { needs [a:x] run \"echo b\" }",
    );

    let err = load_project(&dir.path().join("a/Beamfile")).unwrap_err();

    assert!(err.error.message.contains("import cycle"));
    // A structural check on the chain, not just a substring match: every
    // message here contains the letters "a" and "b" regardless of whether
    // a real chain was reported (e.g. "Beamfile" alone contains "a"), so
    // assert on the shape instead — three file names joined by two
    // arrows (a -> b -> a, closing the cycle).
    assert_eq!(err.error.message.matches("Beamfile").count(), 3);
    assert_eq!(err.error.message.matches(" -> ").count(), 2);
}

#[test]
fn imported_default_is_ignored() {
    // Root has no `default`; the imported child does. The child's
    // `default` must not leak into the project's default.
    let dir = tempfile::tempdir().unwrap();
    write(
        dir.path().join("Beamfile"),
        "import \"api/Beamfile\" as api\nbeam all { run \"echo ok\" }",
    );
    write(
        dir.path().join("api/Beamfile"),
        "default build\nbeam build { run \"echo b\" }",
    );

    let (project, _) = load_project(&dir.path().join("Beamfile")).unwrap();

    assert!(project.default.is_none());
}

#[test]
fn root_default_is_kept() {
    let dir = tempfile::tempdir().unwrap();
    write(
        dir.path().join("Beamfile"),
        "default all\nbeam all { run \"echo ok\" }",
    );

    let (project, _) = load_project(&dir.path().join("Beamfile")).unwrap();

    assert_eq!(
        project.default.as_ref().map(|d| d.value.0.as_str()),
        Some("all")
    );
}

#[test]
fn nested_imports_join_namespaces_with_colon() {
    // A beam reached through two levels of aliasing gets a two-segment id
    // once the root absorbs `api`'s beams under the `api:` prefix, and the
    // root can name it in full.
    let dir = tempfile::tempdir().unwrap();
    write(
        dir.path().join("Beamfile"),
        "import \"api/Beamfile\" as api\n\
         beam all { needs [api:build, api:db:migrate] run \"echo ok\" }",
    );
    write(
        dir.path().join("api/Beamfile"),
        "import \"db/Beamfile\" as db\nbeam build { needs [db:migrate] run \"echo b\" }",
    );
    write(
        dir.path().join("api/db/Beamfile"),
        "beam migrate { run \"echo m\" }",
    );

    let (project, _) = load_project(&dir.path().join("Beamfile")).unwrap();

    let ids: Vec<&str> = project.beams.iter().map(|b| b.id.0.as_str()).collect();
    assert!(ids.contains(&"api:db:migrate"));
    let build = project
        .beams
        .iter()
        .find(|b| b.id.0 == "api:build")
        .unwrap();
    assert_eq!(build.needs[0].value.0, "api:db:migrate");

    // The root's own reference to that two-segment id resolves: graph
    // validation would have rejected the load otherwise.
    let all = project.beams.iter().find(|b| b.id.0 == "all").unwrap();
    let needs: Vec<&str> = all.needs.iter().map(|n| n.value.0.as_str()).collect();
    assert_eq!(needs, ["api:build", "api:db:migrate"]);
}

/// A file reached from two import sites is read, parsed, and evaluated
/// once, not once per site. Without that, cost is exponential in nesting
/// depth: nineteen three-line files that each import the next twice used
/// to take half a million loads.
///
/// The pin is the source map: every load registers one entry, so a second
/// registration for `shared/Beamfile` (a fourth id here) means the file
/// was loaded twice. The beams themselves must still be namespaced per
/// site — that is a purely textual step, applied to the memoized result.
#[test]
fn a_file_imported_from_two_sites_is_loaded_once() {
    let dir = tempfile::tempdir().unwrap();
    write(
        dir.path().join("Beamfile"),
        "import \"left/Beamfile\" as l\nimport \"right/Beamfile\" as r\n\
         beam all { needs [l:x, r:x] run \"echo ok\" }",
    );
    write(
        dir.path().join("left/Beamfile"),
        "import \"../shared/Beamfile\" as s\nbeam x { needs [s:base] run \"echo l\" }",
    );
    write(
        dir.path().join("right/Beamfile"),
        "import \"../shared/Beamfile\" as s\nbeam x { needs [s:base] run \"echo r\" }",
    );
    write(
        dir.path().join("shared/Beamfile"),
        "beam base { run \"echo s\" }",
    );

    let (project, sources) = load_project(&dir.path().join("Beamfile")).unwrap();

    let mut ids: Vec<&str> = project.beams.iter().map(|b| b.id.0.as_str()).collect();
    ids.sort_unstable();
    assert_eq!(ids, ["all", "l:s:base", "l:x", "r:s:base", "r:x"]);

    assert!(
        sources.get(SourceId(3)).is_some(),
        "four distinct files were read"
    );
    assert!(
        sources.get(SourceId(4)).is_none(),
        "`shared/Beamfile` must be read once, not once per import site"
    );
}

/// The memoized result must not leak one import site's namespacing into
/// another's: both copies of a shared beam keep their own prefix, and both
/// point back at the single file that declared them.
#[test]
fn both_copies_of_a_shared_import_keep_their_own_namespace() {
    let dir = tempfile::tempdir().unwrap();
    write(
        dir.path().join("Beamfile"),
        "import \"shared/Beamfile\" as a\nimport \"shared/Beamfile\" as b\n\
         beam all { needs [a:base, b:base] run \"echo ok\" }",
    );
    write(
        dir.path().join("shared/Beamfile"),
        "beam base { run \"echo s\" }",
    );

    let (project, _) = load_project(&dir.path().join("Beamfile")).unwrap();

    let shared: Vec<&alba_core::Beam> = project
        .beams
        .iter()
        .filter(|beam| beam.id.0.ends_with(":base"))
        .collect();
    let ids: Vec<&str> = shared.iter().map(|beam| beam.id.0.as_str()).collect();
    assert_eq!(ids, ["a:base", "b:base"]);
    assert_eq!(shared[0].source, shared[1].source);
}

#[test]
fn duplicate_import_alias_is_an_error() {
    let dir = tempfile::tempdir().unwrap();
    write(
        dir.path().join("Beamfile"),
        "import \"a/Beamfile\" as x\nimport \"b/Beamfile\" as x\nbeam all { run \"echo ok\" }",
    );
    write(dir.path().join("a/Beamfile"), "beam one { run \"echo 1\" }");
    write(dir.path().join("b/Beamfile"), "beam two { run \"echo 2\" }");

    let err = load_project(&dir.path().join("Beamfile")).unwrap_err();

    assert!(err.error.message.contains("duplicate import alias"));
}

#[test]
fn missing_import_reports_span_and_importing_source_id() {
    let dir = tempfile::tempdir().unwrap();
    write(
        dir.path().join("Beamfile"),
        "import \"missing/Beamfile\" as m\nbeam all { run \"echo ok\" }",
    );

    let err = load_project(&dir.path().join("Beamfile")).unwrap_err();

    // The root file is always assigned SourceId 0; the missing import is
    // reported against the *importing* file, i.e. the root.
    assert_eq!(err.error.source_id, SourceId(0));
    // The span points at the import's path text, not the start of the file.
    assert!(err.error.span.expect("the import path has a span").start > 0);
    // The root source is still available for rendering even though
    // loading failed on one of its imports.
    assert!(err.sources.get(err.error.source_id).is_some());
}

#[test]
fn beam_dir_is_its_defining_files_directory() {
    let dir = tempfile::tempdir().unwrap();
    write(
        dir.path().join("Beamfile"),
        "import \"api/Beamfile\" as api\nbeam all { run \"echo ok\" }",
    );
    write(
        dir.path().join("api/Beamfile"),
        "beam build { run \"echo b\" }",
    );

    let (project, _) = load_project(&dir.path().join("Beamfile")).unwrap();

    // `dir` is the *display* path (see `loader.rs`'s module doc comment):
    // exactly what was passed/joined, never run through `canonicalize`.
    // This matters beyond tidiness — on Windows, `canonicalize`'s verbatim
    // (`\\?\`) output can't be joined onto for further relative paths (a
    // beam's `cwd`), so `dir` must never be derived from it.
    let build = project
        .beams
        .iter()
        .find(|b| b.id.0 == "api:build")
        .unwrap();
    assert_eq!(build.dir, dir.path().join("api"));
    let all = project.beams.iter().find(|b| b.id.0 == "all").unwrap();
    assert_eq!(all.dir, dir.path());
}

#[test]
fn nested_relative_imports_avoid_verbatim_windows_paths() {
    // Regression guard for a bug where `dir` (the join base for further
    // imports, and a beam's `cwd` base) was derived from
    // `std::fs::canonicalize`'s output. On Windows that's a `\\?\`
    // verbatim path, and the standard library passes verbatim paths to
    // Win32 completely unnormalized, so joining a further relative import
    // (especially one using `..`) onto it silently fails to resolve. This
    // can't reproduce the Windows failure on this platform, but it does
    // pin down the two things that matter: importing through a
    // multi-segment relative path *and* a `..` still succeeds, and no
    // `Beam::dir` ever contains the verbatim prefix.
    let dir = tempfile::tempdir().unwrap();
    write(
        dir.path().join("Beamfile"),
        "import \"a/b/Beamfile\" as ab\nbeam all { run \"echo ok\" }",
    );
    write(
        dir.path().join("a/b/Beamfile"),
        "import \"../../back/Beamfile\" as back\nbeam here { run \"echo h\" }",
    );
    write(
        dir.path().join("back/Beamfile"),
        "beam there { run \"echo t\" }",
    );

    let (project, _) = load_project(&dir.path().join("Beamfile")).unwrap();

    let ids: Vec<&str> = project.beams.iter().map(|b| b.id.0.as_str()).collect();
    assert!(ids.contains(&"ab:here") && ids.contains(&"ab:back:there"));
    for beam in &project.beams {
        let dir_str = beam.dir.to_string_lossy();
        assert!(
            !dir_str.starts_with(r"\\?\"),
            "beam `{}`'s dir must not be a verbatim path, got {dir_str}",
            beam.id.0
        );
    }
}

#[test]
fn source_map_tracks_path_and_text_per_source_id() {
    let dir = tempfile::tempdir().unwrap();
    let root_src = "import \"api/Beamfile\" as api\nbeam all { run \"echo ok\" }";
    let api_src = "beam build { run \"echo b\" }";
    write(dir.path().join("Beamfile"), root_src);
    write(dir.path().join("api/Beamfile"), api_src);

    let (project, sources) = load_project(&dir.path().join("Beamfile")).unwrap();

    let build = project
        .beams
        .iter()
        .find(|b| b.id.0 == "api:build")
        .unwrap();
    let (_, text) = sources.get(build.source).unwrap();
    assert_eq!(text, api_src);

    let all = project.beams.iter().find(|b| b.id.0 == "all").unwrap();
    let (path, text) = sources.get(all.source).unwrap();
    assert_eq!(text, root_src);
    assert!(path.ends_with("Beamfile"));
}

fn git(dir: &Path, args: &[&str]) {
    let output = std::process::Command::new("git")
        .current_dir(dir)
        .args(args)
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "git {args:?} failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
}

/// A repository with one commit holding `src/lib.rs` and `README.md`.
fn repository() -> tempfile::TempDir {
    let dir = tempfile::tempdir().unwrap();
    git(dir.path(), &["init", "-q", "-b", "main"]);
    git(dir.path(), &["config", "user.email", "alba@example.com"]);
    git(dir.path(), &["config", "user.name", "Alba"]);
    git(dir.path(), &["config", "commit.gpgsign", "false"]);
    std::fs::create_dir(dir.path().join("src")).unwrap();
    std::fs::write(dir.path().join("src/lib.rs"), "fn a() {}").unwrap();
    std::fs::write(dir.path().join("README.md"), "# x").unwrap();
    git(dir.path(), &["add", "-A"]);
    git(dir.path(), &["commit", "-q", "-m", "init"]);
    dir
}

#[test]
fn git_fields_evaluate_from_the_repository_holding_the_beamfile() {
    let dir = repository();
    write(
        dir.path().join("Beamfile"),
        "let tag = git.branch + \"-\" + git.short_sha\n\
         beam b { description \"{tag} {if git.dirty then 'dirty' else 'clean'}\" run \"echo {git.sha}\" }",
    );
    // The Beamfile itself is untracked, so the tree is dirty.
    let (project, _) = load_project(&dir.path().join("Beamfile")).unwrap();
    let description = project.beams[0].description.as_deref().unwrap();
    assert!(description.starts_with("main-"), "{description}");
    assert!(description.ends_with(" dirty"), "{description}");
    let sha =
        alba_core::render_template(&project.beams[0].run[0], &project.beams[0].scope).unwrap();
    assert_eq!(sha.len(), "echo ".len() + 40, "{sha}");
    // Not just the prefix and suffix: the `short_sha` in between must
    // actually look like one (non-empty, hexadecimal), so a formatting
    // typo in between "main-" and " dirty" would still be caught.
    let short_sha = description
        .strip_prefix("main-")
        .and_then(|rest| rest.strip_suffix(" dirty"))
        .unwrap_or_else(|| panic!("unexpected description shape: {description}"));
    assert!(!short_sha.is_empty(), "{description}");
    assert!(
        short_sha.chars().all(|c| c.is_ascii_hexdigit()),
        "{description}"
    );
}

#[test]
fn git_is_only_spawned_when_an_expression_reads_it() {
    // Not a repository at all: loading must still succeed.
    let dir = tempfile::tempdir().unwrap();
    write(dir.path().join("Beamfile"), "beam b { run \"echo plain\" }");
    load_project(&dir.path().join("Beamfile")).unwrap();
}

#[test]
fn git_behind_a_statically_known_untaken_branch_is_never_spawned() {
    // Not a repository at all: if `check_expr` evaluated `git.branch`
    // eagerly while validating the untaken `else` branch of a
    // statically-known `if`, this load would fail before the beam ever
    // ran. `run` is deferred to schedule time, so nothing here ever calls
    // `eval_expr` on it either; only `check_expr`'s load-time pass touches
    // this expression.
    let dir = tempfile::tempdir().unwrap();
    write(
        dir.path().join("Beamfile"),
        "beam x { run \"{if true then 'a' else git.branch}\" }",
    );
    load_project(&dir.path().join("Beamfile")).unwrap();
}

#[test]
fn reading_git_outside_a_repository_points_at_the_expression() {
    let dir = tempfile::tempdir().unwrap();
    write(
        dir.path().join("Beamfile"),
        "let b = git.branch\nbeam x { run \"echo\" }",
    );
    let err = load_project(&dir.path().join("Beamfile")).unwrap_err();
    assert!(
        err.error.message.contains("cannot read `git.branch`"),
        "{}",
        err.error.message
    );
    let source = "let b = git.branch";
    let span = err.error.span.unwrap();
    assert_eq!(&source[span.start..span.end], "git.branch");
}

#[test]
fn an_unknown_git_field_is_rejected_without_spawning_git() {
    let dir = tempfile::tempdir().unwrap();
    write(
        dir.path().join("Beamfile"),
        "let b = git.tag\nbeam x { run \"echo\" }",
    );
    let err = load_project(&dir.path().join("Beamfile")).unwrap_err();
    assert!(
        err.error.message.contains("unknown git field `tag`"),
        "{}",
        err.error.message
    );
    assert!(err.error.help.as_deref().unwrap().contains("short_sha"));
}

#[test]
fn only_git_has_fields() {
    let dir = tempfile::tempdir().unwrap();
    write(
        dir.path().join("Beamfile"),
        "let a = \"x\"\nlet b = a.len\nbeam x { run \"echo\" }",
    );
    let err = load_project(&dir.path().join("Beamfile")).unwrap_err();
    assert!(
        err.error.message.contains("`a` has no fields"),
        "{}",
        err.error.message
    );
}

#[test]
fn git_dirty_is_a_boolean_usable_in_a_condition() {
    let dir = repository();
    write(
        dir.path().join("Beamfile"),
        "let flag = git.dirty == true\nbeam x { run \"echo {flag}\" }",
    );
    let (project, _) = load_project(&dir.path().join("Beamfile")).unwrap();
    let rendered =
        alba_core::render_template(&project.beams[0].run[0], &project.beams[0].scope).unwrap();
    assert_eq!(rendered, "echo true");
}

#[test]
fn hooks_load_with_their_target_and_arity() {
    let dir = tempfile::tempdir().unwrap();
    write(
        dir.path().join("Beamfile"),
        "import \"api/Beamfile\" as api\n\
         hook pre-commit { beam check }\n\
         hook commit-msg { beam api:lint_message }\n\
         beam check { run \"x\" }",
    );
    write(
        dir.path().join("api/Beamfile"),
        "hook pre-push { beam lint_message }\n\
         beam lint_message(path) { run \"lint {path}\" }",
    );
    let (project, _) = load_project(&dir.path().join("Beamfile")).unwrap();
    let hooks: Vec<(&str, &str, usize)> = project
        .hooks
        .iter()
        .map(|hook| (hook.name.as_str(), hook.beam.value.0.as_str(), hook.arity))
        .collect();
    // The import's own `pre-push` is ignored: only the root declares hooks.
    assert_eq!(
        hooks,
        [
            ("pre-commit", "check", 0),
            ("commit-msg", "api:lint_message", 1)
        ]
    );
}

#[test]
fn an_unknown_hook_name_is_rejected_with_a_suggestion() {
    let dir = tempfile::tempdir().unwrap();
    write(
        dir.path().join("Beamfile"),
        "hook pre-comit { beam check }\nbeam check { run \"x\" }",
    );
    let err = load_project(&dir.path().join("Beamfile")).unwrap_err();
    assert!(
        err.error.message.contains("unknown git hook `pre-comit`"),
        "{}",
        err.error.message
    );
    assert!(err.error.help.as_deref().unwrap().contains("pre-commit"));
}

#[test]
fn a_duplicate_hook_is_rejected() {
    let dir = tempfile::tempdir().unwrap();
    write(
        dir.path().join("Beamfile"),
        "hook pre-commit { beam a }\nhook pre-commit { beam b }\nbeam a { run \"x\" }\nbeam b { run \"x\" }",
    );
    let err = load_project(&dir.path().join("Beamfile")).unwrap_err();
    assert!(
        err.error.message.contains("duplicate hook `pre-commit`"),
        "{}",
        err.error.message
    );
}

#[test]
fn a_hook_naming_an_unknown_beam_is_rejected_at_the_reference() {
    let dir = tempfile::tempdir().unwrap();
    write(
        dir.path().join("Beamfile"),
        "hook pre-commit { beam chekc }\nbeam check { run \"x\" }",
    );
    let err = load_project(&dir.path().join("Beamfile")).unwrap_err();
    assert!(
        err.error.message.contains("unknown beam `chekc`"),
        "{}",
        err.error.message
    );
    let source = std::fs::read_to_string(dir.path().join("Beamfile")).unwrap();
    let span = err.error.span.unwrap();
    assert_eq!(&source[span.start..span.end], "chekc");
}

#[test]
fn a_hook_target_with_too_many_parameters_is_rejected() {
    let dir = tempfile::tempdir().unwrap();
    write(
        dir.path().join("Beamfile"),
        "hook commit-msg { beam lint }\nbeam lint(path, extra) { run \"x {path} {extra}\" }",
    );
    let err = load_project(&dir.path().join("Beamfile")).unwrap_err();
    assert_eq!(
        err.error.message,
        "hook `commit-msg` passes 1 argument, but beam `lint` declares 2 parameters"
    );
}

#[test]
fn a_hook_target_with_fewer_parameters_than_arguments_is_fine() {
    let dir = tempfile::tempdir().unwrap();
    write(
        dir.path().join("Beamfile"),
        "hook pre-push { beam lint }\nbeam lint { run \"x\" }",
    );
    load_project(&dir.path().join("Beamfile")).unwrap();
}

#[test]
fn an_invalid_hook_in_an_imported_file_is_ignored_not_rejected() {
    // An import's `hook` declarations are ignored exactly like its
    // `default` — including when they would themselves be load errors
    // (an unknown name, a duplicate). The root here declares no hooks at
    // all, so the load must still succeed with an empty `Project::hooks`.
    let dir = tempfile::tempdir().unwrap();
    write(
        dir.path().join("Beamfile"),
        "import \"api/Beamfile\" as api\nbeam all { run \"echo ok\" }",
    );
    write(
        dir.path().join("api/Beamfile"),
        "hook pre-comit { beam build }\n\
         hook pre-commit { beam build }\n\
         hook pre-commit { beam build }\n\
         beam build { run \"echo b\" }",
    );

    let (project, _) = load_project(&dir.path().join("Beamfile")).unwrap();

    assert!(project.hooks.is_empty());
}