Skip to content

Commit

Permalink
fix: fail on non existing hook name (#545)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexox authored Sep 5, 2023
1 parent 3cabf4a commit 22eaf62
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
9 changes: 7 additions & 2 deletions internal/lefthook/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"slices"
"strings"
"time"

Expand Down Expand Up @@ -97,8 +98,12 @@ Run 'lefthook install' manually.`,
// Find the hook
hook, ok := cfg.Hooks[hookName]
if !ok {
log.Debugf("[lefthook] skip: Hook %s doesn't exist in the config", hookName)
return nil
if slices.Contains(config.AvailableHooks[:], hookName) {
log.Debugf("[lefthook] skip: Hook %s doesn't exist in the config", hookName)
return nil
}

return fmt.Errorf("Hook %s doesn't exist in the config", hookName)
}
if err := hook.Validate(); err != nil {
return err
Expand Down
8 changes: 4 additions & 4 deletions internal/lefthook/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,31 +47,31 @@ func TestRun(t *testing.T) {
}{
{
name: "Skip case",
hook: "any-hook",
hook: "pre-commit",
envs: map[string]string{
"LEFTHOOK": "0",
},
error: false,
},
{
name: "Skip case",
hook: "any-hook",
hook: "pre-commit",
envs: map[string]string{
"LEFTHOOK": "false",
},
error: false,
},
{
name: "Invalid version",
hook: "any-hook",
hook: "pre-commit",
config: `
min_version: 23.0.1
`,
error: true,
},
{
name: "Valid version, no hook",
hook: "any-hook",
hook: "pre-commit",
config: `
min_version: 0.7.9
`,
Expand Down
8 changes: 8 additions & 0 deletions testdata/run_non_existing.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
exec git init
exec lefthook run pre-commit
! stdout 'Error.*'
! exec lefthook run no-a-hook
stdout 'Error.*'

-- lefthook.yml --
# empty

0 comments on commit 22eaf62

Please sign in to comment.