Skip to content

Commit

Permalink
fix: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexox committed Mar 16, 2023
1 parent 6358e08 commit db0b995
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
2 changes: 2 additions & 0 deletions internal/lefthook/runner/prepare_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ func (r *Runner) buildCommandArgs(command *config.Command) (*commandArgs, error,

if len(filteredFiles) == 0 && config.HookUsesStagedFiles(r.HookName) {
files, err := r.Repo.StagedFiles()
log.Infof("FILES: %#v\n", files)
log.Infof("FILES Prepared: %#v\n", prepareFiles(command, files))
if err == nil {
if len(prepareFiles(command, files)) == 0 {
return nil, nil, errors.New("no matching staged files")
Expand Down
14 changes: 8 additions & 6 deletions internal/lefthook/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,7 @@ func (r *Runner) runScript(script *config.Script, path string, file os.FileInfo)
return
}

if err := r.Repo.AddFiles(files); err != nil {
log.Warn("Couldn't stage fixed files:", err)
}
r.addStagedFiles(files)
}
}

Expand Down Expand Up @@ -388,9 +386,13 @@ func (r *Runner) runCommand(name string, command *config.Command) {
}
}

if err := r.Repo.AddFiles(files); err != nil {
log.Warn("Couldn't stage fixed files:", err)
}
r.addStagedFiles(files)
}
}

func (r *Runner) addStagedFiles(files []string) {
if err := r.Repo.AddFiles(files); err != nil {
log.Warn("Couldn't stage fixed files:", err)
}
}

Expand Down
30 changes: 28 additions & 2 deletions internal/lefthook/runner/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (g *GitMock) CmdLines(cmd string) ([]string, error) {
cmd == "git diff --name-only HEAD @{push}" {
root, _ := filepath.Abs("src")
return []string{
filepath.Join(root, "script.sh"),
filepath.Join(root, "scripts", "script.sh"),
filepath.Join(root, "README.md"),
}, nil
}
Expand Down Expand Up @@ -423,7 +423,7 @@ func TestRunAll(t *testing.T) {
existingFiles: []string{
filepath.Join(root, config.DefaultSourceDir, "pre-commit", "success.sh"),
filepath.Join(root, config.DefaultSourceDir, "pre-commit", "failing.js"),
filepath.Join(root, "script.sh"),
filepath.Join(root, "scripts", "script.sh"),
filepath.Join(root, "README.md"),
},
hook: &config.Hook{
Expand Down Expand Up @@ -493,6 +493,32 @@ func TestRunAll(t *testing.T) {
"git stash list",
},
},
{
name: "pre-commit hook with stage_fixed under root",
hookName: "pre-commit",
existingFiles: []string{
filepath.Join(root, "scripts", "script.sh"),
filepath.Join(root, "README.md"),
},
hook: &config.Hook{
Commands: map[string]*config.Command{
"ok": {
Run: "success",
Root: filepath.Join(root, "scripts"),
StageFixed: true,
},
},
},
success: []Result{{Name: "ok", Status: StatusOk}},
gitCommands: []string{
"git status --short",
"git diff --name-only --cached --diff-filter=ACMR",
"git diff --name-only --cached --diff-filter=ACMR",
"git add .*scripts.*script.sh",
"git apply -v --whitespace=nowarn --recount --unidiff-zero ",
"git stash list",
},
},
{
name: "pre-push hook with implicit skip",
hookName: "pre-push",
Expand Down

0 comments on commit db0b995

Please sign in to comment.