Skip to content

Commit 3fa1f6e

Browse files
committed
Add FanOut3 helper and reorganize test helpers
1 parent caa7007 commit 3fa1f6e

File tree

6 files changed

+69
-44
lines changed

6 files changed

+69
-44
lines changed

hooks/commit_msg_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import (
88
"github.com/stretchr/testify/assert"
99
"github.com/stretchr/testify/require"
1010

11-
"github.com/dirk/quickhook/testutils"
11+
"github.com/dirk/quickhook/internal/test"
1212
)
1313

14-
func initGitForCommitMsg(t *testing.T) testutils.TempDir {
15-
tempDir := testutils.NewTempDir(t, 1)
14+
func initGitForCommitMsg(t *testing.T) test.TempDir {
15+
tempDir := test.NewTempDir(t, 1)
1616
tempDir.RequireExec("git", "init", "--quiet", ".")
1717
return tempDir
1818
}

hooks/pre_commit.go

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import (
66
"path"
77
"strings"
88

9-
"github.com/samber/lo"
109
lop "github.com/samber/lo/parallel"
1110

11+
"github.com/dirk/quickhook/internal"
1212
"github.com/dirk/quickhook/repo"
1313
"github.com/dirk/quickhook/tracing"
1414
)
@@ -26,41 +26,32 @@ type PreCommit struct {
2626
// argsFiles can be non-empty with the files passed in by the user when manually running this hook,
2727
// or it can be empty and the list of files will be retrieved from Git.
2828
func (hook *PreCommit) Run(argsFiles []string) error {
29-
// Resolve files to be committed in parallel with shimming git.
30-
filesChan := lo.Async2(func() ([]string, error) {
31-
if len(argsFiles) > 0 {
32-
return argsFiles, nil
33-
}
34-
if files, err := hook.Repo.FilesToBeCommitted(); err != nil {
35-
return nil, err
36-
} else {
37-
return files, nil
38-
}
39-
})
40-
shimChan := lo.Async2(shimGit)
41-
mutatingChan := lo.Async2(func() ([]string, error) {
42-
return hook.Repo.FindHookExecutables(PRE_COMMIT_MUTATING_HOOK)
43-
})
44-
parallelChan := lo.Async2(func() ([]string, error) {
45-
return hook.Repo.FindHookExecutables(PRE_COMMIT_HOOK)
46-
})
47-
48-
dirForPath, err := (<-shimChan).Unpack()
29+
// The shimming is really fast, so just do it first with a defer for cleaning up the
30+
// temporary directory.
31+
dirForPath, err := shimGit()
4932
if err != nil {
5033
return err
5134
}
52-
// Check the shimChan first so that if we did successfully create a directory with a shim we
53-
// can make sure to clean it up if anything else errored.
5435
defer os.RemoveAll(dirForPath)
55-
files, err := (<-filesChan).Unpack()
56-
if err != nil {
57-
return err
58-
}
59-
mutatingExecutables, err := (<-mutatingChan).Unpack()
60-
if err != nil {
61-
return err
62-
}
63-
parallelExecutables, err := (<-parallelChan).Unpack()
36+
37+
files, mutatingExecutables, parallelExecutables, err := internal.FanOut3(
38+
func() ([]string, error) {
39+
if len(argsFiles) > 0 {
40+
return argsFiles, nil
41+
}
42+
if files, err := hook.Repo.FilesToBeCommitted(); err != nil {
43+
return nil, err
44+
} else {
45+
return files, nil
46+
}
47+
},
48+
func() ([]string, error) {
49+
return hook.Repo.FindHookExecutables(PRE_COMMIT_MUTATING_HOOK)
50+
},
51+
func() ([]string, error) {
52+
return hook.Repo.FindHookExecutables(PRE_COMMIT_HOOK)
53+
},
54+
)
6455
if err != nil {
6556
return err
6657
}

hooks/pre_commit_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import (
99
"github.com/stretchr/testify/assert"
1010
"github.com/stretchr/testify/require"
1111

12-
"github.com/dirk/quickhook/testutils"
12+
"github.com/dirk/quickhook/internal/test"
1313
)
1414

15-
func initGitForPreCommit(t *testing.T) testutils.TempDir {
16-
tempDir := testutils.NewTempDir(t, 1)
15+
func initGitForPreCommit(t *testing.T) test.TempDir {
16+
tempDir := test.NewTempDir(t, 1)
1717
tempDir.RequireExec("git", "init", "--quiet", ".")
1818
tempDir.RequireExec("git", "config", "--local", "user.name", "example")
1919
tempDir.RequireExec("git", "config", "--local", "user.email", "[email protected]")

install_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import (
88

99
"github.com/stretchr/testify/assert"
1010

11+
"github.com/dirk/quickhook/internal/test"
1112
"github.com/dirk/quickhook/repo"
12-
"github.com/dirk/quickhook/testutils"
1313
)
1414

1515
func TestInstallPreCommitYes(t *testing.T) {
16-
tempDir := testutils.NewTempDir(t, 0)
16+
tempDir := test.NewTempDir(t, 0)
1717
tempDir.RequireExec("git", "init", "--quiet", ".")
1818
tempDir.MkdirAll(".quickhook", "pre-commit")
1919

@@ -28,7 +28,7 @@ func TestInstallPreCommitYes(t *testing.T) {
2828
}
2929

3030
func TestInstallPreCommitMutatingYes(t *testing.T) {
31-
tempDir := testutils.NewTempDir(t, 0)
31+
tempDir := test.NewTempDir(t, 0)
3232
tempDir.RequireExec("git", "init", "--quiet", ".")
3333
tempDir.MkdirAll(".quickhook", "pre-commit-mutating")
3434

@@ -43,7 +43,7 @@ func TestInstallPreCommitMutatingYes(t *testing.T) {
4343
}
4444

4545
func TestInstallNoQuickhookDirectory(t *testing.T) {
46-
tempDir := testutils.NewTempDir(t, 0)
46+
tempDir := test.NewTempDir(t, 0)
4747
tempDir.RequireExec("git", "init", "--quiet", ".")
4848

4949
output, err := tempDir.ExecQuickhook("install", "--yes")
@@ -85,7 +85,7 @@ func TestPromptForInstall(t *testing.T) {
8585
}
8686
for _, tt := range ptyTests {
8787
t.Run(tt.name, func(t *testing.T) {
88-
tempDir := testutils.NewTempDir(t, 0)
88+
tempDir := test.NewTempDir(t, 0)
8989
repo := &repo.Repo{Root: tempDir.Root}
9090

9191
stdin := strings.NewReader(tt.stdin)

internal/fanout.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package internal
2+
3+
import "github.com/samber/lo"
4+
5+
// Run three functions which return results or errors in parallel. If any return an error then
6+
// return that error (starting with the first function). If none error then return their results.
7+
func FanOut3[A any, B any, C any](
8+
a func() (A, error),
9+
b func() (B, error),
10+
c func() (C, error),
11+
) (A, B, C, error) {
12+
ch1 := lo.Async2(a)
13+
ch2 := lo.Async2(b)
14+
ch3 := lo.Async2(c)
15+
16+
var zero1 A
17+
var zero2 B
18+
var zero3 C
19+
20+
r1, err := (<-ch1).Unpack()
21+
if err != nil {
22+
return zero1, zero2, zero3, err
23+
}
24+
r2, err := (<-ch2).Unpack()
25+
if err != nil {
26+
return zero1, zero2, zero3, err
27+
}
28+
r3, err := (<-ch3).Unpack()
29+
if err != nil {
30+
return zero1, zero2, zero3, err
31+
}
32+
33+
return r1, r2, r3, nil
34+
}

testutils/tempdir.go renamed to internal/test/tempdir.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package testutils
1+
package test
22

33
import (
44
"os"

0 commit comments

Comments
 (0)