mitts 0.3.21

Rust-first build and package tooling with a shared artifact cache
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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
/*
 * Mitts Build — Rust-first build and package tooling with shared artifact cache
 * Copyright (c) 2026 Alexander Panasenko
 *
 * Contact: alex@prod.codes
 * Author: https://prod.codes/about/
 * Project: https://github.com/alex09x/mitts
 * SPDX-License-Identifier: MIT OR Apache-2.0
 */

use mitts::adapter::rust::types::{BuildMode, PlannedCompilationUnit, RustBuildOptions};
use mitts::adapter::rust::unit_inputs::{
    compute_unit_coarse_inputs, is_file_in_scanned_pkg_sources, is_file_in_scanned_tree,
    is_file_in_unit_coarse_inputs, PackageInputsCacheEntry,
};
use mitts::adapter::RustAdapter;
use std::fs;
use std::path::Path;
use tempfile::TempDir;

fn setup_test_crate(root: &Path) {
    fs::create_dir_all(root.join("src")).unwrap();
    fs::create_dir_all(root.join("tests/support")).unwrap();
    fs::create_dir_all(root.join("assets")).unwrap();

    fs::write(
        root.join("Cargo.toml"),
        r#"[package]
name = "isolated_test_crate"
version = "0.1.0"
edition = "2021"

[lib]
path = "src/lib.rs"

[[test]]
name = "test_a"
path = "tests/a.rs"

[[test]]
name = "test_b"
path = "tests/b.rs"
"#,
    )
    .unwrap();

    fs::write(
        root.join("src/lib.rs"),
        r#"pub fn add(a: i32, b: i32) -> i32 {
    a + b
}
"#,
    )
    .unwrap();

    fs::write(
        root.join("tests/a.rs"),
        r#"#[test]
fn it_works_a() {
    assert_eq!(isolated_test_crate::add(1, 2), 3);
}
"#,
    )
    .unwrap();

    fs::write(
        root.join("tests/b.rs"),
        r#"#[test]
fn it_works_b() {
    assert_eq!(isolated_test_crate::add(2, 3), 5);
}
"#,
    )
    .unwrap();
}

fn is_action_cached(action: &mitts::core::action::Action) -> bool {
    action.description.starts_with("Restored")
}

#[test]
fn test_mutate_single_test_recompiles_only_that_test() {
    let cache_dir = TempDir::new().unwrap();
    let project_dir = TempDir::new().unwrap();
    setup_test_crate(project_dir.path());

    let opt = RustBuildOptions {
        manifest_path: Some(project_dir.path().join("Cargo.toml")),
        cache_dir: Some(cache_dir.path().to_path_buf()),
        target_dir: Some(project_dir.path().join("target")),
        mode: BuildMode::Test,
        all_tests: true,
        ..Default::default()
    };

    // 1. Initial Action Keys derivation
    let expl1 = RustAdapter::explain_plan(&opt).expect("explain initial plan");
    let keys1 = RustAdapter::derive_unit_action_keys(&opt, &expl1).expect("derive keys 1");

    let lib_id = expl1
        .units
        .iter()
        .find(|u| u.target_name == "isolated_test_crate")
        .unwrap()
        .unit_id();
    let test_a_id = expl1
        .units
        .iter()
        .find(|u| u.target_name == "test_a")
        .unwrap()
        .unit_id();
    let test_b_id = expl1
        .units
        .iter()
        .find(|u| u.target_name == "test_b")
        .unwrap()
        .unit_id();

    // 2. Mutate only tests/a.rs
    fs::write(
        project_dir.path().join("tests/a.rs"),
        r#"#[test]
fn it_works_a_mutated() {
    assert_eq!(isolated_test_crate::add(10, 20), 30);
}
"#,
    )
    .unwrap();

    // 3. Re-derive Action Keys after leaf test mutation
    let expl2 = RustAdapter::explain_plan(&opt).expect("explain second plan");
    let keys2 = RustAdapter::derive_unit_action_keys(&opt, &expl2).expect("derive keys 2");

    assert_eq!(
        keys1[&lib_id], keys2[&lib_id],
        "Library ActionKey must be completely unchanged when tests/a.rs is edited"
    );
    assert_eq!(
        keys1[&test_b_id], keys2[&test_b_id],
        "Sibling test_b ActionKey must be completely unchanged when tests/a.rs is edited"
    );
    assert_ne!(
        keys1[&test_a_id], keys2[&test_a_id],
        "Mutated test_a ActionKey must change"
    );
}

