Skip to content

Commit 009da68

Browse files
authored
chore: add more tests (#1244)
1 parent 737b956 commit 009da68

File tree

14 files changed

+488
-239
lines changed

14 files changed

+488
-239
lines changed

internal/command/install_test.go

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

1212
"github.com/evilmartians/lefthook/v2/internal/config"
13+
"github.com/evilmartians/lefthook/v2/tests/helpers/cmdtest"
1314
"github.com/evilmartians/lefthook/v2/tests/helpers/gittest"
1415
)
1516

@@ -31,7 +32,8 @@ func TestLefthookInstall(t *testing.T) {
3132
name, config, checksum string
3233
force bool
3334
hooks []string
34-
existingHooks map[string]string
35+
git []cmdtest.Out
36+
existingFiles map[string]string
3537
wantExist, wantNotExist []string
3638
wantError bool
3739
}{
@@ -97,8 +99,8 @@ post-commit:
9799
notify:
98100
run: echo 'Done!'
99101
`,
100-
existingHooks: map[string]string{
101-
"pre-commit": "",
102+
existingFiles: map[string]string{
103+
hookPath("pre-commit"): "",
102104
},
103105
wantExist: []string{
104106
configPath,
@@ -122,8 +124,8 @@ post-commit:
122124
notify:
123125
run: echo 'Done!'
124126
`,
125-
existingHooks: map[string]string{
126-
"pre-commit": "# LEFTHOOK file",
127+
existingFiles: map[string]string{
128+
hookPath("pre-commit"): "# LEFTHOOK file",
127129
},
128130
wantExist: []string{
129131
configPath,
@@ -171,9 +173,9 @@ post-commit:
171173
notify:
172174
run: echo 'Done!'
173175
`,
174-
existingHooks: map[string]string{
175-
"pre-commit": "",
176-
"pre-commit.old": "",
176+
existingFiles: map[string]string{
177+
hookPath("pre-commit"): "",
178+
hookPath("pre-commit.old"): "",
177179
},
178180
wantError: true,
179181
wantExist: []string{
@@ -199,9 +201,9 @@ post-commit:
199201
notify:
200202
run: echo 'Done!'
201203
`,
202-
existingHooks: map[string]string{
203-
"pre-commit": "",
204-
"pre-commit.old": "",
204+
existingFiles: map[string]string{
205+
hookPath("pre-commit"): "",
206+
hookPath("pre-commit.old"): "",
205207
},
206208
wantExist: []string{
207209
configPath,
@@ -211,17 +213,60 @@ post-commit:
211213
infoPath(config.ChecksumFileName),
212214
},
213215
},
216+
{
217+
name: "with unfetched remote",
218+
config: `
219+
remotes:
220+
- git_url: https://github.com/evilmartians/lefthook
221+
configs:
222+
- lefthook.yml
223+
`,
224+
git: []cmdtest.Out{
225+
{
226+
Command: "git -C " + filepath.Join(root, ".git", "info", "lefthook-remotes") + " clone --quiet --origin origin --depth 1 https://github.com/evilmartians/lefthook lefthook",
227+
},
228+
},
229+
},
230+
{
231+
name: "needs refetching",
232+
config: `
233+
remotes:
234+
- git_url: https://github.com/evilmartians/lefthook
235+
ref: v2.0.0
236+
configs:
237+
- lefthook.yml
238+
`,
239+
existingFiles: map[string]string{
240+
filepath.Join(root, ".git", "info", "lefthook-remotes", "lefthook-v2.0.1", ".git", "FETCH_HEAD"): "",
241+
},
242+
git: []cmdtest.Out{
243+
{
244+
Command: "git -C " + filepath.Join(root, ".git", "info", "lefthook-remotes") + " clone --quiet --origin origin --depth 1 --branch v2.0.0 https://github.com/evilmartians/lefthook lefthook-v2.0.0",
245+
},
246+
{
247+
Command: "git -C " + filepath.Join(root, ".git", "info", "lefthook-remotes", "lefthook-v2.0.0") + " fetch --quiet --depth 1 origin v2.0.0",
248+
},
249+
{
250+
Command: "git -C " + filepath.Join(root, ".git", "info", "lefthook-remotes", "lefthook-v2.0.0") + " checkout FETCH_HEAD",
251+
},
252+
},
253+
},
214254
} {
215255
fs := afero.NewMemMapFs()
216-
repo := gittest.NewRepositoryBuilder().Root(root).Fs(fs).Build()
217-
lefthook := &Lefthook{
218-
fs: fs,
219-
repo: repo,
220-
}
221256

222257
t.Run(fmt.Sprintf("%d: %s", n, tt.name), func(t *testing.T) {
223258
assert := assert.New(t)
224259

260+
repo := gittest.NewRepositoryBuilder().
261+
Root(root).
262+
Fs(fs).
263+
Cmd(cmdtest.NewOrdered(t, tt.git)).
264+
Build()
265+
lefthook := &Lefthook{
266+
fs: fs,
267+
repo: repo,
268+
}
269+
225270
// Create configuration file
226271
if len(tt.config) > 0 {
227272
assert.NoError(afero.WriteFile(fs, configPath, []byte(tt.config), 0o644))
@@ -234,8 +279,7 @@ post-commit:
234279
}
235280

236281
// Create files that should exist
237-
for hook, content := range tt.existingHooks {
238-
path := hookPath(hook)
282+
for path, content := range tt.existingFiles {
239283
assert.NoError(fs.MkdirAll(filepath.Dir(path), 0o755))
240284
assert.NoError(afero.WriteFile(fs, path, []byte(content), 0o755))
241285
}
@@ -252,14 +296,14 @@ post-commit:
252296
for _, file := range tt.wantExist {
253297
ok, err := afero.Exists(fs, file)
254298
assert.NoError(err)
255-
assert.Equal(ok, true)
299+
assert.Equal(true, ok)
256300
}
257301

258302
// Test files that should not exist
259303
for _, file := range tt.wantNotExist {
260304
ok, err := afero.Exists(fs, file)
261305
assert.NoError(err)
262-
assert.Equal(ok, false)
306+
assert.Equal(false, ok)
263307
}
264308
})
265309
}
@@ -281,7 +325,8 @@ func Test_syncHooks(t *testing.T) {
281325

282326
for n, tt := range [...]struct {
283327
name, config, checksum string
284-
existingHooks map[string]string
328+
existingFiles map[string]string
329+
git []cmdtest.Out
285330
wantExist, wantNotExist []string
286331
wantError bool
287332
}{
@@ -390,17 +435,72 @@ commit-msg:
390435
hookPath(config.GhostHookName),
391436
},
392437
},
438+
{
439+
name: "with unfetched remote",
440+
config: `
441+
remotes:
442+
- git_url: https://github.com/evilmartians/lefthook
443+
configs:
444+
- lefthook.yml
445+
`,
446+
git: []cmdtest.Out{
447+
{
448+
Command: "git -C " + filepath.Join(root, ".git", "info", "lefthook-remotes") + " clone --quiet --origin origin --depth 1 https://github.com/evilmartians/lefthook lefthook",
449+
},
450+
},
451+
},
452+
{
453+
name: "no need to refetch",
454+
config: `
455+
remotes:
456+
- git_url: https://github.com/evilmartians/lefthook
457+
ref: v2.0.1
458+
configs:
459+
- lefthook.yml
460+
`,
461+
existingFiles: map[string]string{
462+
filepath.Join(root, ".git", "info", "lefthook-remotes", "lefthook-v2.0.1", ".git", "FETCH_HEAD"): "",
463+
},
464+
},
465+
{
466+
name: "needs refetching",
467+
config: `
468+
remotes:
469+
- git_url: https://github.com/evilmartians/lefthook
470+
ref: v2.0.0
471+
configs:
472+
- lefthook.yml
473+
`,
474+
existingFiles: map[string]string{
475+
filepath.Join(root, ".git", "info", "lefthook-remotes", "lefthook-v2.0.1", ".git", "FETCH_HEAD"): "",
476+
},
477+
git: []cmdtest.Out{
478+
{
479+
Command: "git -C " + filepath.Join(root, ".git", "info", "lefthook-remotes") + " clone --quiet --origin origin --depth 1 --branch v2.0.0 https://github.com/evilmartians/lefthook lefthook-v2.0.0",
480+
},
481+
{
482+
Command: "git -C " + filepath.Join(root, ".git", "info", "lefthook-remotes", "lefthook-v2.0.0") + " fetch --quiet --depth 1 origin v2.0.0",
483+
},
484+
{
485+
Command: "git -C " + filepath.Join(root, ".git", "info", "lefthook-remotes", "lefthook-v2.0.0") + " checkout FETCH_HEAD",
486+
},
487+
},
488+
wantNotExist: []string{
489+
filepath.Join(root, ".git", "info", "lefthook-remotes", "lefthook-v2.0.1"),
490+
},
491+
},
393492
} {
394493
fs := afero.NewMemMapFs()
395-
repo := gittest.NewRepositoryBuilder().Root(root).Fs(fs).Build()
396-
lefthook := &Lefthook{
397-
fs: fs,
398-
repo: repo,
399-
}
400494

401495
t.Run(fmt.Sprintf("%d: %s", n, tt.name), func(t *testing.T) {
402496
assert := assert.New(t)
403497

498+
repo := gittest.NewRepositoryBuilder().Root(root).Fs(fs).Cmd(cmdtest.NewOrdered(t, tt.git)).Build()
499+
lefthook := &Lefthook{
500+
fs: fs,
501+
repo: repo,
502+
}
503+
404504
// Create configuration file
405505
if len(tt.config) > 0 {
406506
assert.NoError(afero.WriteFile(fs, configPath, []byte(tt.config), 0o644))
@@ -413,8 +513,7 @@ commit-msg:
413513
}
414514

415515
// Create files that should exist
416-
for hook, content := range tt.existingHooks {
417-
path := hookPath(hook)
516+
for path, content := range tt.existingFiles {
418517
assert.NoError(fs.MkdirAll(filepath.Dir(path), 0o755))
419518
assert.NoError(afero.WriteFile(fs, path, []byte(content), 0o755))
420519
}
@@ -423,7 +522,7 @@ commit-msg:
423522
assert.NoError(err)
424523

425524
// Create hooks
426-
_, err = lefthook.syncHooks(cfg, false)
525+
_, err = lefthook.syncHooks(cfg, true)
427526
if tt.wantError {
428527
assert.Error(err)
429528
} else {

internal/command/run_test.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,16 @@ package command
22

33
import (
44
"fmt"
5-
"io"
65
"path/filepath"
76
"testing"
87

98
"github.com/spf13/afero"
109
"github.com/stretchr/testify/assert"
1110

12-
"github.com/evilmartians/lefthook/v2/internal/system"
11+
"github.com/evilmartians/lefthook/v2/tests/helpers/cmdtest"
1312
"github.com/evilmartians/lefthook/v2/tests/helpers/gittest"
1413
)
1514

16-
type gitCmd struct{}
17-
18-
func (g gitCmd) WithoutEnvs(...string) system.Command {
19-
return g
20-
}
21-
22-
func (g gitCmd) Run([]string, string, io.Reader, io.Writer, io.Writer) error {
23-
return nil
24-
}
25-
2615
func TestRun(t *testing.T) {
2716
root, err := filepath.Abs("src")
2817
if err != nil {
@@ -151,7 +140,7 @@ post-commit:
151140
fs := afero.NewMemMapFs()
152141
lefthook := &Lefthook{
153142
fs: fs,
154-
repo: gittest.NewRepositoryBuilder().Git(gitCmd{}).Fs(fs).Root(root).Build(),
143+
repo: gittest.NewRepositoryBuilder().Cmd(cmdtest.NewDumb()).Fs(fs).Root(root).Build(),
155144
}
156145
lefthook.repo.Setup()
157146

0 commit comments

Comments
 (0)