Skip to content

Commit

Permalink
Change the Linter
Browse files Browse the repository at this point in the history
The linter was changed to use the golangci-linter and was fixed
all the problems found.
  • Loading branch information
faabiosr committed Jan 21, 2020
1 parent 974ee54 commit a311e1e
Show file tree
Hide file tree
Showing 18 changed files with 139 additions and 147 deletions.
8 changes: 2 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
language: go

go:
- "1.9"
- "1.10"
- "1.13.x"
- master

services:
Expand All @@ -11,14 +10,11 @@ services:
- mongodb

before_install:
- wget -O- -nv https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.23.1
- go get github.com/axw/gocov/gocov
- go get github.com/mattn/goveralls
- if ! go get github.com/golang/tools/cmd/cover; then go get golang.org/x/tools/cmd/cover; fi

install:
- make depend
- make configure

script:
- make lint
- $HOME/gopath/bin/goveralls -service=travis-ci
16 changes: 1 addition & 15 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@ cover: test
go tool cover -html=./cover.out -o ./cover.html
.PHONY: cover

# Download dependencies
depend:
go get -u gopkg.in/alecthomas/gometalinter.v2
gometalinter.v2 --install
go get -u github.com/golang/dep/...
.PHONY: depend

# Up the docker container for testing
docker:
docker-compose up -d
Expand All @@ -29,14 +22,7 @@ fmt:

# Run linters
lint:
gometalinter.v2 \
--vendor \
--disable-all \
--enable=golint \
--enable=gofmt \
--enable=misspell \
--enable=vet ./... \
--deadline=60s
golangci-linter run ./...
.PHONY: lint

# Run tests
Expand Down
9 changes: 4 additions & 5 deletions bolt.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package cachego

import (
"encoding/json"
"time"

bolt "github.com/coreos/bbolt"
errors "github.com/pkg/errors"
"time"
)

var (
Expand Down Expand Up @@ -76,7 +77,7 @@ func (b *Bolt) read(key string) (*BoltContent, error) {
}

if content.Duration <= time.Now().Unix() {
b.Delete(key)
_ = b.Delete(key)
return nil, ErrBoltCacheExpired
}

Expand All @@ -101,9 +102,7 @@ func (b *Bolt) Delete(key string) error {
return ErrBoltBucketNotFound
}

bucket.Delete([]byte(key))

return nil
return bucket.Delete([]byte(key))
})

return err
Expand Down
27 changes: 14 additions & 13 deletions bolt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package cachego