#[test]
fn test_mutate_shared_test_support_recompiles_only_dependent_tests() {
    let cache_dir = TempDir::new().unwrap();
    let project_dir = TempDir::new().unwrap();
    setup_test_crate(project_dir.path());

    // Add shared helper in tests/support/mod.rs
    fs::write(
        project_dir.path().join("tests/support/mod.rs"),
        r#"pub fn helper_val() -> i32 { 42 }
"#,
    )
    .unwrap();

    // test_a uses support helper
    fs::write(
        project_dir.path().join("tests/a.rs"),
        r#"mod support;
#[test]
fn it_works_a() {
    assert_eq!(support::helper_val(), 42);
}
"#,
    )
    .unwrap();

    let opt = RustBuildOptions {
        manifest_path: Some(project_dir.path().join("Cargo.toml")),
        cache_dir: Some(cache_dir.path().to_path_buf()),
        target_dir: Some(project_dir.path().join("target")),
        mode: BuildMode::Test,
        all_tests: true,
        ..Default::default()
    };

    // 1. Initial build: compiles lib and tests, publishing ActionManifest with extra inputs
    let (_, res1) = RustAdapter::build(&opt).expect("initial build");
    assert!(res1.success);

    // 2. Mutate tests/support/mod.rs
    fs::write(
        project_dir.path().join("tests/support/mod.rs"),
        r#"pub fn helper_val() -> i32 { 99 }
"#,
    )
    .unwrap();

    // 3. Second build: lib and test_b should be cached, test_a should be recompiled
    let (plan2, res2) = RustAdapter::build(&opt).expect("second build");
    assert!(res2.success);

    let plan_units = plan2.actions;
    let lib_action = plan_units.iter().find(|a| a.description.contains("lib"));
    let test_b_action = plan_units.iter().find(|a| a.description.contains("test_b"));
    let test_a_action = plan_units.iter().find(|a| a.description.contains("test_a"));

    if let Some(la) = lib_action {
        assert!(
            is_action_cached(la),
            "lib must remain cached when test support is mutated"
        );
    }
    if let Some(tb) = test_b_action {
        assert!(
            is_action_cached(tb),
            "test_b must remain cached when unreferenced support is mutated"
        );
    }
    if let Some(ta) = test_a_action {
        assert!(
            !is_action_cached(ta),
            "test_a must be recompiled when its support helper is mutated"
        );
    }
}

#[test]
fn test_mutate_path_attribute_file_in_tests_recompiles_lib_and_dependents() {
    let cache_dir = TempDir::new().unwrap();
    let project_dir = TempDir::new().unwrap();
    setup_test_crate(project_dir.path());

    // Place extra source file inside tests/
    fs::write(
        project_dir.path().join("tests/support/extra_calc.rs"),
        r#"pub fn calc() -> i32 { 100 }
"#,
    )
    .unwrap();

    // Include it in src/lib.rs via #[path]
    fs::write(
        project_dir.path().join("src/lib.rs"),
        r#"#[path = "../tests/support/extra_calc.rs"]
pub mod extra_calc;

pub fn add(a: i32, b: i32) -> i32 {
    a + b + extra_calc::calc()
}
"#,
    )
    .unwrap();

    let opt = RustBuildOptions {
        manifest_path: Some(project_dir.path().join("Cargo.toml")),
        cache_dir: Some(cache_dir.path().to_path_buf()),
        target_dir: Some(project_dir.path().join("target")),
        mode: BuildMode::Test,
        all_tests: true,
        ..Default::default()
    };

    // 1. Initial build: lib compiles and records extra_calc.rs in extra_inputs
    let (_, res1) = RustAdapter::build(&opt).expect("initial build");
    assert!(res1.success);

    // 2. Mutate the file inside tests/ that is referenced via #[path]
    fs::write(
        project_dir.path().join("tests/support/extra_calc.rs"),
        r#"pub fn calc() -> i32 { 200 }
"#,
    )
    .unwrap();

    // 3. Second build: lib must be recompiled because extra_calc.rs changed
    let (plan2, res2) = RustAdapter::build(&opt).expect("second build");
    assert!(res2.success);

    let plan_units = plan2.actions;
    let lib_action = plan_units.iter().find(|a| a.description.contains("lib"));
    if let Some(la) = lib_action {
        assert!(
            !is_action_cached(la),
            "lib must be recompiled when #[path] file in tests/ is mutated"
        );
    }
}

