Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0ae7b16

Browse files
authored
refactor: better scope subpackages (#1132)
1 parent 311971a commit 0ae7b16

File tree

18 files changed

+355
-141
lines changed

18 files changed

+355
-141
lines changed

internal/command/install_test.go

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/stretchr/testify/assert"
1111

1212
"github.com/evilmartians/lefthook/internal/config"
13-
"github.com/evilmartians/lefthook/internal/git"
13+
"github.com/evilmartians/lefthook/tests/helpers/gittest"
1414
)
1515

1616
func TestLefthookInstall(t *testing.T) {
@@ -20,17 +20,11 @@ func TestLefthookInstall(t *testing.T) {
2020
configPath := filepath.Join(root, "lefthook.yml")
2121

2222
hookPath := func(hook string) string {
23-
return filepath.Join(root, ".git", "hooks", hook)
23+
return filepath.Join(gittest.GitPath(root), "hooks", hook)
2424
}
2525

2626
infoPath := func(file string) string {
27-
return filepath.Join(root, ".git", "info", file)
28-
}
29-
30-
repo := &git.Repository{
31-
HooksPath: filepath.Join(root, ".git", "hooks"),
32-
RootPath: root,
33-
InfoPath: filepath.Join(root, ".git", "info"),
27+
return filepath.Join(gittest.GitPath(root), "info", file)
3428
}
3529

3630
for n, tt := range [...]struct {
@@ -219,6 +213,7 @@ post-commit:
219213
},
220214
} {
221215
fs := afero.NewMemMapFs()
216+
repo := gittest.NewRepositoryBuilder().Root(root).Fs(fs).Build()
222217
lefthook := &Lefthook{
223218
fs: fs,
224219
repo: repo,
@@ -284,11 +279,6 @@ func Test_syncHooks(t *testing.T) {
284279
return filepath.Join(root, ".git", "info", file)
285280
}
286281

287-
repo := &git.Repository{
288-
HooksPath: filepath.Join(root, ".git", "hooks"),
289-
RootPath: root,
290-
InfoPath: filepath.Join(root, ".git", "info"),
291-
}
292282
for n, tt := range [...]struct {
293283
name, config, checksum string
294284
existingHooks map[string]string
@@ -402,6 +392,7 @@ commit-msg:
402392
},
403393
} {
404394
fs := afero.NewMemMapFs()
395+
repo := gittest.NewRepositoryBuilder().Root(root).Fs(fs).Build()
405396
lefthook := &Lefthook{
406397
fs: fs,
407398
repo: repo,
@@ -466,11 +457,6 @@ func TestShouldRefetch(t *testing.T) {
466457
return filepath.Join(remotePath, ".git", "FETCH_HEAD")
467458
}
468459

469-
repo := &git.Repository{
470-
HooksPath: filepath.Join(root, ".git", "hooks"),
471-
RootPath: root,
472-
InfoPath: filepath.Join(root, ".git", "info"),
473-
}
474460
for n, tt := range [...]struct {
475461
name, config string
476462
shouldRefetchInitially, shouldRefetchAfter, shouldRefetchBefore bool
@@ -528,6 +514,7 @@ remotes:
528514
},
529515
} {
530516
fs := afero.NewMemMapFs()
517+
repo := gittest.NewRepositoryBuilder().Root(root).Fs(fs).Build()
531518
lefthook := &Lefthook{
532519
fs: fs,
533520
repo: repo,

internal/command/run_test.go

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

12-
"github.com/evilmartians/lefthook/internal/git"
1312
"github.com/evilmartians/lefthook/internal/system"
13+
"github.com/evilmartians/lefthook/tests/helpers/gittest"
1414
)
1515

1616
type gitCmd struct{}
@@ -29,9 +29,8 @@ func TestRun(t *testing.T) {
2929
t.Errorf("unexpected error: %s", err)
3030
}
3131

32+
gitPath := gittest.GitPath(root)
3233
configPath := filepath.Join(root, "lefthook.yml")
33-
hooksPath := filepath.Join(root, ".git", "hooks")
34-
gitPath := filepath.Join(root, ".git")
3534

3635
for i, tt := range [...]struct {
3736
name, hook, config string
@@ -161,14 +160,8 @@ post-commit:
161160
assert := assert.New(t)
162161
fs := afero.NewMemMapFs()
163162
lefthook := &Lefthook{
164-
fs: fs,
165-
repo: &git.Repository{
166-
Fs: fs,
167-
Git: git.NewExecutor(gitCmd{}),
168-
HooksPath: hooksPath,
169-
RootPath: root,
170-
GitPath: gitPath,
171-
},
163+
fs: fs,
164+
repo: gittest.NewRepositoryBuilder().Git(gitCmd{}).Fs(fs).Root(root).Build(),
172165
}
173166
lefthook.repo.Setup()
174167

internal/command/uninstall_test.go

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/spf13/afero"
99

1010
"github.com/evilmartians/lefthook/internal/config"
11-
"github.com/evilmartians/lefthook/internal/git"
11+
"github.com/evilmartians/lefthook/tests/helpers/gittest"
1212
)
1313

1414
func TestLefthookUninstall(t *testing.T) {
@@ -18,13 +18,10 @@ func TestLefthookUninstall(t *testing.T) {
1818
}
1919

2020
configPath := filepath.Join(root, "lefthook.yml")
21-
gitPath := filepath.Join(root, ".git")
22-
hooksPath := filepath.Join(gitPath, "hooks")
23-
infoPath := filepath.Join(gitPath, "info")
24-
checksumPath := filepath.Join(infoPath, config.ChecksumFileName)
21+
checksumPath := filepath.Join(gittest.GitPath(root), "info", config.ChecksumFileName)
2522

2623
hookPath := func(hook string) string {
27-
return filepath.Join(root, ".git", "hooks", hook)
24+
return filepath.Join(gittest.GitPath(root), "hooks", hook)
2825
}
2926

3027
for n, tt := range [...]struct {
@@ -103,14 +100,8 @@ func TestLefthookUninstall(t *testing.T) {
103100
t.Run(fmt.Sprintf("%d: %s", n, tt.name), func(t *testing.T) {
104101
fs := afero.NewMemMapFs()
105102
lefthook := &Lefthook{
106-
fs: fs,
107-
repo: &git.Repository{
108-
Fs: fs,
109-
HooksPath: hooksPath,
110-
RootPath: root,
111-
GitPath: gitPath,
112-
InfoPath: infoPath,
113-
},
103+
fs: fs,
104+
repo: gittest.NewRepositoryBuilder().Fs(fs).Root(root).Build(),
114105
}
115106

116107
// Create config and checksum file

internal/config/load_test.go

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/spf13/afero"
1010
"github.com/stretchr/testify/assert"
1111

12-
"github.com/evilmartians/lefthook/internal/git"
12+
"github.com/evilmartians/lefthook/tests/helpers/gittest"
1313
)
1414

1515
//gocyclo:ignore
@@ -989,12 +989,7 @@ pre-commit:
989989
},
990990
} {
991991
fs := afero.Afero{Fs: afero.NewMemMapFs()}
992-
repo := &git.Repository{
993-
Fs: fs,
994-
RootPath: root,
995-
InfoPath: filepath.Join(root, ".git", "info"),
996-
}
997-
992+
repo := gittest.NewRepositoryBuilder().Fs(fs).Root(root).Build()
998993
t.Run(name, func(t *testing.T) {
999994
assert := assert.New(t)
1000995

@@ -1064,11 +1059,7 @@ run = "echo 1"
10641059
},
10651060
} {
10661061
fs := afero.Afero{Fs: afero.NewMemMapFs()}
1067-
repo := &git.Repository{
1068-
Fs: fs,
1069-
RootPath: root,
1070-
InfoPath: filepath.Join(root, ".git", "info"),
1071-
}
1062+
repo := gittest.NewRepositoryBuilder().Root(root).Fs(fs).Build()
10721063

10731064
t.Run(fmt.Sprintf("%d: %s", i, tt.name), func(t *testing.T) {
10741065
assert := assert.New(t)
@@ -1210,11 +1201,7 @@ pre-commit:
12101201
},
12111202
} {
12121203
fs := afero.Afero{Fs: afero.NewMemMapFs()}
1213-
repo := &git.Repository{
1214-
Fs: fs,
1215-
RootPath: root,
1216-
InfoPath: filepath.Join(root, ".git", "info"),
1217-
}
1204+
repo := gittest.NewRepositoryBuilder().Root(root).Fs(fs).Build()
12181205

12191206
t.Run(name, func(t *testing.T) {
12201207
assert := assert.New(t)

internal/git/repository.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,12 @@ func NewRepository(fs afero.Fs, git *CommandExecutor) (*Repository, error) {
106106
git.root = rootPath
107107

108108
r := &Repository{
109-
Fs: fs,
110-
Git: git,
111-
HooksPath: hooksPath,
112-
RootPath: rootPath,
113-
GitPath: gitPath,
114-
InfoPath: infoPath,
115-
unstagedPatchPath: filepath.Join(infoPath, unstagedPatchName),
109+
Fs: fs,
110+
Git: git,
111+
HooksPath: hooksPath,
112+
RootPath: rootPath,
113+
GitPath: gitPath,
114+
InfoPath: infoPath,
116115
}
117116

118117
r.Setup()
@@ -167,6 +166,8 @@ func (r *Repository) Setup() {
167166
r.stateOnce = sync.OnceValue(func() State {
168167
return r.state()
169168
})
169+
170+
r.unstagedPatchPath = filepath.Join(r.InfoPath, unstagedPatchName)
170171
}
171172

172173
// StagedFiles returns a list of staged files which exist on file system.

internal/run/controller/controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212
"github.com/evilmartians/lefthook/internal/config"
1313
"github.com/evilmartians/lefthook/internal/git"
1414
"github.com/evilmartians/lefthook/internal/log"
15+
"github.com/evilmartians/lefthook/internal/run/controller/exec"
1516
"github.com/evilmartians/lefthook/internal/run/controller/utils"
16-
"github.com/evilmartians/lefthook/internal/run/exec"
1717
"github.com/evilmartians/lefthook/internal/run/result"
1818
"github.com/evilmartians/lefthook/internal/system"
1919
)
@@ -75,7 +75,7 @@ func (c *Controller) RunHook(ctx context.Context, opts Options, hook *config.Hoo
7575
defer log.StopSpinner()
7676
}
7777

78-
guard := newGuard(c, !opts.NoStageFixed && config.HookUsesStagedFiles(hook.Name), opts.FailOnChanges)
78+
guard := newGuard(c.git, !opts.NoStageFixed && config.HookUsesStagedFiles(hook.Name), opts.FailOnChanges)
7979
scope := newScope(hook, opts)
8080
err := guard.wrap(func() {
8181
if hook.Parallel {

0 commit comments

Comments
 (0)