Skip to content

Commit 48b0644

Browse files
authored
Merge pull request #20 from pjbgf/improve-tests
Refactoring and removing check.v1
2 parents 46037e5 + 0cecebc commit 48b0644

File tree

16 files changed

+909
-225
lines changed

16 files changed

+909
-225
lines changed

.github/workflows/test.yml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,22 @@ jobs:
99
strategy:
1010
fail-fast: false
1111
matrix:
12-
go-version: [1.19.x, 1.20.x, 1.21.x]
12+
go-version: [1.20.x, 1.21.x, 1.22.x]
1313
platform: [ubuntu-latest, macos-latest, windows-latest]
1414

1515
runs-on: ${{ matrix.platform }}
1616
steps:
17+
- name: Checkout code
18+
uses: actions/checkout@v4
19+
1720
- name: Install Go
1821
uses: actions/setup-go@v5
1922
with:
20-
go-version: ${{ matrix.go-version }}
23+
go-version: ${{ matrix.go-version }}
2124

22-
- name: Checkout code
23-
uses: actions/checkout@v4
25+
- name: Validate
26+
if: matrix.platform == 'ubuntu-latest'
27+
run: make validate
2428

2529
- name: Test
2630
run: make test

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build/

.golangci.yaml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
linters:
2+
disable-all: true
3+
enable:
4+
- asasalint
5+
- asciicheck
6+
- bidichk
7+
- bodyclose
8+
- containedctx
9+
- contextcheck
10+
- decorder
11+
- dogsled
12+
- dupl
13+
- dupword
14+
- durationcheck
15+
- errcheck
16+
- errchkjson
17+
- errname
18+
- errorlint
19+
- execinquery
20+
- exhaustive
21+
- exportloopref
22+
- forcetypeassert
23+
- ginkgolinter
24+
- gocheckcompilerdirectives
25+
- gochecknoinits
26+
- gochecksumtype
27+
- goconst
28+
- gofmt
29+
- goheader
30+
- goimports
31+
- gomodguard
32+
- goprintffuncname
33+
- gosec
34+
- gosimple
35+
- gosmopolitan
36+
- govet
37+
- grouper
38+
- importas
39+
- ineffassign
40+
- loggercheck
41+
- makezero
42+
- mirror
43+
- misspell
44+
- nakedret
45+
- nestif
46+
- nilerr
47+
- nilnil
48+
- noctx
49+
- nolintlint
50+
- nosprintfhostport
51+
- prealloc
52+
- predeclared
53+
- promlinter
54+
- reassign
55+
- revive
56+
- rowserrcheck
57+
- sloglint
58+
- spancheck
59+
- sqlclosecheck
60+
- stylecheck
61+
- tagalign
62+
- tagliatelle
63+
- tenv
64+
- testableexamples
65+
- testifylint
66+
- thelper
67+
- typecheck
68+
- unconvert
69+
- unparam
70+
- unused
71+
- usestdlibvars
72+
- wastedassign
73+
- whitespace
74+
- zerologlint

Makefile

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,35 @@
1-
# Go parameters
21
GOCMD = go
32
GOTEST = $(GOCMD) test
43

4+
GOLANGCI_VERSION ?= v1.57.2
5+
TOOLS_BIN := $(shell mkdir -p build/tools && realpath build/tools)
6+
7+
GOLANGCI = $(TOOLS_BIN)/golangci-lint-$(GOLANGCI_VERSION)
8+
$(GOLANGCI):
9+
rm -f $(TOOLS_BIN)/golangci-lint*
10+
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/$(GOLANGCI_VERSION)/install.sh | sh -s -- -b $(TOOLS_BIN) $(GOLANGCI_VERSION)
11+
mv $(TOOLS_BIN)/golangci-lint $(TOOLS_BIN)/golangci-lint-$(GOLANGCI_VERSION)
12+
513
test:
614
$(GOTEST) ./...
715