#[test]
fn test_mutate_lib_recompiles_lib_and_all_tests() {
    let cache_dir = TempDir::new().unwrap();
    let project_dir = TempDir::new().unwrap();
    setup_test_crate(project_dir.path());

    let opt = RustBuildOptions {
        manifest_path: Some(project_dir.path().join("Cargo.toml")),
        cache_dir: Some(cache_dir.path().to_path_buf()),
        target_dir: Some(project_dir.path().join("target")),
        mode: BuildMode::Test,
        all_tests: true,
        ..Default::default()
    };

    let expl1 = RustAdapter::explain_plan(&opt).expect("explain plan 1");
    let keys1 = RustAdapter::derive_unit_action_keys(&opt, &expl1).expect("derive keys 1");
    let lib_id = expl1
        .units
        .iter()
        .find(|u| u.target_name == "isolated_test_crate")
        .unwrap()
        .unit_id();
    let test_a_id = expl1
        .units
        .iter()
        .find(|u| u.target_name == "test_a")
        .unwrap()
        .unit_id();
    let test_b_id = expl1
        .units
        .iter()
        .find(|u| u.target_name == "test_b")
        .unwrap()
        .unit_id();

    // Mutate src/lib.rs
    fs::write(
        project_dir.path().join("src/lib.rs"),
        r#"pub fn add(a: i32, b: i32) -> i32 {
    a + b + 1
}
"#,
    )
    .unwrap();

    let expl2 = RustAdapter::explain_plan(&opt).expect("explain plan 2");
    let keys2 = RustAdapter::derive_unit_action_keys(&opt, &expl2).expect("derive keys 2");

    assert_ne!(
        keys1[&lib_id], keys2[&lib_id],
        "lib ActionKey must change when src/lib.rs is mutated"
    );
    assert_ne!(
        keys1[&test_a_id], keys2[&test_a_id],
        "test_a ActionKey must change when dependency lib changes"
    );
    assert_ne!(
        keys1[&test_b_id], keys2[&test_b_id],
        "test_b ActionKey must change when dependency lib changes"
    );
}

#[test]
fn test_mutate_included_str_recompiles_lib() {
    let cache_dir = TempDir::new().unwrap();
    let project_dir = TempDir::new().unwrap();
    setup_test_crate(project_dir.path());

    fs::write(project_dir.path().join("assets/data.txt"), "initial data\n").unwrap();
    fs::write(
        project_dir.path().join("src/lib.rs"),
        r#"pub fn add(a: i32, b: i32) -> i32 { a + b }
pub const DATA: &str = include_str!("../assets/data.txt");
pub fn get_data() -> &'static str {
    DATA
}
"#,
    )
    .unwrap();

    let opt = RustBuildOptions {
        manifest_path: Some(project_dir.path().join("Cargo.toml")),
        cache_dir: Some(cache_dir.path().to_path_buf()),
        target_dir: Some(project_dir.path().join("target")),
        mode: BuildMode::Test,
        all_tests: true,
        ..Default::default()
    };

    let (_, res1) = RustAdapter::build(&opt).expect("initial build");
    assert!(res1.success);

    // Mutate assets/data.txt
    fs::write(project_dir.path().join("assets/data.txt"), "updated data\n").unwrap();

    let (plan2, res2) = RustAdapter::build(&opt).expect("second build");
    assert!(res2.success);

    let plan_units = plan2.actions;
    let lib_action = plan_units.iter().find(|a| a.description.contains("lib"));
    if let Some(la) = lib_action {
        assert!(
            !is_action_cached(la),
            "lib must be recompiled when include_str! file is mutated"
        );
    }
}

