Skip to content

Commit

Permalink
build: Add make validate to run golangci linters
Browse files Browse the repository at this point in the history
Signed-off-by: Paulo Gomes <[email protected]>
  • Loading branch information
pjbgf committed Apr 26, 2024
1 parent 0e9e048 commit 43baec7
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 57 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,9 @@ jobs:
with:
go-version: ${{ matrix.go-version }}

- 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
30 changes: 30 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
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) ./...

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
3 changes: 2 additions & 1 deletion fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ func (f *Fixture) DotGit(opts ...Option) billy.Filesystem {

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

file, err := Filesystem.Open(fmt.Sprintf("data/git-%s.tgz", f.DotGitHash))
Expand Down Expand Up @@ -329,6 +329,7 @@ type Fixtures []*Fixture

// 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 Down
5 changes: 0 additions & 5 deletions fixtures_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ import (
"github.com/go-git/go-git-fixtures/v5/internal/tgz"
)

const (
useDefaultTempDir = ""
tmpPrefix = "tmp-tgz-"
)

type Option func(*options)

type options struct {
Expand Down
15 changes: 11 additions & 4 deletions fixtures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,26 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestDotGit(t *testing.T) {
t.Parallel()

fs := Basic().One().DotGit(WithTargetDir(t.TempDir))
files, err := fs.ReadDir("/")
assert.NoError(t, err)
assert.True(t, len(files) > 1)
require.NoError(t, err)
assert.Greater(t, len(files), 1)

fs = Basic().One().DotGit(WithMemFS())
files, err = fs.ReadDir("/")
assert.NoError(t, err)
assert.True(t, len(files) > 1)
require.NoError(t, err)
assert.Greater(t, len(files), 1)
}

func TestEmbeddedFiles(t *testing.T) {
t.Parallel()

for i, f := range fixtures {
if f.PackfileHash != "" {
if f.Packfile() == nil {
Expand Down Expand Up @@ -53,6 +58,8 @@ func TestEmbeddedFiles(t *testing.T) {
}

func TestRevFiles(t *testing.T) {
t.Parallel()

f := ByTag("packfile-sha256").One()

assert.NotNil(t, f)
Expand Down
22 changes: 10 additions & 12 deletions internal/embedfs/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (fs *Embed) Open(filename string) (billy.File, error) {
return fs.OpenFile(filename, os.O_RDONLY, 0)
}

func (fs *Embed) OpenFile(filename string, flag int, perm os.FileMode) (billy.File, error) {
func (fs *Embed) OpenFile(filename string, flag int, _ os.FileMode) (billy.File, error) {
if flag&(os.O_CREATE|os.O_WRONLY|os.O_APPEND|os.O_RDWR|os.O_EXCL|os.O_TRUNC) != 0 {
return nil, billy.ErrReadOnly
}
Expand Down Expand Up @@ -96,7 +96,7 @@ func (fs *Embed) ReadDir(path string) ([]os.FileInfo, error) {
return nil, err
}

var entries []os.FileInfo
entries := make([]os.FileInfo, 0, len(e))
for _, f := range e {
fi, _ := f.Info()
entries = append(entries, fi)
Expand All @@ -110,7 +110,7 @@ func (fs *Embed) ReadDir(path string) ([]os.FileInfo, error) {
// Chroot is not supported.
//
// Calls will always return billy.ErrNotSupported.
func (fs *Embed) Chroot(path string) (billy.Filesystem, error) {
func (fs *Embed) Chroot(_ string) (billy.Filesystem, error) {
return nil, billy.ErrNotSupported
}

Expand Down Expand Up @@ -145,38 +145,36 @@ func (fs *Embed) Symlink(_, _ string) error {
// Create is not supported.
//
// Calls will always return billy.ErrReadOnly.
func (fs *Embed) Create(filename string) (billy.File, error) {
func (fs *Embed) Create(_ string) (billy.File, error) {
return nil, billy.ErrReadOnly
}

// Rename is not supported.
//
// Calls will always return billy.ErrReadOnly.
func (fs *Embed) Rename(from, to string) error {
func (fs *Embed) Rename(_, _ string) error {
return billy.ErrReadOnly
}

// Remove is not supported.
//
// Calls will always return billy.ErrReadOnly.
func (fs *Embed) Remove(filename string) error {
func (fs *Embed) Remove(_ string) error {
return billy.ErrReadOnly
}

// MkdirAll is not supported.
//
// Calls will always return billy.ErrReadOnly.
func (fs *Embed) MkdirAll(filename string, perm os.FileMode) error {
func (fs *Embed) MkdirAll(_ string, _ os.FileMode) error {
return billy.ErrReadOnly
}

func toFile(lazy func() *bytes.Reader, fi fs.FileInfo) billy.File {
new := &file{
return &file{
lazy: lazy,
fi: fi,
}

return new
}

type file struct {
Expand Down Expand Up @@ -234,13 +232,13 @@ func (f *file) Unlock() error {
// Truncate is not supported.
//
// Calls will always return billy.ErrReadOnly.
func (f *file) Truncate(size int64) error {
func (f *file) Truncate(_ int64) error {
return billy.ErrReadOnly
}

// Write is not supported.
//
// Calls will always return billy.ErrReadOnly.
func (f *file) Write(p []byte) (int, error) {
func (f *file) Write(_ []byte) (int, error) {
return 0, billy.ErrReadOnly
}
Loading

0 comments on commit 43baec7

Please sign in to comment.