Skip to content

Commit

Permalink
Merge pull request #20 from pjbgf/improve-tests
Browse files Browse the repository at this point in the history
Refactoring and removing check.v1
  • Loading branch information
pjbgf authored May 20, 2024
2 parents 46037e5 + 0cecebc commit 48b0644
Show file tree
Hide file tree
Showing 16 changed files with 909 additions and 225 deletions.
12 changes: 8 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,22 @@ jobs:
strategy:
fail-fast: false
matrix:
go-version: [1.19.x, 1.20.x, 1.21.x]
go-version: [1.20.x, 1.21.x, 1.22.x]
platform: [ubuntu-latest, macos-latest, windows-latest]

runs-on: ${{ matrix.platform }}
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Install Go
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}
go-version: ${{ matrix.go-version }}

- name: Checkout code
uses: actions/checkout@v4
- name: Validate
if: matrix.platform == 'ubuntu-latest'
run: make validate

- name: Test
run: make test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/
74 changes: 74 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
linters:
disable-all: true
enable:
- asasalint
- asciicheck
- bidichk
- bodyclose
- containedctx
- contextcheck
- decorder
- dogsled
- dupl
- dupword
- durationcheck
- errcheck
- errchkjson
- errname
- errorlint
- execinquery
- exhaustive
- exportloopref
- forcetypeassert
- ginkgolinter
- gocheckcompilerdirectives
- gochecknoinits
- gochecksumtype
- goconst
- gofmt
- goheader
- goimports
- gomodguard
- goprintffuncname
- gosec
- gosimple
- gosmopolitan
- govet
- grouper
- importas
- ineffassign
- loggercheck
- makezero
- mirror
- misspell
- nakedret
- nestif
- nilerr
- nilnil
- noctx
- nolintlint
- nosprintfhostport
- prealloc
- predeclared
- promlinter
- reassign
- revive
- rowserrcheck
- sloglint
- spancheck
- sqlclosecheck
- stylecheck
- tagalign
- tagliatelle
- tenv
- testableexamples
- testifylint
- thelper
- typecheck
- unconvert
- unparam
- unused
- usestdlibvars
- wastedassign
- whitespace
- zerologlint
32 changes: 29 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,35 @@
# Go parameters
GOCMD = go
GOTEST = $(GOCMD) test

GOLANGCI_VERSION ?= v1.57.2
TOOLS_BIN := $(shell mkdir -p build/tools && realpath build/tools)

GOLANGCI = $(TOOLS_BIN)/golangci-lint-$(GOLANGCI_VERSION)
$(GOLANGCI):
rm -f $(TOOLS_BIN)/golangci-lint*
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/$(GOLANGCI_VERSION)/install.sh | sh -s -- -b $(TOOLS_BIN) $(GOLANGCI_VERSION)
mv $(TOOLS_BIN)/golangci-lint $(TOOLS_BIN)/golangci-lint-$(GOLANGCI_VERSION)

test:
$(GOTEST) ./...

generate: $(esc)
$(GOCMD) generate
validate: validate-lint validate-dirty ## Run validation checks.

validate-lint: $(GOLANGCI)
$(GOLANGCI) run

define go-install-tool
@[ -f $(1) ] || { \
set -e ;\
echo "Downloading $(2)" ;\
GOBIN=$(TOOLS_BIN) go install $(2) ;\
}
endef