import (
"fmt"
bolt "github.com/coreos/bbolt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"os"
"testing"
"time"

bolt "github.com/coreos/bbolt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)

type BoltTestSuite struct {
Expand All @@ -22,7 +23,7 @@ type BoltTestSuite struct {
func (s *BoltTestSuite) SetupTest() {
s.directory = "./cache-dir/"

os.Mkdir(s.directory, 0777)
_ = os.Mkdir(s.directory, 0777)

db, err := bolt.Open(s.directory+"cachego.db", 0600, nil)

Expand Down Expand Up @@ -75,7 +76,7 @@ func (s *BoltTestSuite) TestFetchThrowErrorWhenExpired() {
key := "foo"
value := "bar"

s.cache.Save(key, value, 1*time.Second)
_ = s.cache.Save(key, value, 1*time.Second)

time.Sleep(1 * time.Second)

Expand All @@ -89,7 +90,7 @@ func (s *BoltTestSuite) TestFetch() {
key := "foo"
value := "bar"

s.cache.Save(key, value, 0)
_ = s.cache.Save(key, value, 0)
result, err := s.cache.Fetch(key)

s.assert.Nil(err)
Expand All @@ -100,15 +101,15 @@ func (s *BoltTestSuite) TestFetchLongCacheDuration() {
key := "foo"
value := "bar"

s.cache.Save(key, value, 10*time.Second)
_ = s.cache.Save(key, value, 10*time.Second)
result, err := s.cache.Fetch(key)

s.assert.Nil(err)
s.assert.Equal(value, result)
}

func (s *BoltTestSuite) TestContains() {
s.cache.Save("foo", "bar", 0)
_ = s.cache.Save("foo", "bar", 0)

s.assert.True(s.cache.Contains("foo"))
s.assert.False(s.cache.Contains("bar"))
Expand All @@ -123,7 +124,7 @@ func (s *BoltTestSuite) TestDeleteThrowErrorWhenBucketNotFound() {
}

func (s *BoltTestSuite) TestDelete() {
s.cache.Save("foo", "bar", 0)
_ = s.cache.Save("foo", "bar", 0)

s.assert.Nil(s.cache.Delete("foo"))
s.assert.False(s.cache.Contains("foo"))
Expand All @@ -138,7 +139,7 @@ func (s *BoltTestSuite) TestFlushThrowErrorWhenBucketNotFound() {
}

func (s *BoltTestSuite) TestFlush() {
s.cache.Save("foo", "bar", 0)
_ = s.cache.Save("foo", "bar", 0)

s.assert.Nil(s.cache.Flush())
s.assert.False(s.cache.Contains("foo"))
Expand All @@ -152,16 +153,16 @@ func (s *BoltTestSuite) TestFetchMultiReturnNoItemsWhenThrowError() {
}

func (s *BoltTestSuite) TestFetchMulti() {
s.cache.Save("foo", "bar", 0)
s.cache.Save("john", "doe", 0)
_ = s.cache.Save("foo", "bar", 0)
_ = s.cache.Save("john", "doe", 0)

result := s.cache.FetchMulti([]string{"foo", "john"})

s.assert.Len(result, 2)
}

func (s *BoltTestSuite) TestFetchMultiWhenOnlyOneOfKeysExists() {
s.cache.Save("foo", "bar", 0)
_ = s.cache.Save("foo", "bar", 0)

result := s.cache.FetchMulti([]string{"foo", "alice"})

Expand Down
2 changes: 1 addition & 1 deletion chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (c *Chain) Fetch(key string) (string, error) {
for _, driver := range c.drivers {
value, err := driver.Fetch(key)

if driver.Fetch(key); err == nil {
if err == nil {
return value, nil
}

Expand Down
21 changes: 11 additions & 10 deletions chain_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package cachego

import (
"testing"
"time"

"github.com/bradfitz/gomemcache/memcache"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"testing"
"time"
)

type ChainTestSuite struct {
Expand Down Expand Up @@ -38,7 +39,7 @@ func (s *ChainTestSuite) TestFetchThrowErrorWhenExpired() {
key := "foo"
value := "bar"

s.cache.Save(key, value, 1*time.Second)
_ = s.cache.Save(key, value, 1*time.Second)

time.Sleep(1 * time.Second)

Expand All @@ -52,7 +53,7 @@ func (s *ChainTestSuite) TestFetch() {
key := "foo"
value := "bar"

s.cache.Save(key, value, 0)
_ = s.cache.Save(key, value, 0)

result, err := s.cache.Fetch(key)

Expand All @@ -61,7 +62,7 @@ func (s *ChainTestSuite) TestFetch() {
}

func (s *ChainTestSuite) TestContains() {
s.cache.Save("foo", "bar", 0)
_ = s.cache.Save("foo", "bar", 0)

s.assert.True(s.cache.Contains("foo"))
s.assert.False(s.cache.Contains("bar"))
Expand All @@ -77,7 +78,7 @@ func (s *ChainTestSuite) TestDeleteThrowErrorWhenOneOfDriverFail() {
}

func (s *ChainTestSuite) TestDelete() {
s.cache.Save("foo", "bar", 0)
_ = s.cache.Save("foo", "bar", 0)

s.assert.Nil(s.cache.Delete("foo"))
s.assert.False(s.cache.Contains("foo"))
Expand All @@ -93,23 +94,23 @@ func (s *ChainTestSuite) TestFlushThrowErrorWhenOneOfDriverFail() {
}

func (s *ChainTestSuite) TestFlush() {
s.cache.Save("foo", "bar", 0)
_ = s.cache.Save("foo", "bar", 0)

s.assert.Nil(s.cache.Flush())
s.assert.False(s.cache.Contains("foo"))
}

func (s *ChainTestSuite) TestFetchMulti() {
s.cache.Save("foo", "bar", 0)
s.cache.Save("john", "doe", 0)
_ = s.cache.Save("foo", "bar", 0)
_ = s.cache.Save("john", "doe", 0)

result := s.cache.FetchMulti([]string{"foo", "john"})

s.assert.Len(result, 2)
}

func (s *ChainTestSuite) TestFetchMultiWhenOnlyOneOfKeysExists() {
s.cache.Save("foo", "bar", 0)
_ = s.cache.Save("foo", "bar", 0)

result := s.cache.FetchMulti([]string{"foo", "alice"})

Expand Down
7 changes: 4 additions & 3 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
errors "github.com/pkg/errors"
"io/ioutil"
"os"
"path/filepath"
"time"

errors "github.com/pkg/errors"
)

type (
Expand All @@ -31,7 +32,7 @@ func NewFile(dir string) *File {

func (f *File) createName(key string) string {
h := sha256.New()
h.Write([]byte(key))
_, _ = h.Write([]byte(key))
hash := hex.EncodeToString(h.Sum(nil))

filename := hash + ".cachego"
Expand Down Expand Up @@ -63,7 +64,7 @@ func (f *File) read(key string) (*FileContent, error) {
}

if content.Duration <= time.Now().Unix() {
f.Delete(key)
_ = f.Delete(key)
return nil, errors.New("Cache expired")
}

Expand Down

0 comments on commit a311e1e

Please sign in to comment.