protofetch 0.1.18

A source dependency management tool for Protobuf.
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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
//! End-to-end fetch tests using the `file://` git protocol.

mod infra;

use indoc::indoc;
use infra::TestWorld;
use insta::assert_snapshot;
use protofetch::LockMode;
use toml::toml;

/// Fetch a single dependency with one proto file and assert the output tree.
#[test]
fn fetch_single_file_dep() {
    let mut world = TestWorld::new();

    world.create_repo(
        "org/repo1",
        &[(
            "proto/hello.proto",
            indoc! {r#"
                syntax = "proto3";
                message Hello {}
            "#},
        )],
    );

    let result = world.fetch(toml! {
        name = "e2e-test"

        [repo1]
        url = "org/repo1"
        branch = "main"
    });

    assert_snapshot!("single_file_dep_output", result.snapshot_tree());
    assert_snapshot!("single_file_dep_lockfile", result.snapshot_lockfile());
}

/// Two direct deps; repo2 has a transitive dep on repo1 at a different commit.
///
/// repo1 main (commit1): proto/v1.proto
/// repo1 v2   (commit2): proto/v1.proto + proto/v2.proto
/// repo2 main (commit1): proto/b.proto  +  protofetch.toml → repo1@v2
///
/// Main manifest: repo1@main (commit1) + repo2@main.
/// Verifies which commit wins for the shared transitive dep.
#[test]
fn fetch_two_repos_transitive_dep() {
    let mut world = TestWorld::new();

    // v2 branch branches from commit1, adds v2.proto (so it has both v1 and v2)
    world
        .create_repo(
            "org/repo1",
            &[(
                "proto/v1.proto",
                indoc! {r#"
                    syntax = "proto3";
                    message V1 {}
                "#},
            )],
        )
        .add_commit(
            "v2",
            &[(
                "proto/v2.proto",
                indoc! {r#"
                    syntax = "proto3";
                    message V2 {}
                "#},
            )],
        );

    world.create_repo(
        "org/repo2",
        &[
            (
                "protofetch.toml",
                indoc! {r#"
                    name = "repo2"

                    [repo1]
                    url = "<base>/org/repo1"
                    protocol = "file"
                    branch = "v2"
                "#},
            ),
            (
                "proto/b.proto",
                indoc! {r#"
                    syntax = "proto3";
                    message B {}
                "#},
            ),
        ],
    );

    let result = world.fetch(toml! {
        name = "e2e-test"

        [repo1]
        url = "org/repo1"
        branch = "main"

        [repo2]
        url = "org/repo2"
        branch = "main"
    });

    assert_snapshot!("two_repos_output", result.snapshot_tree());
    assert_snapshot!("two_repos_lockfile", result.snapshot_lockfile());
}

/// repo1 is never listed in the main manifest — it only appears as a
/// transitive dep via repo2's own protofetch.toml.  Its protos must still
/// end up in the output.
#[test]
fn fetch_transitive_dep_only() {
    let mut world = TestWorld::new();

    world.create_repo(
        "org/repo1",
        &[(
            "proto/a.proto",
            indoc! {r#"
                syntax = "proto3";
                message A {}
            "#},
        )],
    );

    world.create_repo(
        "org/repo2",
        &[
            (
                "protofetch.toml",
                indoc! {r#"
                    name = "repo2"

                    [repo1]
                    url = "<base>/org/repo1"
                    protocol = "file"
                    branch = "main"
                "#},
            ),
            (
                "proto/b.proto",
                indoc! {r#"
                    syntax = "proto3";
                    message B {}
                "#},
            ),
        ],
    );

    let result = world.fetch(toml! {
        name = "e2e-test"

        [repo2]
        url = "org/repo2"
        branch = "main"
    });

    assert_snapshot!("transitive_only_output", result.snapshot_tree());
    assert_snapshot!("transitive_only_lockfile", result.snapshot_lockfile());
}

/// A pre-existing lock file pins repo1 to commit1.  The branch has since
/// advanced to commit2.  LockMode::Locked must use commit1 and not pull
/// in the new files.
#[test]
fn fetch_locked_mode_uses_pinned_commit() {
    let mut world = TestWorld::new();

    world
        .create_repo(
            "org/repo1",
            &[(
                "proto/v1.proto",
                indoc! {r#"
                    syntax = "proto3";
                    message V1 {}
                "#},
            )],
        )
        .add_commit(
            "main",
            &[(
                "proto/v2.proto",
                indoc! {r#"
                    syntax = "proto3";
                    message V2 {}
                "#},
            )],
        );

    let result = world.fetch_with_initial_lock(
        toml! {
            name = "e2e-test"

            [repo1]
            url = "org/repo1"
            branch = "main"
        },
        LockMode::Locked,
        toml! {
            version = 2

            [[dependencies]]
            name = "repo1"
            url = "<base>/org/repo1"
            protocol = "file"
            branch = "main"
            commit_hash = "<commit:main:1>"
        },
    );

    // Output must contain only v1.proto — locked to commit1
    assert_snapshot!("locked_mode_output", result.snapshot_tree());
    assert_snapshot!("locked_mode_lockfile", result.snapshot_lockfile());
}

/// Only files under directories matching allow_policies are copied.
/// `allow_policies = ["public/*"]` is a Prefix policy: includes everything
/// under `public/` and excludes everything else.
#[test]
fn fetch_allow_policies() {
    let mut world = TestWorld::new();

    world.create_repo(
        "org/repo1",
        &[
            (
                "public/service.proto",
                indoc! {r#"
                    syntax = "proto3";
                    message Service {}
                "#},
            ),
            (
                "internal/admin.proto",
                indoc! {r#"
                    syntax = "proto3";
                    message Admin {}
                "#},
            ),
        ],
    );

    let result = world.fetch(toml! {
        name = "e2e-test"

        [repo1]
        url = "org/repo1"
        branch = "main"
        allow_policies = ["public/*"]
    });

    // Only public/service.proto; internal/admin.proto is excluded
    assert_snapshot!("allow_policies_output", result.snapshot_tree());
}

/// With content_roots = ["api/proto"] files under api/proto/ appear without
/// that prefix in the output; files outside the root keep their original path.
#[test]
fn fetch_content_roots() {
    let mut world = TestWorld::new();

    world.create_repo(
        "org/repo1",
        &[
            (
                "api/proto/service.proto",
                indoc! {r#"
                    syntax = "proto3";
                    message Service {}
                "#},
            ),
            (
                "api/proto/model.proto",
                indoc! {r#"
                    syntax = "proto3";
                    message Model {}
                "#},
            ),
            (
                "internal/secret.proto",
                indoc! {r#"
                    syntax = "proto3";
                    message Secret {}
                "#},
            ),
        ],
    );

    let result = world.fetch(toml! {
        name = "e2e-test"

        [repo1]
        url = "org/repo1"
        branch = "main"
        content_roots = ["api/proto"]
    });

    // service.proto and model.proto appear without the api/proto prefix;
    // internal/secret.proto keeps its original path
    assert_snapshot!("content_roots_output", result.snapshot_tree());
}

/// deny_policies is the mirror of allow_policies: matching files are excluded.
/// `deny_policies = ["internal/*"]` removes everything under internal/.
#[test]
fn fetch_deny_policies() {
    let mut world = TestWorld::new();

    world.create_repo(
        "org/repo1",
        &[
            (
                "public/service.proto",
                indoc! {r#"
                    syntax = "proto3";
                    message Service {}
                "#},
            ),
            (
                "internal/admin.proto",
                indoc! {r#"
                    syntax = "proto3";
                    message Admin {}
                "#},
            ),
        ],
    );

    let result = world.fetch(toml! {
        name = "e2e-test"

        [repo1]
        url = "org/repo1"
        branch = "main"
        deny_policies = ["internal/*"]
    });

    // internal/admin.proto excluded; public/service.proto kept
    assert_snapshot!("deny_policies_output", result.snapshot_tree());
}

/// prune = true walks the import graph starting from the dep's own files and
/// only includes transitive-dep files that are actually imported.  extra.proto
/// in repo1 is never imported so it must be absent from the output.
#[test]
fn fetch_prune() {
    let mut world = TestWorld::new();

    // repo1: two files; only imported.proto is imported by repo2
    world.create_repo(
        "org/repo1",
        &[
            (
                "imported.proto",
                indoc! {r#"
                    syntax = "proto3";
                    message Imported {}
                "#},
            ),
            (
                "extra.proto",
                indoc! {r#"
                    syntax = "proto3";
                    message Extra {}
                "#},
            ),
        ],
    );

    world.create_repo(
        "org/repo2",
        &[
            (
                "protofetch.toml",
                indoc! {r#"
                    name = "repo2"

                    [repo1]
                    url = "<base>/org/repo1"
                    protocol = "file"
                    branch = "main"
                "#},
            ),
            (
                "service.proto",
                indoc! {r#"
                    syntax = "proto3";
                    import "imported.proto";
                    message Service {}
                "#},
            ),
        ],
    );

    let result = world.fetch(toml! {
        name = "e2e-test"

        [repo2]
        url = "org/repo2"
        branch = "main"
        prune = true
    });

    // service.proto + imported.proto are in the import chain; extra.proto is not
    assert_snapshot!("prune_output", result.snapshot_tree());
}

/// `revision = "<hash>"` in the manifest pins to that exact commit.
/// After pinning to commit1 the branch advances to commit2;
/// only commit1's files must appear in the output.
#[test]
fn fetch_revision_pin() {
    let mut world = TestWorld::new();

    world
        .create_repo(
            "org/repo1",
            &[(
                "proto/v1.proto",
                indoc! {r#"
                    syntax = "proto3";
                    message V1 {}
                "#},
            )],
        )
        .add_commit(
            "main",
            &[(
                "proto/v2.proto",
                indoc! {r#"
                    syntax = "proto3";
                    message V2 {}
                "#},
            )],
        );

    let result = world.fetch(toml! {
        name = "e2e-test"

        [repo1]
        url = "org/repo1"
        revision = "<commit:main:1>"
    });

    // Only v1.proto — pinned to commit1 before v2.proto was added
    assert_snapshot!("revision_pin_output", result.snapshot_tree());
    assert_snapshot!("revision_pin_lockfile", result.snapshot_lockfile());
}

/// `transitive = true` on a dep makes it visible as a transitive dep for the
/// prune import-graph walk of *all other* deps, even if those deps do not list
/// it in their own `protofetch.toml`.
///
/// repo_a uses `prune = true` and its `a.proto` imports `shared.proto` from
/// repo_shared.  repo_a has no `protofetch.toml`, so without `transitive = true`
/// on repo_shared the import could not be resolved.
#[test]
fn fetch_transitive_flag() {
    let mut world = TestWorld::new();

    world.create_repo(
        "org/repo_shared",
        &[(
            "shared.proto",
            indoc! {r#"
                syntax = "proto3";
                message Shared {}
            "#},
        )],
    );

    world.create_repo(
        "org/repo_a",
        &[(
            "a.proto",
            indoc! {r#"
                syntax = "proto3";
                import "shared.proto";
                message A {}
            "#},
        )],
    );

    let result = world.fetch(toml! {
        name = "e2e-test"

        [repo_a]
        url = "org/repo_a"
        branch = "main"
        prune = true

        [repo_shared]
        url = "org/repo_shared"
        branch = "main"
        transitive = true
    });

    assert_snapshot!("transitive_flag_output", result.snapshot_tree());
    assert_snapshot!("transitive_flag_lockfile", result.snapshot_lockfile());
}

/// Regex allow policy: `re://service` matches any path whose string
/// representation contains "service", using the full regex engine.
#[test]
fn fetch_regex_policy() {
    let mut world = TestWorld::new();

    world.create_repo(
        "org/repo1",
        &[
            (
                "service.proto",
                indoc! {r#"
                    syntax = "proto3";
                    message Service {}
                "#},
            ),
            (
                "internal.proto",
                indoc! {r#"
                    syntax = "proto3";
                    message Internal {}
                "#},
            ),
        ],
    );

    let result = world.fetch(toml! {
        name = "e2e-test"

        [repo1]
        url = "org/repo1"
        branch = "main"
        allow_policies = ["re://service"]
    });

    // Only service.proto matches the regex; internal.proto is excluded
    assert_snapshot!("regex_policy_output", result.snapshot_tree());
}

/// LockMode::Update with a pre-existing lock is a partial update:
/// deps already in the lock keep their pinned commit even if the branch
/// has advanced, while deps new to the manifest are resolved fresh.
///
/// Initial lock: repo1 @ commit1.
/// Branch advances to commit2.
/// Manifest adds repo2.
/// After update: repo1 still @ commit1, repo2 @ its head.
#[test]
fn fetch_partial_lock_update() {
    let mut world = TestWorld::new();

    world
        .create_repo(
            "org/repo1",
            &[(
                "proto/v1.proto",
                indoc! {r#"
                    syntax = "proto3";
                    message V1 {}
                "#},
            )],
        )
        .add_commit(
            "main",
            &[(
                "proto/v2.proto",
                indoc! {r#"
                    syntax = "proto3";
                    message V2 {}
                "#},
            )],
        );

    world.create_repo(
        "org/repo2",
        &[(
            "proto/b.proto",
            indoc! {r#"
                syntax = "proto3";
                message B {}
            "#},
        )],
    );

    let result = world.fetch_with_initial_lock(
        toml! {
            name = "e2e-test"

            [repo1]
            url = "org/repo1"
            branch = "main"

            [repo2]
            url = "org/repo2"
            branch = "main"
        },
        LockMode::Update,
        toml! {
            version = 2

            [[dependencies]]
            name = "repo1"
            url = "<base>/org/repo1"
            protocol = "file"
            branch = "main"
            commit_hash = "<commit:main:1>"
        },
    );

    // repo1 stays at commit1 (v1.proto only); repo2 resolved to its head (b.proto)
    assert_snapshot!("partial_update_output", result.snapshot_tree());
    assert_snapshot!("partial_update_lockfile", result.snapshot_lockfile());
}

/// When the same dep is declared with `prune = true` in the root manifest but also
/// appears as a dependency of another dep that does not use prune, all files from
/// the shared transitive dep are included — not just those reachable via the import
/// graph.
///
/// Dependency graph:
///   root → dep_mixed (prune=true) → dep_inner
///   root → dep_ref   (no prune)   → dep_mixed
///
/// dep_inner has `imported.proto` (imported by dep_mixed's service.proto) and
/// `extra.proto` (not imported by anything).  Because dep_ref also depends on
/// dep_mixed without prune, the prune restriction on dep_mixed is lifted and all
/// of dep_inner's files must appear in the output.
#[test]
fn fetch_prune_mixed_true_and_false_rules() {
    let mut world = TestWorld::new();

    world.create_repo(
        "org/dep_inner",
        &[
            (
                "imported.proto",
                indoc! {r#"
                    syntax = "proto3";
                    message Imported {}
                "#},
            ),
            (
                "extra.proto",
                indoc! {r#"
                    syntax = "proto3";
                    message Extra {}
                "#},
            ),
        ],
    );

    world.create_repo(
        "org/dep_mixed",
        &[
            (
                "protofetch.toml",
                indoc! {r#"
                    name = "dep_mixed"

                    [dep_inner]
                    url = "<base>/org/dep_inner"
                    protocol = "file"
                    branch = "main"
                "#},
            ),
            (
                "service.proto",
                indoc! {r#"
                    syntax = "proto3";
                    import "imported.proto";
                    message Service {}
                "#},
            ),
        ],
    );

    // dep_ref depends on dep_mixed without prune, lifting the prune restriction.
    world.create_repo(
        "org/dep_ref",
        &[
            (
                "protofetch.toml",
                indoc! {r#"
                    name = "dep_ref"

                    [dep_mixed]
                    url = "<base>/org/dep_mixed"
                    protocol = "file"
                    branch = "main"
                "#},
            ),
            (
                "other.proto",
                indoc! {r#"
                    syntax = "proto3";
                    message Other {}
                "#},
            ),
        ],
    );

    let result = world.fetch(toml! {
        name = "e2e-test"

        [dep_mixed]
        url = "org/dep_mixed"
        branch = "main"
        prune = true

        [dep_ref]
        url = "org/dep_ref"
        branch = "main"
    });

    // extra.proto must appear: the prune restriction on dep_mixed is lifted by
    // dep_ref's non-pruned reference, so all of dep_inner's files are included.
    assert_snapshot!("prune_mixed_rules_output", result.snapshot_tree());
}

/// When the same dep is declared as `transitive = true` in the root manifest AND
/// also listed as a normal dependency in another dep's protofetch.toml, the dep's
/// files must still appear in the output.
///
/// Dependency graph:
///   root → dep_consumer (no prune) → shared
///   root → shared (transitive=true)
///
/// shared.proto must be in the output even though dep_consumer does not import it.
#[test]
fn fetch_transitive_flag_mixed_with_normal_transitive_dep() {
    let mut world = TestWorld::new();

    world.create_repo(
        "org/shared",
        &[(
            "shared.proto",
            indoc! {r#"
                syntax = "proto3";
                message Shared {}
            "#},
        )],
    );

    world.create_repo(
        "org/dep_consumer",
        &[
            (
                "protofetch.toml",
                indoc! {r#"
                    name = "dep_consumer"

                    [shared]
                    url = "<base>/org/shared"
                    protocol = "file"
                    branch = "main"
                "#},
            ),
            (
                "consumer.proto",
                indoc! {r#"
                    syntax = "proto3";
                    message Consumer {}
                "#},
            ),
        ],
    );

    let result = world.fetch(toml! {
        name = "e2e-test"

        [dep_consumer]
        url = "org/dep_consumer"
        branch = "main"

        [shared]
        url = "org/shared"
        branch = "main"
        transitive = true
    });

    assert_snapshot!("transitive_mixed_rules_output", result.snapshot_tree());
}

/// When the same dep appears both directly in the root manifest and transitively
/// via another dep, allow/deny policy sets from all occurrences must be unioned.
///
/// - root declares `shared` with `allow_policies = ["from_root/*"]`
/// - foo's protofetch.toml declares `shared` with `allow_policies = ["from_foo/*"]`
/// - root also declares `foo`
///
/// Without the fix (issue #183) only `from_root/` files would appear.
/// With the fix both subtrees must be present (union semantics).
#[test]
fn fetch_allow_policies_merged_across_duplicate_deps() {
    let mut world = TestWorld::new();

    world.create_repo(
        "org/shared",
        &[
            (
                "from_root/service.proto",
                indoc! {r#"
                    syntax = "proto3";
                    message Service {}
                "#},
            ),
            (
                "from_foo/model.proto",
                indoc! {r#"
                    syntax = "proto3";
                    message Model {}
                "#},
            ),
        ],
    );

    world.create_repo(
        "org/foo",
        &[
            (
                "protofetch.toml",
                indoc! {r#"
                    name = "foo"

                    [shared]
                    url = "<base>/org/shared"
                    protocol = "file"
                    branch = "main"
                    allow_policies = ["from_foo/*"]
                "#},
            ),
            (
                "foo.proto",
                indoc! {r#"
                    syntax = "proto3";
                    message Foo {}
                "#},
            ),
        ],
    );

    let result = world.fetch(toml! {
        name = "e2e-test"

        [shared]
        url = "org/shared"
        branch = "main"
        allow_policies = ["from_root/*"]

        [foo]
        url = "org/foo"
        branch = "main"
    });

    // Both subtrees must be present: from_root/ (root's policy) and from_foo/ (foo's policy)
    assert_snapshot!("allow_policies_merged_output", result.snapshot_tree());
}