#[test]
fn test_mutate_cargo_toml_recompiles_all_package_units() {
    let cache_dir = TempDir::new().unwrap();
    let project_dir = TempDir::new().unwrap();
    setup_test_crate(project_dir.path());

    let opt = RustBuildOptions {
        manifest_path: Some(project_dir.path().join("Cargo.toml")),
        cache_dir: Some(cache_dir.path().to_path_buf()),
        target_dir: Some(project_dir.path().join("target")),
        mode: BuildMode::Test,
        all_tests: true,
        ..Default::default()
    };

    let expl1 = RustAdapter::explain_plan(&opt).expect("explain plan 1");
    let keys1 = RustAdapter::derive_unit_action_keys(&opt, &expl1).expect("derive keys 1");
    let lib_id = expl1
        .units
        .iter()
        .find(|u| u.target_name == "isolated_test_crate")
        .unwrap()
        .unit_id();
    let test_a_id = expl1
        .units
        .iter()
        .find(|u| u.target_name == "test_a")
        .unwrap()
        .unit_id();
    let test_b_id = expl1
        .units
        .iter()
        .find(|u| u.target_name == "test_b")
        .unwrap()
        .unit_id();

    // Mutate Cargo.toml (add features section)
    fs::write(
        project_dir.path().join("Cargo.toml"),
        r#"[package]
name = "isolated_test_crate"
version = "0.1.0"
edition = "2021"

[features]
extra_feature = []

[lib]
path = "src/lib.rs"

[[test]]
name = "test_a"
path = "tests/a.rs"

[[test]]
name = "test_b"
path = "tests/b.rs"
"#,
    )
    .unwrap();

    let expl2 = RustAdapter::explain_plan(&opt).expect("explain plan 2");
    let keys2 = RustAdapter::derive_unit_action_keys(&opt, &expl2).expect("derive keys 2");

    assert_ne!(
        keys1[&lib_id], keys2[&lib_id],
        "lib ActionKey must change on Cargo.toml mutation"
    );
    assert_ne!(
        keys1[&test_a_id], keys2[&test_a_id],
        "test_a ActionKey must change on Cargo.toml mutation"
    );
    assert_ne!(
        keys1[&test_b_id], keys2[&test_b_id],
        "test_b ActionKey must change on Cargo.toml mutation"
    );
}

#[test]
fn test_mutate_build_rs_keeps_plan_time_unit_keys() {
    let cache_dir = TempDir::new().unwrap();
    let project_dir = TempDir::new().unwrap();
    setup_test_crate(project_dir.path());

    // Add build.rs
    fs::write(
        project_dir.path().join("build.rs"),
        r#"fn main() {
    println!("cargo:rerun-if-changed=build.rs");
}
"#,
    )
    .unwrap();

    let opt = RustBuildOptions {
        manifest_path: Some(project_dir.path().join("Cargo.toml")),
        cache_dir: Some(cache_dir.path().to_path_buf()),
        target_dir: Some(project_dir.path().join("target")),
        mode: BuildMode::Test,
        all_tests: true,
        ..Default::default()
    };

    let expl1 = RustAdapter::explain_plan(&opt).expect("explain plan 1");
    let keys1 = RustAdapter::derive_unit_action_keys(&opt, &expl1).expect("derive keys 1");
    let lib_id = expl1
        .units
        .iter()
        .find(|u| u.target_name == "isolated_test_crate")
        .unwrap()
        .unit_id();
    let test_a_id = expl1
        .units
        .iter()
        .find(|u| u.target_name == "test_a")
        .unwrap()
        .unit_id();
    let test_b_id = expl1
        .units
        .iter()
        .find(|u| u.target_name == "test_b")
        .unwrap()
        .unit_id();

    // Mutate build.rs
    fs::write(
        project_dir.path().join("build.rs"),
        r#"fn main() {
    println!("cargo:rustc-cfg=feature_foo");
}
"#,
    )
    .unwrap();

    let expl2 = RustAdapter::explain_plan(&opt).expect("explain plan 2");
    let keys2 = RustAdapter::derive_unit_action_keys(&opt, &expl2).expect("derive keys 2");

    // build.rs is not a rustc input of the package's units: they depend on what the
    // script emits (cfgs, env, link args, OUT_DIR), which joins their key once the
    // script has run. Plan-time keys therefore stay put; the script's own key moves.
    // End-to-end propagation is covered by tests/key_scope_test.rs.
    assert_eq!(keys1[&lib_id], keys2[&lib_id]);
    assert_eq!(keys1[&test_a_id], keys2[&test_a_id]);
    assert_eq!(keys1[&test_b_id], keys2[&test_b_id]);
}

