Skip to content

Commit 5c9310e

Browse files
authored
Add golangci-lint to the Travis tests (#36)
This also fixes some issues that golangci-lint identified.
1 parent 3372b69 commit 5c9310e

File tree

8 files changed

+55
-60
lines changed

8 files changed

+55
-60
lines changed

.travis.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
language: go
22
go: 1.13
33
sudo: false
4+
5+
before_install:
6+
- curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | bash -s -- -b $GOPATH/bin v1.24.0
7+
8+
script:
9+
- $GOPATH/bin/golangci-lint run
10+
- go test ./...

pkg/avancement/service_manager.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const (
2929

3030
type scmClientFactory func(token string) *scm.Client
3131
type repoFactory func(url, localPath string, debug bool) (git.Repo, error)
32-
type localFactory func(localPath string, debug bool) (git.Source, error)
32+
type localFactory func(localPath string, debug bool) git.Source
3333
type serviceOpt func(*ServiceManager)
3434

3535
// New creates and returns a new ServiceManager.
@@ -45,9 +45,9 @@ func New(cacheDir string, author *git.Author, opts ...serviceOpt) *ServiceManage
4545
r, err := git.NewRepository(url, localPath, debug)
4646
return git.Repo(r), err
4747
},
48-
localFactory: func(localPath string, debug bool)(git.Source, error){
48+
localFactory: func(localPath string, debug bool) git.Source {
4949
l := &local.Local{LocalPath: localPath, Debug: debug, Logger: log.Printf}
50-
return git.Source(l), nil
50+
return git.Source(l)
5151
},
5252
}
5353
for _, o := range opts {
@@ -79,9 +79,9 @@ func (s *ServiceManager) Promote(serviceName, fromURL, toURL, newBranchName stri
7979
defer func(keepRepos bool, repos *[]git.Repo) {
8080
if !keepRepos {
8181
for _, repo := range *repos {
82-
err := repo.DeleteCachedRepo()
82+
err := repo.DeleteCache()
8383
if err != nil {
84-
fmt.Errorf("failed deleting files from cache: %w", err)
84+
log.Printf("failed deleting files from cache: %s", err)
8585
}
8686
}
8787
}
@@ -93,9 +93,9 @@ func (s *ServiceManager) Promote(serviceName, fromURL, toURL, newBranchName stri
9393
}
9494
reposToDelete = append(reposToDelete, destination)
9595

96-
copied := []string{}
96+
var copied []string
9797
if fromLocalRepo(fromURL) {
98-
localSource, err := s.localFactory(fromURL, s.debug)
98+
localSource := s.localFactory(fromURL, s.debug)
9999
copied, err = local.CopyConfig(serviceName, localSource, destination)
100100
if err != nil {
101101
return fmt.Errorf("failed to setup local repo: %w", err)

pkg/avancement/service_manager_test.go

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package avancement
22

33
import (
4-
"testing"
4+
"errors"
5+
"path"
56
"path/filepath"
67
"strings"
7-
"path"
8+
"testing"
89

910
"github.com/jenkins-x/go-scm/scm"
1011
fakescm "github.com/jenkins-x/go-scm/scm/driver/fake"
@@ -29,17 +30,16 @@ func TestPromoteWithSuccessKeepCacheFalse(t *testing.T) {
2930
func promoteWithSuccess(t *testing.T, keepCache bool) {
3031
dstBranch := "test-branch"
3132
author := &git.Author{Name: "Testing User", Email: "[email protected]", Token: "test-token"}
32-
client, _ := fakescm.NewDefault()
33-
fakeClientFactory := func(s string) *scm.Client {
34-
return client
35-
}
3633
devRepo, stagingRepo := mock.New("/dev", "master"), mock.New("/staging", "master")
3734
repos := map[string]*mock.Repository{
3835
mustAddCredentials(t, dev, author): devRepo,
3936
mustAddCredentials(t, staging, author): stagingRepo,
4037
}
4138
sm := New("tmp", author)
42-
sm.clientFactory = fakeClientFactory
39+
sm.clientFactory = func(s string) *scm.Client {
40+
client, _ := fakescm.NewDefault()
41+
return client
42+
}
4343
sm.repoFactory = func(url, _ string, _ bool) (git.Repo, error) {
4444
return git.Repo(repos[url]), nil
4545
}
@@ -75,20 +75,19 @@ func TestPromoteLocalWithSuccessKeepCacheTrue(t *testing.T) {
7575
func promoteLocalWithSuccess(t *testing.T, keepCache bool) {
7676
dstBranch := "test-branch"
7777
author := &git.Author{Name: "Testing User", Email: "[email protected]", Token: "test-token"}
78-
client, _ := fakescm.NewDefault()
79-
fakeClientFactory := func(s string) *scm.Client {
80-
return client
81-
}
8278
stagingRepo := mock.New("/staging", "master")
8379
devRepo := NewLocal("/dev")
8480

8581
sm := New("tmp", author)
86-
sm.clientFactory = fakeClientFactory
82+
sm.clientFactory = func(s string) *scm.Client {
83+
client, _ := fakescm.NewDefault()
84+
return client
85+
}
8786
sm.repoFactory = func(url, _ string, _ bool) (git.Repo, error) {
8887
return git.Repo(stagingRepo), nil
8988
}
90-
sm.localFactory = func(path string, _ bool) (git.Source, error) {
91-
return git.Source(devRepo), nil
89+
sm.localFactory = func(path string, _ bool) git.Source {
90+
return git.Source(devRepo)
9291
}
9392
sm.debug = true
9493
devRepo.AddFiles("/config/myfile.yaml")
@@ -143,19 +142,19 @@ func mustAddCredentials(t *testing.T, repoURL string, a *git.Author) string {
143142
}
144143

145144
func TestPromoteWithCacheDeletionFailure(t *testing.T) {
146-
dstBranch := mock.FAIL_DELETING_REPO_BRANCH
145+
dstBranch := "test-branch"
147146
author := &git.Author{Name: "Testing User", Email: "[email protected]", Token: "test-token"}
148-
client, _ := fakescm.NewDefault()
149-
fakeClientFactory := func(s string) *scm.Client {
150-
return client
151-
}
152147
devRepo, stagingRepo := mock.New("/dev", "master"), mock.New("/staging", "master")
148+
stagingRepo.DeleteErr = errors.New("failed test delete")
153149
repos := map[string]*mock.Repository{
154150
mustAddCredentials(t, dev, author): devRepo,
155151
mustAddCredentials(t, staging, author): stagingRepo,
156152
}
157153
sm := New("tmp", author)
158-
sm.clientFactory = fakeClientFactory
154+
sm.clientFactory = func(s string) *scm.Client {
155+
client, _ := fakescm.NewDefault()
156+
return client
157+
}
159158
sm.repoFactory = func(url, _ string, _ bool) (git.Repo, error) {
160159
return git.Repo(repos[url]), nil
161160
}
@@ -175,7 +174,6 @@ func TestPromoteWithCacheDeletionFailure(t *testing.T) {
175174
devRepo.AssertDeletedFromCache(t)
176175
}
177176

178-
179177
type mockSource struct {
180178
files []string
181179
localPath string
@@ -193,11 +191,11 @@ func NewLocal(localPath string) *mockSource {
193191
// and then calls filePath.Walk() on /full/path/to/repo/services/ .
194192
// When CopyService() drives Walk(), 'base' is typically services/service-name
195193
// Thus we take each /full/path/to/file/in/mockSource.files[] and split it at 'services/' as happens in the Walk() method we're mocking.
196-
func (s *mockSource) Walk(base string, cb func(string, string) error) error {
194+
func (s *mockSource) Walk(_ string, cb func(string, string) error) error {
197195
if s.files == nil {
198196
return nil
199197
}
200-
base = filepath.Join(s.localPath, "config")
198+
base := filepath.Join(s.localPath, "config")
201199

202200
for _, f := range s.files {
203201
splitString := filepath.Dir(base) + "/"

pkg/git/interface.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ type Repo interface {
2626
StageFiles(filenames ...string) error
2727
Commit(msg string, author *Author) error
2828
Push(branch string) error
29-
DeleteCachedRepo() error
29+
DeleteCache() error
3030
}

pkg/git/mock/mock.go

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package mock
22

33
import (
4-
"errors"
54
"io"
65
"path"
76
"path/filepath"
@@ -11,10 +10,6 @@ import (
1110
"github.com/rhd-gitops-example/services/pkg/git"
1211
)
1312

14-
/* FAIL_DELETING_REPO_BRANCH is a branch name that when used as a target branch will
15-
cause the the deletion from the cache to fail (in tests only) */
16-
const FAIL_DELETING_REPO_BRANCH = "fail_to_delete_repo"
17-
1813
type Repository struct {
1914
currentBranch string
2015
knownBranches []string
@@ -32,13 +27,13 @@ type Repository struct {
3227
copyFileErr error
3328

3429
commits []string
35-
commitErr error
30+
CommitErr error
3631

3732
pushedBranches []string
3833
pushErr error
3934

40-
deleted bool
41-
deletedErr error
35+
deleted bool
36+
DeleteErr error
4237
}
4338

4439
// New creates and returns a new git.Cache implementation that operates entirely
@@ -82,7 +77,7 @@ func (m *Repository) Commit(msg string, author *git.Author) error {
8277
m.commits = []string{}
8378
}
8479
m.commits = append(m.commits, key(m.currentBranch, msg, author.Token))
85-
return m.commitErr
80+
return m.CommitErr
8681
}
8782

8883
// Push fulfils the git.Repo interface.
@@ -148,15 +143,13 @@ func (m *Repository) AddFiles(names ...string) {
148143
}
149144
}
150145

151-
// DeleteCachedRepo deletes the repo from the local cache directory
152-
func (m *Repository) DeleteCachedRepo() error {
153-
if m.currentBranch == FAIL_DELETING_REPO_BRANCH {
154-
m.deleted = false
155-
m.deletedErr = errors.New("MOCK FAILURE DELETING REPO MESSAGE")
156-
} else {
157-
m.deleted = true
146+
// DeleteCache deletes the repo from the local cache directory.
147+
func (m *Repository) DeleteCache() error {
148+
if m.DeleteErr != nil {
149+
return m.DeleteErr
158150
}
159-
return m.deletedErr
151+
m.deleted = true
152+
return nil
160153
}
161154

162155
// AssertBranchCreated asserts that the named branch was created from the from
@@ -200,7 +193,7 @@ func (m *Repository) AssertDeletedFromCache(t *testing.T) {
200193
// AssertNotDeletedFromCache asserts that delete was called to remove the local repo
201194
func (m *Repository) AssertNotDeletedFromCache(t *testing.T) {
202195
if m.deleted {
203-
t.Fatalf("repo was unexpectedly deleted from the promotion cache directory : %w", m.deletedErr)
196+
t.Fatal("repo was unexpectedly deleted from the promotion cache directory")
204197
}
205198
}
206199

pkg/git/repository.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ func envFromAuthor(a *Author) []string {
156156
sf("GIT_AUTHOR_EMAIL", a.Email)}
157157
}
158158

159-
// DeleteCachedRepo removes the local clones from the promotion cache
160-
func (r *Repository) DeleteCachedRepo() error {
159+
// DeleteCache removes the local clones from the promotion cache.
160+
func (r *Repository) DeleteCache() error {
161161
err := os.RemoveAll(r.LocalPath)
162162
if err != nil {
163163
return fmt.Errorf("failed deleting `%s` : %w", r.LocalPath, err)

pkg/local/local.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,22 @@ import (
55
"path"
66
"path/filepath"
77
"strings"
8+
89
"github.com/rhd-gitops-example/services/pkg/git"
910
)
1011

11-
1212
type Local struct {
1313
LocalPath string
1414
Debug bool
1515
Logger func(fmt string, v ...interface{})
1616
}
1717

18-
1918
// CopyConfig takes the name of a service and a Source local service root path to be copied to a Destination.
2019
//
21-
// Only files under /path/to/local/repo/config/* are copied to the destination /services/[serviceName]/base/config/*
20+
// Only files under /path/to/local/repo/config/* are copied to the destination /services/[serviceName]/base/config/*
2221
//
2322
// Returns the list of files that were copied, and possibly an error.
2423
func CopyConfig(serviceName string, source git.Source, dest git.Destination) ([]string, error) {
25-
2624
copied := []string{}
2725
err := source.Walk("", func(prefix, name string) error {
2826
sourcePath := path.Join(prefix, name)
@@ -42,8 +40,8 @@ func pathForDestServiceConfig(serviceName, name string) string {
4240
return filepath.Join("services/", serviceName, "base", name)
4341
}
4442

45-
func (l *Local) Walk(base string, cb func(prefix, name string) error) error {
46-
base = filepath.Join(l.LocalPath, "config")
43+
func (l *Local) Walk(_ string, cb func(prefix, name string) error) error {
44+
base := filepath.Join(l.LocalPath, "config")
4745
prefix := filepath.Dir(base) + "/"
4846
return filepath.Walk(base, func(path string, info os.FileInfo, err error) error {
4947
if err != nil {

pkg/local/local_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ func TestCopyConfig(t *testing.T) {
3131
}
3232
}
3333

34-
3534
type mockSource struct {
3635
files []string
3736
localPath string
@@ -45,11 +44,11 @@ type mockSource struct {
4544
// and then calls filePath.Walk() on /full/path/to/repo/services/ .
4645
// When CopyService() drives Walk(), 'base' is typically services/service-name
4746
// Thus we take each /full/path/to/file/in/mockSource.files[] and split it at 'services/' as happens in the Walk() method we're mocking.
48-
func (s *mockSource) Walk(base string, cb func(string, string) error) error {
47+
func (s *mockSource) Walk(_ string, cb func(string, string) error) error {
4948
if s.files == nil {
5049
return nil
5150
}
52-
base = filepath.Join(s.localPath, "config")
51+
base := filepath.Join(s.localPath, "config")
5352

5453
for _, f := range s.files {
5554
splitString := filepath.Dir(base) + "/"

0 commit comments

Comments
 (0)