8-
generate: $(esc)
9-
$(GOCMD) generate
16+
validate: validate-lint validate-dirty ## Run validation checks.
17+
18+
validate-lint: $(GOLANGCI)
19+
$(GOLANGCI) run
20+
21+
define go-install-tool
22+
@[ -f $(1) ] || { \
23+
set -e ;\
24+
echo "Downloading $(2)" ;\
25+
GOBIN=$(TOOLS_BIN) go install $(2) ;\
26+
}
27+
endef
28+
29+
validate-dirty:
30+
ifneq ($(shell git status --porcelain --untracked-files=no),)
31+
@echo worktree is dirty
32+
@git --no-pager status
33+
@git --no-pager diff
34+
@exit 1
35+
endif

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,3 @@ ls .git/objects/pack/
2626
PackfileHash: "<PACK_HASH>",
2727
}
2828
```
29-
30-
4. Run `make generate`.

fixtures.go

Lines changed: 41 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,11 @@ import (
99
"testing"
1010

1111
"github.com/go-git/go-billy/v5"
12-
"github.com/go-git/go-billy/v5/osfs"
13-
"github.com/go-git/go-git-fixtures/v4/internal/tgz"
14-
"gopkg.in/check.v1"
12+
"github.com/go-git/go-git-fixtures/v5/internal/embedfs"
13+
"github.com/go-git/go-git-fixtures/v5/internal/tgz"
1514
)
1615

17-
var (
18-
files = make(map[string]string)
19-
Filesystem = osfs.New(os.TempDir())
20-
)
16+
var Filesystem = embedfs.New(&data)
2117

2218
//go:embed data
2319
var data embed.FS
@@ -231,35 +227,8 @@ func (f *Fixture) Is(tag string) bool {
231227
return false
232228
}
233229

234-
func (f *Fixture) file(path string) (billy.File, error) {
235-
if fpath, ok := files[path]; ok {
236-
return Filesystem.Open(fpath)
237-
}
238-
239-
bytes, err := data.ReadFile("data/" + path)
240-
if err != nil {
241-
return nil, err
242-
}
243-
244-
file, err := Filesystem.TempFile("", "go-git-fixtures")
245-
if err != nil {
246-
return nil, err
247-
}
248-
249-
if _, err := file.Write(bytes); err != nil {
250-
return nil, err
251-
}
252-
253-
if err := file.Close(); err != nil {
254-
return nil, err
255-
}
256-
257-
files[path] = file.Name()
258-
return Filesystem.Open(file.Name())
259-
}
260-
261230
func (f *Fixture) Packfile() billy.File {
262-
file, err := f.file(fmt.Sprintf("pack-%s.pack", f.PackfileHash))
231+
file, err := Filesystem.Open(fmt.Sprintf("data/pack-%s.pack", f.PackfileHash))
263232
if err != nil {
264233
panic(err)
265234
}
@@ -268,7 +237,7 @@ func (f *Fixture) Packfile() billy.File {
268237
}
269238

270239
func (f *Fixture) Idx() billy.File {
271-
file, err := f.file(fmt.Sprintf("pack-%s.idx", f.PackfileHash))
240+
file, err := Filesystem.Open(fmt.Sprintf("data/pack-%s.idx", f.PackfileHash))
272241
if err != nil {
273242
panic(err)
274243
}
@@ -277,7 +246,7 @@ func (f *Fixture) Idx() billy.File {
277246
}
278247

279248
func (f *Fixture) Rev() billy.File {
280-
file, err := f.file(fmt.Sprintf("pack-%s.rev", f.PackfileHash))
249+
file, err := Filesystem.Open(fmt.Sprintf("data/pack-%s.rev", f.PackfileHash))
281250
if err != nil {
282251
panic(err)
283252
}
@@ -287,18 +256,28 @@ func (f *Fixture) Rev() billy.File {
287256

288257
// DotGit creates a new temporary directory and unpacks the repository .git
289258
// directory into it. Multiple calls to DotGit returns different directories.
290-
func (f *Fixture) DotGit() billy.Filesystem {
259+
func (f *Fixture) DotGit(opts ...Option) billy.Filesystem {
260+
o := newOptions()
261+
for _, opt := range opts {
262+
opt(o)
263+
}
264+
291265
if f.DotGitHash == "" && f.WorktreeHash != "" {
292-
fs, _ := f.Worktree().Chroot(".git")
293-
return fs.(billy.Filesystem)
266+
fs, _ := f.Worktree(opts...).Chroot(".git")
267+
return fs
294268
}
295269

296-
file, err := f.file(fmt.Sprintf("git-%s.tgz", f.DotGitHash))
270+
file, err := Filesystem.Open(fmt.Sprintf("data/git-%s.tgz", f.DotGitHash))
297271
if err != nil {
298272
panic(err)
299273
}
300274

301-
fs, err, _ := tgz.Extract(Filesystem, file.Name())
275+
fs, err := o.fsFactory()
276+
if err != nil {
277+
panic(err)
278+
}
279+
280+
err = tgz.Extract(file, fs)
302281
if err != nil {
303282
panic(err)
304283
}
@@ -332,13 +311,23 @@ func EnsureIsBare(fs billy.Filesystem) error {
332311
return err
333312
}
334313

335-
func (f *Fixture) Worktree() billy.Filesystem {
336-
file, err := f.file(fmt.Sprintf("worktree-%s.tgz", f.WorktreeHash))
314+
func (f *Fixture) Worktree(opts ...Option) billy.Filesystem {
315+
o := newOptions()
316+
for _, opt := range opts {
317+
opt(o)
318+
}
319+
320+
file, err := Filesystem.Open(fmt.Sprintf("data/worktree-%s.tgz", f.WorktreeHash))
321+
if err != nil {
322+
panic(err)
323+
}
324+
325+
fs, err := o.fsFactory()
337326
if err != nil {
338327
panic(err)
339328
}
340329

341-
fs, err, _ := tgz.Extract(Filesystem, file.Name())
330+
err = tgz.Extract(file, fs)
342331
if err != nil {
343332
panic(err)
344333
}
@@ -348,17 +337,9 @@ func (f *Fixture) Worktree() billy.Filesystem {
348337

349338
type Fixtures []*Fixture
350339

351-
// Deprecated as part of removing check from the code base.
352-
// Use Run instead.
353-
func (g Fixtures) Test(c *check.C, test func(*Fixture)) {
354-
for _, f := range g {
355-
c.Logf("executing test at %s %s", f.URL, f.Tags)
356-
test(f)
357-
}
358-
}
359-
360340
// Run calls test within a t.Run for each fixture in g.
361341
func (g Fixtures) Run(t *testing.T, test func(*testing.T, *Fixture)) {
342+
t.Helper()
362343
for _, f := range g {
363344
name := fmt.Sprintf("fixture run (%q, %q)", f.URL, f.Tags)
364345
t.Run(name, func(t *testing.T) {
@@ -368,11 +349,14 @@ func (g Fixtures) Run(t *testing.T, test func(*testing.T, *Fixture)) {
368349
}
369350

370351
func (g Fixtures) One() *Fixture {
352+
if len(g) == 0 {
353+
return nil
354+
}
371355
return g[0]
372356
}
373357

374358
func (g Fixtures) ByTag(tag string) Fixtures {
375-
r := make(Fixtures, 0)
359+
r := make(Fixtures, 0, len(g))
376360
for _, f := range g {
377361
if f.Is(tag) {
378362
r = append(r, f)
@@ -383,7 +367,7 @@ func (g Fixtures) ByTag(tag string) Fixtures {
383367
}
384368

385369
func (g Fixtures) ByURL(url string) Fixtures {
386-
r := make(Fixtures, 0)
370+
r := make(Fixtures, 0, len(g))
387371
for _, f := range g {
388372
if f.URL == url {
389373
r = append(r, f)
@@ -394,7 +378,7 @@ func (g Fixtures) ByURL(url string) Fixtures {
394378
}
395379

396380
func (g Fixtures) Exclude(tag string) Fixtures {
397-
r := make(Fixtures, 0)
381+
r := make(Fixtures, 0, len(g))
398382
for _, f := range g {
399383
if !f.Is(tag) {
400384
r = append(r, f)
@@ -403,21 +387,3 @@ func (g Fixtures) Exclude(tag string) Fixtures {
403387

404388
return r
405389
}
406-
407-
// Clean cleans all the temporal files created
408-
func Clean() error {
409-
for fname, f := range files {
410-
if err := Filesystem.Remove(f); err != nil {
411-
return err
412-
}
413-
delete(files, fname)
414-
}
415-
return nil
416-
}
417-
418-
type Suite struct{}
419-
420-
// Deprecated as part of removing check from the code base.
421-
func (s *Suite) TearDownSuite(c *check.C) {
422-
Clean()
423-
}

0 commit comments

Comments
 (0)