Skip to content

Commit 22eaf62

Browse files
authoredSep 5, 2023
fix: fail on non existing hook name (#545)
1 parent 3cabf4a commit 22eaf62

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed
 

‎internal/lefthook/run.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"os"
77
"path/filepath"
8+
"slices"
89
"strings"
910
"time"
1011

@@ -97,8 +98,12 @@ Run 'lefthook install' manually.`,
9798
// Find the hook
9899
hook, ok := cfg.Hooks[hookName]
99100
if !ok {
100-
log.Debugf("[lefthook] skip: Hook %s doesn't exist in the config", hookName)
101-
return nil
101+
if slices.Contains(config.AvailableHooks[:], hookName) {
102+
log.Debugf("[lefthook] skip: Hook %s doesn't exist in the config", hookName)
103+
return nil
104+
}
105+
106+
return fmt.Errorf("Hook %s doesn't exist in the config", hookName)
102107
}
103108
if err := hook.Validate(); err != nil {
104109
return err

‎internal/lefthook/run_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -47,31 +47,31 @@ func TestRun(t *testing.T) {
4747
}{
4848
{
4949
name: "Skip case",
50-
hook: "any-hook",
50+
hook: "pre-commit",
5151
envs: map[string]string{
5252
"LEFTHOOK": "0",
5353
},
5454
error: false,
5555
},
5656
{
5757
name: "Skip case",
58-
hook: "any-hook",
58+
hook: "pre-commit",
5959
envs: map[string]string{
6060
"LEFTHOOK": "false",
6161
},
6262
error: false,
6363
},
6464
{
6565
name: "Invalid version",
66-
hook: "any-hook",
66+
hook: "pre-commit",
6767
config: `
6868
min_version: 23.0.1
6969
`,
7070
error: true,
7171
},
7272
{
7373
name: "Valid version, no hook",
74-
hook: "any-hook",
74+
hook: "pre-commit",
7575
config: `
7676
min_version: 0.7.9
7777
`,

‎testdata/run_non_existing.txt

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
exec git init
2+
exec lefthook run pre-commit
3+
! stdout 'Error.*'
4+
! exec lefthook run no-a-hook
5+
stdout 'Error.*'
6+
7+
-- lefthook.yml --
8+
# empty

0 commit comments

Comments
 (0)