prek 0.5.7

A fast Git hook manager written in Rust, designed as a drop-in alternative to pre-commit, reimagined.
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
use crate::common::{TestEnv, cmd_snapshot};

/// Test multiple hook IDs scenarios.
#[test]
fn multiple_hook_ids() {
    let context = TestEnv::new()
        .with_config(indoc::indoc! {r"
        repos:
          - repo: local
            hooks:
              - id: hook1
                name: First Hook
                language: system
                entry: echo hook1
              - id: hook2
                name: Second Hook
                language: system
                entry: echo hook2
              - id: shared-name
                name: Shared Hook A
                language: system
                entry: echo shared-a
              - id: shared-name-2
                name: Shared Hook B
                language: system
                entry: echo shared-b
                alias: shared-name
    "})
        .init_git();

    // Multiple repeated hook-id (should deduplicate)
    cmd_snapshot!(context, context.run().arg("hook1").arg("hook1").arg("hook1"), @r#"
    success: true
    exit_code: 0
    ----- stdout -----
    First Hook...............................................................Passed

    ----- stderr -----
    "#);

    // Hook-id that matches multiple hooks (by alias)
    cmd_snapshot!(context, context.run().arg("shared-name"), @r#"
    success: true
    exit_code: 0
    ----- stdout -----
    Shared Hook A............................................................Passed
    Shared Hook B............................................................Passed

    ----- stderr -----
    "#);

    // Hook-id matches nothing
    cmd_snapshot!(context, context.run().arg("nonexistent-hook"), @r"
    success: false
    exit_code: 1
    ----- stdout -----

    ----- stderr -----
    warning: selector `nonexistent-hook` did not match any hooks
    error: No hooks found after filtering with the given selectors
    ");

    // Multiple hook_ids match nothing
    cmd_snapshot!(context, context.run().arg("nonexistent-hook").arg("nonexistent-hook").arg("nonexistent-hook-2"), @r"
    success: false
    exit_code: 1
    ----- stdout -----

    ----- stderr -----
    warning: the following selectors did not match any hooks or projects:
      - `nonexistent-hook`
      - `nonexistent-hook-2`
    error: No hooks found after filtering with the given selectors
    ");

    // Hook-id matches one hook
    cmd_snapshot!(context, context.run().arg("hook2"), @r#"
    success: true
    exit_code: 0
    ----- stdout -----
    Second Hook..............................................................Passed

    ----- stderr -----
    "#);

    // Multiple hook-ids with mixed results (some exist, some don't)
    cmd_snapshot!(context, context.run().arg("hook1").arg("nonexistent").arg("hook2"), @r"
    success: true
    exit_code: 0
    ----- stdout -----
    First Hook...............................................................Passed
    Second Hook..............................................................Passed

    ----- stderr -----
    warning: selector `nonexistent` did not match any hooks
    ");

    // Multiple valid hook-ids
    cmd_snapshot!(context, context.run().arg("hook1").arg("hook2").arg("nonexistent-hook"), @r"
    success: true
    exit_code: 0
    ----- stdout -----
    First Hook...............................................................Passed
    Second Hook..............................................................Passed

    ----- stderr -----
    warning: selector `nonexistent-hook` did not match any hooks
    ");

    // Multiple hook-ids with some duplicates and aliases
    cmd_snapshot!(context, context.run().arg("hook1").arg("shared-name").arg("hook1"), @r#"
    success: true
    exit_code: 0
    ----- stdout -----
    First Hook...............................................................Passed
    Shared Hook A............................................................Passed
    Shared Hook B............................................................Passed

    ----- stderr -----
    "#);
}

#[test]
fn run_group_without_stage_selects_hooks_across_stages() {
    let context = TestEnv::new()
        .with_config(indoc::indoc! {r#"
        repos:
          - repo: local
            hooks:
              - id: ci-files
                name: CI Files
                language: system
                entry: python3 -c "print('ci-files')"
                always_run: true
                stages: [pre-commit]
                groups: [ci]
              - id: ci-check
                name: CI Check
                language: system
                entry: python3 -c "print('ci')"
                always_run: true
                stages: [pre-push]
                groups: [ci]
              - id: commit-msg-check
                name: Commit Msg Check
                language: system
                entry: python3 -c "raise SystemExit('commit-msg hook should not run')"
                always_run: true
                stages: [commit-msg]
                groups: [ci]
              - id: prepare-commit-msg-check
                name: Prepare Commit Msg Check
                language: system
                entry: python3 -c "raise SystemExit('prepare-commit-msg hook should not run')"
                always_run: true
                stages: [prepare-commit-msg]
                groups: [ci]
              - id: local-check
                name: Local Check
                language: system
                entry: python3 -c "print('local')"
                always_run: true
                stages: [pre-commit]
    "#})
        .init_git();

    cmd_snapshot!(context, context.run().arg("--all-files").arg("--group").arg("ci"), @r#"
    success: true
    exit_code: 0
    ----- stdout -----
    CI Files.................................................................Passed
    CI Check.................................................................Passed

    ----- stderr -----
    "#);
}

#[test]
fn run_ungrouped_group_selects_hooks_without_groups() {
    let context = TestEnv::new()
        .with_config(indoc::indoc! {r#"
        repos:
          - repo: local
            hooks:
              - id: ungrouped
                name: Ungrouped
                language: system
                entry: python3 -c "print('ungrouped')"
                always_run: true
              - id: ci
                name: CI
                language: system
                entry: python3 -c "print('ci')"
                always_run: true
                groups: [ci]
              - id: other
                name: Other
                language: system
                entry: python3 -c "print('other')"
                always_run: true
                groups: [other]

          - repo: https://notexistentatallnevergonnahappen.com/nonexistent/repo
            rev: v1.0.0
            hooks:
              - id: remote-other
                groups: [other]
    "#})
        .init_git();

    cmd_snapshot!(context,
        context
            .run()
            .arg("--all-files")
            .arg("--group")
            .arg("ci")
            .arg("--group")
            .arg("@ungrouped"),
        @r#"
    success: true
    exit_code: 0
    ----- stdout -----
    Ungrouped................................................................Passed
    CI.......................................................................Passed

    ----- stderr -----
    "#
    );
}

#[test]
fn run_required_group_without_stage_warns_when_only_message_file_hooks_match() {
    let context = TestEnv::new()
        .with_config(indoc::indoc! {r#"
        repos:
          - repo: local
            hooks:
              - id: commit-msg-check
                name: Commit Msg Check
                language: system
                entry: python3 -c "print('commit-msg')"
                always_run: true
                stages: [commit-msg]
                groups: [ci]
              - id: prepare-commit-msg-check
                name: Prepare Commit Msg Check
                language: system
                entry: python3 -c "print('prepare-commit-msg')"
                always_run: true
                stages: [prepare-commit-msg]
                groups: [ci]
    "#})
        .init_git();

    cmd_snapshot!(context, context.run().arg("--all-files").arg("--require-group").arg("ci"), @r#"
    success: false
    exit_code: 1
    ----- stdout -----

    ----- stderr -----
    warning: all hooks selected by group filters require `commit-msg` or `prepare-commit-msg` stage and were not run; pass `--stage commit-msg` or `--stage prepare-commit-msg` to run them
    "#);
}

#[test]
fn run_no_group_excludes_matching_hooks_and_keeps_ungrouped_hooks() {
    let context = TestEnv::new()
        .with_config(indoc::indoc! {r#"
        repos:
          - repo: local
            hooks:
              - id: format
                name: Format
                language: system
                entry: python3 -c "print('format')"
                always_run: true
                groups: [format]
              - id: lint
                name: Lint
                language: system
                entry: python3 -c "print('lint')"
                always_run: true
    "#})
        .init_git();

    cmd_snapshot!(context, context.run().arg("--all-files").arg("--no-group").arg("format"), @r#"
    success: true
    exit_code: 0
    ----- stdout -----
    Lint.....................................................................Passed

    ----- stderr -----
    "#);
}

#[test]
fn run_group_exclusion_wins_over_inclusion() {
    let context = TestEnv::new()
        .with_config(indoc::indoc! {r#"
        repos:
          - repo: local
            hooks:
              - id: fast-lint
                name: Fast Lint
                language: system
                entry: python3 -c "print('fast')"
                always_run: true
                groups: [ci]
              - id: slow-lint
                name: Slow Lint
                language: system
                entry: python3 -c "print('slow')"
                always_run: true
                groups: [ci, slow]
    "#})
        .init_git();

    cmd_snapshot!(context, context.run().arg("--all-files").arg("--group").arg("ci").arg("--no-group").arg("slow"), @r#"
    success: true
    exit_code: 0
    ----- stdout -----
    Fast Lint................................................................Passed

    ----- stderr -----
    "#);
}

#[test]
fn run_required_groups_intersect_and_compose_with_other_group_filters() {
    let context = TestEnv::new()
        .with_config(indoc::indoc! {r#"
        repos:
          - repo: local
            hooks:
              - id: ty
                name: Ty
                language: system
                entry: python3 -c "print('ty')"
                always_run: true
                groups: [lint, fast, local]
              - id: ruff-format
                name: Ruff Format
                language: system
                entry: python3 -c "print('ruff-format')"
                always_run: true
                groups: [format, fast, local]
              - id: mypy
                name: Mypy
                language: system
                entry: python3 -c "print('mypy')"
                always_run: true
                groups: [lint, slow, ci]
              - id: black
                name: Black
                language: system
                entry: python3 -c "print('black')"
                always_run: true
                groups: [format, slow, ci]
              - id: black-fast
                name: Black Fast
                language: system
                entry: python3 -c "print('black-fast')"
                always_run: true
                groups: [format, slow, ci, fast]
    "#})
        .init_git();

    cmd_snapshot!(context,
        context
            .run()
            .arg("--all-files")
            .arg("--no-group")
            .arg("fast")
            .arg("--require-group")
            .arg("format")
            .arg("--group")
            .arg("lint")
            .arg("--no-group")
            .arg("local")
            .arg("--require-group")
            .arg("slow")
            .arg("--group")
            .arg("ci"),
        @r"
    success: true
    exit_code: 0
    ----- stdout -----
    Black....................................................................Passed

    ----- stderr -----
    "
    );

    cmd_snapshot!(context,
        context
            .run()
            .arg("--all-files")
            .arg("--group")
            .arg("ci")
            .arg("--require-group")
            .arg("slow")
            .arg("--no-group")
            .arg("local")
            .arg("--group")
            .arg("lint")
            .arg("--require-group")
            .arg("format")
            .arg("--no-group")
            .arg("fast"),
        @r"
    success: true
    exit_code: 0
    ----- stdout -----
    Black....................................................................Passed

    ----- stderr -----
    "
    );

    cmd_snapshot!(context,
        context
            .run()
            .arg("--all-files")
            .arg("--require-group")
            .arg("lint")
            .arg("--require-group")
            .arg("format"),
        @r"
    success: false
    exit_code: 1
    ----- stdout -----

    ----- stderr -----
    error: No hooks found after filtering with the given selectors
    "
    );
}

#[test]
fn run_unknown_group_selectors_warn_and_empty_selection_fails() {
    let context = TestEnv::new()
        .with_config(indoc::indoc! {r#"
        repos:
          - repo: local
            hooks:
              - id: lint
                name: Lint
                language: system
                entry: python3 -c "print('lint')"
                always_run: true
                groups: [ci]
    "#})
        .init_git();

    cmd_snapshot!(context, context.run().arg("--all-files").arg("--group").arg("missing"), @r#"
    success: false
    exit_code: 1
    ----- stdout -----

    ----- stderr -----
    warning: group selector `--group=missing` did not match any hooks
    error: No hooks found after filtering with the given selectors
    "#);

    cmd_snapshot!(context, context.run().arg("--all-files").arg("--require-group").arg("missing"), @r#"
    success: false
    exit_code: 1
    ----- stdout -----

    ----- stderr -----
    warning: group selector `--require-group=missing` did not match any hooks
    error: No hooks found after filtering with the given selectors
    "#);
}

#[test]
fn run_group_selectors_reject_invalid_names() {
    let context = TestEnv::new()
        .with_config(indoc::indoc! {r#"
        repos:
          - repo: local
            hooks:
              - id: lint
                name: Lint
                language: system
                entry: python3 -c "print('lint')"
                always_run: true
                groups: [ci]
    "#})
        .init_git();

    cmd_snapshot!(context, context.run().arg("--all-files").arg("--group").arg("ci slow"), @r#"
    success: false
    exit_code: 2
    ----- stdout -----

    ----- stderr -----
    error: Invalid group selector: `--group=ci slow`
      caused by: group name cannot contain whitespace
    "#);

    cmd_snapshot!(context, context.run().arg("--all-files").arg("--no-group").arg("ci slow"), @r#"
    success: false
    exit_code: 2
    ----- stdout -----

    ----- stderr -----
    error: Invalid group selector: `--no-group=ci slow`
      caused by: group name cannot contain whitespace
    "#);

    cmd_snapshot!(context, context.run().arg("--all-files").arg("--group").arg("@custom"), @r#"
    success: false
    exit_code: 2
    ----- stdout -----

    ----- stderr -----
    error: Invalid group selector: `--group=@custom`
      caused by: group name uses the reserved `@` prefix
    "#);
}

#[test]
fn run_required_group_and_stage_filters_intersect() {
    let context = TestEnv::new()
        .with_config(indoc::indoc! {r#"
        repos:
          - repo: local
            hooks:
              - id: ci-push
                name: CI Push
                language: system
                entry: python3 -c "print('ci-push')"
                always_run: true
                stages: [pre-push]
                groups: [ci]
              - id: ci-manual
                name: CI Manual
                language: system
                entry: python3 -c "print('ci-manual')"
                always_run: true
                stages: [manual]
                groups: [ci]
              - id: other-push
                name: Other Push
                language: system
                entry: python3 -c "print('other-push')"
                always_run: true
                stages: [pre-push]
                groups: [other]
    "#})
        .init_git();

    cmd_snapshot!(context, context.run().arg("--all-files").arg("--require-group").arg("ci").arg("--stage").arg("pre-push"), @r#"
    success: true
    exit_code: 0
    ----- stdout -----
    CI Push..................................................................Passed

    ----- stderr -----
    "#);
}

/// Skips hooks based on the `SKIP` environment variable.
#[test]
fn skips() {
    let context = TestEnv::new()
        .with_config(indoc::indoc! {r#"
        repos:
          - repo: local
            hooks:
              - id: trailing-whitespace
                name: trailing-whitespace
                language: system
                entry: python3 -c "exit(1)"
              - id: end-of-file-fixer
                name: fix end of files
                language: system
                entry: python3 -c "exit(1)"
              - id: check-json
                name: check json
                language: system
                entry: python3 -c "exit(1)"
    "#})
        .init_git();

    cmd_snapshot!(context, context.run().env("SKIP", "end-of-file-fixer"), @r#"
    success: false
    exit_code: 1
    ----- stdout -----
    trailing-whitespace......................................................Failed
    - hook id: trailing-whitespace
    - exit code: 1
    fix end of files........................................................Skipped
    check json...............................................................Failed
    - hook id: check-json
    - exit code: 1

    ----- stderr -----
    "#);

    cmd_snapshot!(context, context.run().env("SKIP", "trailing-whitespace,end-of-file-fixer"), @r#"
    success: false
    exit_code: 1
    ----- stdout -----
    trailing-whitespace.....................................................Skipped
    fix end of files........................................................Skipped
    check json...............................................................Failed
    - hook id: check-json
    - exit code: 1

    ----- stderr -----
    "#);
}

/// Run hooks with matched `stage`.
#[test]
fn stage() {
    let context = TestEnv::new()
        .with_config(indoc::indoc! {r"
        repos:
          - repo: local
            hooks:
              - id: manual-stage
                name: manual-stage
                language: system
                entry: echo manual-stage
                stages: [ manual ]
              # Defaults to all stages.
              - id: default-stage
                name: default-stage
                language: system
                entry: echo default-stage
              - id: post-commit-stage
                name: post-commit-stage
                language: system
                entry: echo post-commit-stage
                stages: [ post-commit ]
    "})
        .init_git();

    // By default, run hooks with `pre-commit` stage.
    cmd_snapshot!(context, context.run(), @r#"
    success: true
    exit_code: 0
    ----- stdout -----
    default-stage............................................................Passed

    ----- stderr -----
    "#);

    // Run hooks with `manual` stage.
    cmd_snapshot!(context, context.run().arg("--hook-stage").arg("manual"), @r#"
    success: true
    exit_code: 0
    ----- stdout -----
    manual-stage.............................................................Passed
    default-stage............................................................Passed

    ----- stderr -----
    "#);

    // Run hooks with `post-commit` stage.
    cmd_snapshot!(context, context.run().arg("--hook-stage").arg("post-commit"), @r#"
    success: true
    exit_code: 0
    ----- stdout -----
    default-stage........................................(no files to check)Skipped
    post-commit-stage....................................(no files to check)Skipped

    ----- stderr -----
    "#);
}

#[test]
fn fallback_to_manual_stage() {
    let context = TestEnv::new()
        .with_config(indoc::indoc! {r"
        repos:
          - repo: local
            hooks:
              - id: manual-only
                name: manual-only
                language: system
                entry: echo manual-only
                stages: [ manual ]
              - id: another-manual
                name: another-manual
                language: system
                entry: echo another-manual
                stages: [ manual ]
              - id: default-stage
                name: default-stage
                language: system
                entry: echo default-stage
              - id: pre-push
                name: pre-push
                language: system
                entry: echo pre-push
                stages: [ pre-push ]
    "})
        .init_git();

    // With pre-commit hooks present, default `prek run` stays on pre-commit.
    cmd_snapshot!(context, context.run(), @r"
    success: true
    exit_code: 0
    ----- stdout -----
    default-stage............................................................Passed

    ----- stderr -----
    ");

    // Explicit `--hook-stage pre-commit` keeps execution scoped to that stage.
    cmd_snapshot!(context, context.run().arg("--hook-stage").arg("pre-commit").arg("default-stage").arg("manual-only"), @r"
    success: true
    exit_code: 0
    ----- stdout -----
    default-stage............................................................Passed

    ----- stderr -----
    ");

    // Selecting manual + pre-commit hooks still runs only the pre-commit ones.
    cmd_snapshot!(context, context.run().arg("manual-only").arg("default-stage"), @r"
    success: true
    exit_code: 0
    ----- stdout -----
    default-stage............................................................Passed

    ----- stderr -----
    ");

    // A skipped pre-commit hook should not prevent a runnable manual hook from falling back.
    cmd_snapshot!(context, context.run().arg("--skip").arg("default-stage").arg("manual-only").arg("default-stage"), @r#"
    success: true
    exit_code: 0
    ----- stdout -----
    manual-only..............................................................Passed
    default-stage...........................................................Skipped

    ----- stderr -----
    "#);

    // Selecting only manual hooks should still succeed via fallback.
    cmd_snapshot!(context, context.run().arg("manual-only").arg("another-manual"), @r"
    success: true
    exit_code: 0
    ----- stdout -----
    manual-only..............................................................Passed
    another-manual...........................................................Passed

    ----- stderr -----
    ");

    // Mixing `pre-push` and manual selectors still runs the manual hook via fallback.
    cmd_snapshot!(context, context.run().arg("pre-push").arg("manual-only"), @r"
    success: true
    exit_code: 0
    ----- stdout -----
    manual-only..............................................................Passed

    ----- stderr -----
    ");
}