#[test]
fn test_is_file_in_scanned_pkg_sources_excludes_subcrates_and_temporary_files() {
    let project_dir = TempDir::new().unwrap();
    let root = project_dir.path();

    // Normal files in root, src, assets
    fs::create_dir_all(root.join("src")).unwrap();
    fs::create_dir_all(root.join("assets")).unwrap();
    fs::write(root.join("src/lib.rs"), "pub fn f() {}").unwrap();
    fs::write(root.join("assets/data.txt"), "hello").unwrap();

    // Subcrate with its own Cargo.toml
    let subcrate_dir = root.join("subcrate");
    fs::create_dir_all(subcrate_dir.join("src")).unwrap();
    fs::write(
        subcrate_dir.join("Cargo.toml"),
        "[package]\nname = \"sub\"\nversion = \"0.1.0\"\n",
    )
    .unwrap();
    fs::write(subcrate_dir.join("src/lib.rs"), "pub fn sub() {}").unwrap();

    // Temporary files
    let tmp_file = root.join("src/lib.rs.tmp");
    let swp_file = root.join("src/.lib.rs.swp");
    let profraw_file = root.join("default.profraw");
    let git_file = root.join(".git/config");
    let lock_file = root.join("Cargo.lock");

    // Standard package sources must include src and assets
    assert!(is_file_in_scanned_pkg_sources(
        root,
        &root.join("src/lib.rs")
    ));
    assert!(is_file_in_scanned_pkg_sources(
        root,
        &root.join("assets/data.txt")
    ));

    // Standard package sources must exclude subcrates
    assert!(!is_file_in_scanned_pkg_sources(
        root,
        &subcrate_dir.join("src/lib.rs")
    ));
    assert!(!is_file_in_scanned_pkg_sources(
        root,
        &subcrate_dir.join("Cargo.toml")
    ));

    // Standard package sources must exclude temporary/VCS files
    assert!(!is_file_in_scanned_pkg_sources(root, &tmp_file));
    assert!(!is_file_in_scanned_pkg_sources(root, &swp_file));
    assert!(!is_file_in_scanned_pkg_sources(root, &profraw_file));
    assert!(!is_file_in_scanned_pkg_sources(root, &git_file));
    assert!(!is_file_in_scanned_pkg_sources(root, &lock_file));

    // Standard package sources must exclude test/bench/example dirs
    let test_file = root.join("tests/test1.rs");
    assert!(!is_file_in_scanned_pkg_sources(root, &test_file));

    // Scanned tree with allow_test_dirs=true includes test dir files, but still excludes subcrates & tmp files
    assert!(is_file_in_scanned_tree(root, &test_file, true));
    assert!(!is_file_in_scanned_tree(
        root,
        &subcrate_dir.join("src/lib.rs"),
        true
    ));
    assert!(!is_file_in_scanned_tree(root, &tmp_file, true));
}