validate-dirty:
ifneq ($(shell git status --porcelain --untracked-files=no),)
@echo worktree is dirty
@git --no-pager status
@git --no-pager diff
@exit 1
endif
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,3 @@ ls .git/objects/pack/
PackfileHash: "<PACK_HASH>",
}
```

4. Run `make generate`.
116 changes: 41 additions & 75 deletions fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,11 @@ import (
"testing"

"github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/osfs"
"github.com/go-git/go-git-fixtures/v4/internal/tgz"
"gopkg.in/check.v1"
"github.com/go-git/go-git-fixtures/v5/internal/embedfs"
"github.com/go-git/go-git-fixtures/v5/internal/tgz"
)

var (
files = make(map[string]string)
Filesystem = osfs.New(os.TempDir())
)
var Filesystem = embedfs.New(&data)

//go:embed data
var data embed.FS
Expand Down Expand Up @@ -231,35 +227,8 @@ func (f *Fixture) Is(tag string) bool {
return false
}

func (f *Fixture) file(path string) (billy.File, error) {
if fpath, ok := files[path]; ok {
return Filesystem.Open(fpath)
}

bytes, err := data.ReadFile("data/" + path)
if err != nil {
return nil, err
}

file, err := Filesystem.TempFile("", "go-git-fixtures")
if err != nil {
return nil, err
}

if _, err := file.Write(bytes); err != nil {
return nil, err
}

if err := file.Close(); err != nil {
return nil, err
}

files[path] = file.Name()
return Filesystem.Open(file.Name())
}

func (f *Fixture) Packfile() billy.File {
file, err := f.file(fmt.Sprintf("pack-%s.pack", f.PackfileHash))
file, err := Filesystem.Open(fmt.Sprintf("data/pack-%s.pack", f.PackfileHash))
if err != nil {
panic(err)
}
Expand All @@ -268,7 +237,7 @@ func (f *Fixture) Packfile() billy.File {
}

func (f *Fixture) Idx() billy.File {
file, err := f.file(fmt.Sprintf("pack-%s.idx", f.PackfileHash))
file, err := Filesystem.Open(fmt.Sprintf("data/pack-%s.idx", f.PackfileHash))
if err != nil {
panic(err)
}
Expand All @@ -277,7 +246,7 @@ func (f *Fixture) Idx() billy.File {
}

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

// DotGit creates a new temporary directory and unpacks the repository .git
// directory into it. Multiple calls to DotGit returns different directories.
func (f *Fixture) DotGit() billy.Filesystem {
func (f *Fixture) DotGit(opts ...Option) billy.Filesystem {
o := newOptions()
for _, opt := range opts {
opt(o)
}

if f.DotGitHash == "" && f.WorktreeHash != "" {
fs, _ := f.Worktree().Chroot(".git")
return fs.(billy.Filesystem)
fs, _ := f.Worktree(opts...).Chroot(".git")
return fs
}

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

fs, err, _ := tgz.Extract(Filesystem, file.Name())
fs, err := o.fsFactory()
if err != nil {
panic(err)
}

err = tgz.Extract(file, fs)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -332,13 +311,23 @@ func EnsureIsBare(fs billy.Filesystem) error {
return err
}

func (f *Fixture) Worktree() billy.Filesystem {
file, err := f.file(fmt.Sprintf("worktree-%s.tgz", f.WorktreeHash))
func (f *Fixture) Worktree(opts ...Option) billy.Filesystem {
o := newOptions()
for _, opt := range opts {
opt(o)
}

file, err := Filesystem.Open(fmt.Sprintf("data/worktree-%s.tgz", f.WorktreeHash))
if err != nil {
panic(err)
}

fs, err := o.fsFactory()
if err != nil {
panic(err)
}

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

type Fixtures []*Fixture

// Deprecated as part of removing check from the code base.
// Use Run instead.
func (g Fixtures) Test(c *check.C, test func(*Fixture)) {
for _, f := range g {
c.Logf("executing test at %s %s", f.URL, f.Tags)
test(f)
}
}

// Run calls test within a t.Run for each fixture in g.
func (g Fixtures) Run(t *testing.T, test func(*testing.T, *Fixture)) {
t.Helper()
for _, f := range g {
name := fmt.Sprintf("fixture run (%q, %q)", f.URL, f.Tags)
t.Run(name, func(t *testing.T) {
Expand All @@ -368,11 +349,14 @@ func (g Fixtures) Run(t *testing.T, test func(*testing.T, *Fixture)) {
}

func (g Fixtures) One() *Fixture {
if len(g) == 0 {
return nil
}
return g[0]
}

func (g Fixtures) ByTag(tag string) Fixtures {
r := make(Fixtures, 0)
r := make(Fixtures, 0, len(g))
for _, f := range g {
if f.Is(tag) {
r = append(r, f)
Expand All @@ -383,7 +367,7 @@ func (g Fixtures) ByTag(tag string) Fixtures {
}

func (g Fixtures) ByURL(url string) Fixtures {
r := make(Fixtures, 0)
r := make(Fixtures, 0, len(g))
for _, f := range g {
if f.URL == url {
r = append(r, f)
Expand All @@ -394,7 +378,7 @@ func (g Fixtures) ByURL(url string) Fixtures {
}

func (g Fixtures) Exclude(tag string) Fixtures {
r := make(Fixtures, 0)
r := make(Fixtures, 0, len(g))
for _, f := range g {
if !f.Is(tag) {
r = append(r, f)
Expand All @@ -403,21 +387,3 @@ func (g Fixtures) Exclude(tag string) Fixtures {

return r
}

// Clean cleans all the temporal files created
func Clean() error {
for fname, f := range files {
if err := Filesystem.Remove(f); err != nil {
return err
}
delete(files, fname)
}
return nil
}

type Suite struct{}

// Deprecated as part of removing check from the code base.
func (s *Suite) TearDownSuite(c *check.C) {
Clean()
}
Loading

0 comments on commit 48b0644

Please sign in to comment.