#[test]
fn test_impure_proc_macro_coarse_inputs_includes_tests_benches_examples() {
    let project_dir = TempDir::new().unwrap();
    let root = project_dir.path();

    fs::create_dir_all(root.join("src")).unwrap();
    fs::create_dir_all(root.join("tests")).unwrap();
    fs::create_dir_all(root.join("benches")).unwrap();
    fs::create_dir_all(root.join("examples")).unwrap();

    fs::write(root.join("src/lib.rs"), "pub fn f() {}").unwrap();
    fs::write(root.join("tests/my_test.rs"), "fn t() {}").unwrap();
    fs::write(root.join("benches/my_bench.rs"), "fn b() {}").unwrap();
    fs::write(root.join("examples/my_example.rs"), "fn ex() {}").unwrap();
    // The project declares which macro reads package files; nothing is impure by default.
    fs::write(
        root.join("Cargo.toml"),
        "[package]\nname = \"app\"\nversion = \"0.1.0\"\n\n[package.metadata.mitts]\nimpure-proc-macros = [\"sqlx-macros\"]\n",
    )
    .unwrap();

    let pkg_entry = PackageInputsCacheEntry {
        common_inputs: vec![("Cargo.toml".to_string(), "h1".to_string())],
        src_inputs: vec![("src/lib.rs".to_string(), "h2".to_string())],
    };

    // 1. Normal unit without impure proc macro: coarse inputs only has common + src
    let normal_unit = PlannedCompilationUnit {
        kind: "lib".to_string(),
        src_path: root.join("src/lib.rs"),
        manifest_path: root.join("Cargo.toml"),
        ..Default::default()
    };
    let normal_coarse = compute_unit_coarse_inputs(&normal_unit, root, &pkg_entry).unwrap();
    let normal_paths: Vec<_> = normal_coarse.iter().map(|(p, _)| p.as_str()).collect();
    assert!(normal_paths.contains(&"src/lib.rs"));
    assert!(!normal_paths.iter().any(|p| p.starts_with("tests/")));
    assert!(!normal_paths.iter().any(|p| p.starts_with("benches/")));
    assert!(!normal_paths.iter().any(|p| p.starts_with("examples/")));
    assert!(!is_file_in_unit_coarse_inputs(
        &normal_unit,
        root,
        &root.join("tests/my_test.rs")
    ));

    // 2. Unit with a declared impure proc macro: coarse inputs includes tests/, benches/, examples/
    let impure_unit = PlannedCompilationUnit {
        kind: "lib".to_string(),
        src_path: root.join("src/lib.rs"),
        manifest_path: root.join("Cargo.toml"),
        dependencies: vec!["sqlx-macros@0.7.0::proc-macro:sqlx_macros:host".to_string()],
        ..Default::default()
    };
    let impure_coarse = compute_unit_coarse_inputs(&impure_unit, root, &pkg_entry).unwrap();
    let impure_paths: Vec<_> = impure_coarse.iter().map(|(p, _)| p.as_str()).collect();
    assert!(impure_paths.contains(&"src/lib.rs"));
    assert!(impure_paths.iter().any(|p| p.contains("my_test.rs")));
    assert!(impure_paths.iter().any(|p| p.contains("my_bench.rs")));
    assert!(impure_paths.iter().any(|p| p.contains("my_example.rs")));
    assert!(is_file_in_unit_coarse_inputs(
        &impure_unit,
        root,
        &root.join("tests/my_test.rs")
    ));

    // 3. Unit with an undeclared proc macro: pure by default, coarse inputs do NOT include tests/
    let pure_unit = PlannedCompilationUnit {
        kind: "lib".to_string(),
        src_path: root.join("src/lib.rs"),
        manifest_path: root.join("Cargo.toml"),
        dependencies: vec!["serde_derive@1.0.0::proc-macro:serde_derive:host".to_string()],
        ..Default::default()
    };
    let pure_coarse = compute_unit_coarse_inputs(&pure_unit, root, &pkg_entry).unwrap();
    let pure_paths: Vec<_> = pure_coarse.iter().map(|(p, _)| p.as_str()).collect();
    assert!(!pure_paths.iter().any(|p| p.starts_with("tests/")));
    assert!(!is_file_in_unit_coarse_inputs(
        &pure_unit,
        root,
        &root.join("tests/my_test.rs")
    ));

    // 4. Test unit with a declared impure proc macro: coarse inputs includes tests/, benches/, examples/
    fs::create_dir_all(root.join("tests/fixtures")).unwrap();
    fs::write(root.join("tests/fixtures/query.sql"), "SELECT 1;").unwrap();
    let test_unit_impure = PlannedCompilationUnit {
        kind: "test".to_string(),
        src_path: root.join("tests/my_test.rs"),
        manifest_path: root.join("Cargo.toml"),
        dependencies: vec!["sqlx-macros@0.7.0::proc-macro:sqlx_macros:host".to_string()],
        ..Default::default()
    };
    let test_coarse = compute_unit_coarse_inputs(&test_unit_impure, root, &pkg_entry).unwrap();
    let test_paths: Vec<_> = test_coarse.iter().map(|(p, _)| p.as_str()).collect();
    assert!(test_paths.iter().any(|p| p.contains("query.sql")));
    assert!(test_paths.contains(&"src/lib.rs"));
    assert!(is_file_in_unit_coarse_inputs(
        &test_unit_impure,
        root,
        &root.join("tests/fixtures/query.sql")
    ));
    assert!(is_file_in_unit_coarse_inputs(
        &test_unit_impure,
        root,
        &root.join("src/lib.rs")
    ));

    // 5. Test unit with an undeclared proc macro: coarse inputs do NOT include fixtures in other directories
    let test_unit_pure = PlannedCompilationUnit {
        kind: "test".to_string(),
        src_path: root.join("tests/my_test.rs"),
        manifest_path: root.join("Cargo.toml"),
        dependencies: vec!["serde_derive@1.0.0::proc-macro:serde_derive:host".to_string()],
        ..Default::default()
    };
    let test_pure_coarse = compute_unit_coarse_inputs(&test_unit_pure, root, &pkg_entry).unwrap();
    let test_pure_paths: Vec<_> = test_pure_coarse.iter().map(|(p, _)| p.as_str()).collect();
    assert!(!test_pure_paths.iter().any(|p| p.contains("query.sql")));
    assert!(!is_file_in_unit_coarse_inputs(
        &test_unit_pure,
        root,
        &root.join("tests/fixtures/query.sql")
    ));
}