From 0654e30b260dc4f9cbfe7f18ef056018b6716987 Mon Sep 17 00:00:00 2001 From: Jarek Kowalski Date: Tue, 10 Sep 2019 22:24:40 -0700 Subject: [PATCH] linter: upgraded to golangci-lint 1.18.0, fixed or suppressed warnings --- .golangci.yml | 6 +- Makefile | 4 +- cli/command_snapshot_list.go | 2 +- fs/ignorefs/ignorefs_test.go | 256 +++++++++++------------ repo/content/cache_hmac.go | 8 +- repo/content/content_manager.go | 2 +- repo/content/content_manager_test.go | 1 + repo/content/packindex_test.go | 86 ++++---- repo/manifest/manifest_manager_test.go | 1 + tests/end_to_end_test/end_to_end_test.go | 1 + 10 files changed, 186 insertions(+), 181 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index b9ca42df0..eb29d01c6 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -8,6 +8,9 @@ linters-settings: - (github.com/op/go-logging/Logger).Warningf - (github.com/op/go-logging/Logger).Errorf - (github.com/op/go-logging/Logger).Fatalf + funlen: + lines: 100 + statements: 60 golint: min-confidence: 0 gocyclo: @@ -54,4 +57,5 @@ issues: # golangci.com configuration # https://github.com/golangci/golangci/wiki/Configuration service: - golangci-lint-version: 1.17.x # use the fixed version to not introduce new linters unexpectedly + golangci-lint-version: 1.18.0 # use the fixed version to not introduce new linters unexpectedly + diff --git a/Makefile b/Makefile index 010480776..a61040d69 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ COVERAGE_PACKAGES=./repo/...,./fs/...,./snapshot/... -GOLANGCI_LINT_VERSION=v1.17.1 +GOLANGCI_LINT_VERSION=v1.18.0 LINTER_TOOL=.tools/bin/golangci-lint GOVERALLS_TOOL=.tools/bin/goveralls GO_TEST=go test @@ -164,4 +164,4 @@ else travis-create-long-term-repository: echo Not creating long-term repository. -endif \ No newline at end of file +endif diff --git a/cli/command_snapshot_list.go b/cli/command_snapshot_list.go index af331bd07..d75184f5d 100644 --- a/cli/command_snapshot_list.go +++ b/cli/command_snapshot_list.go @@ -140,7 +140,7 @@ func outputManifestGroups(ctx context.Context, rep *repo.Repository, manifests [ return nil } -//nolint:gocyclo +//nolint:gocyclo,funlen func outputManifestFromSingleSource(ctx context.Context, rep *repo.Repository, manifests []*snapshot.Manifest, parts []string) error { var count int var lastTotalFileSize int64 diff --git a/fs/ignorefs/ignorefs_test.go b/fs/ignorefs/ignorefs_test.go index f7a86077c..641d49567 100644 --- a/fs/ignorefs/ignorefs_test.go +++ b/fs/ignorefs/ignorefs_test.go @@ -71,144 +71,144 @@ func setupFilesystem() *mockfs.Directory { }, } -func TestIgnoreFS(t *testing.T) { - cases := []struct { - desc string - policy ignorefs.FilesPolicyMap - setup func(root *mockfs.Directory) - addedFiles []string - ignoredFiles []string - }{ - {desc: "null policy, missing dotignore"}, - { - desc: "default policy missing dotignore", - policy: defaultPolicy, - addedFiles: nil, - ignoredFiles: []string{ - "./ignored-by-rule", - "./largefile1", - }, +var cases = []struct { + desc string + policy ignorefs.FilesPolicyMap + setup func(root *mockfs.Directory) + addedFiles []string + ignoredFiles []string +}{ + {desc: "null policy, missing dotignore"}, + { + desc: "default policy missing dotignore", + policy: defaultPolicy, + addedFiles: nil, + ignoredFiles: []string{ + "./ignored-by-rule", + "./largefile1", }, - { - desc: "default policy, have dotignore", - policy: defaultPolicy, - setup: func(root *mockfs.Directory) { - root.AddFileLines(".kopiaignore", []string{"file[12]"}, 0) - }, - addedFiles: []string{"./.kopiaignore"}, - ignoredFiles: []string{ - "./ignored-by-rule", - "./largefile1", - "./file1", - "./file2", - }, + }, + { + desc: "default policy, have dotignore", + policy: defaultPolicy, + setup: func(root *mockfs.Directory) { + root.AddFileLines(".kopiaignore", []string{"file[12]"}, 0) }, - { - desc: "default policy, have dotignore #2", - policy: defaultPolicy, - setup: func(root *mockfs.Directory) { - root.AddFileLines(".kopiaignore", []string{ - "pkg", - "file*", - }, 0) - }, - addedFiles: []string{"./.kopiaignore"}, - ignoredFiles: []string{ - "./ignored-by-rule", - "./largefile1", - "./file1", - "./file2", - "./file3", - "./pkg/", - "./pkg/some-pkg", - }, + addedFiles: []string{"./.kopiaignore"}, + ignoredFiles: []string{ + "./ignored-by-rule", + "./largefile1", + "./file1", + "./file2", }, - { - desc: "default policy, have dotignore #3", - policy: defaultPolicy, - setup: func(root *mockfs.Directory) { - root.AddFileLines(".kopiaignore", []string{ - "pkg", - "file*", - }, 0) - }, - addedFiles: []string{"./.kopiaignore"}, - ignoredFiles: []string{ - "./ignored-by-rule", - "./largefile1", - "./file1", - "./file2", - "./file3", - "./pkg/", - "./pkg/some-pkg", - }, + }, + { + desc: "default policy, have dotignore #2", + policy: defaultPolicy, + setup: func(root *mockfs.Directory) { + root.AddFileLines(".kopiaignore", []string{ + "pkg", + "file*", + }, 0) }, - { - desc: "default policy, have dotignore #4", - policy: defaultPolicy, - setup: func(root *mockfs.Directory) { - root.AddFileLines(".kopiaignore", []string{ - "file[12]", - "**/some-src", - "bin/", - }, 0) - }, - addedFiles: []string{"./.kopiaignore"}, - ignoredFiles: []string{ - "./ignored-by-rule", - "./largefile1", - "./file1", - "./file2", - "./bin/", - "./bin/some-bin", - "./src/some-src/", - "./src/some-src/f1", - }, + addedFiles: []string{"./.kopiaignore"}, + ignoredFiles: []string{ + "./ignored-by-rule", + "./largefile1", + "./file1", + "./file2", + "./file3", + "./pkg/", + "./pkg/some-pkg", }, - { - desc: "two policies, nested policy excludes files", - policy: rootAndSrcPolicy, - ignoredFiles: []string{ - "./ignored-by-rule", - "./largefile1", - "./src/some-src/", // excluded by policy at './src' - "./src/some-src/f1", - }, + }, + { + desc: "default policy, have dotignore #3", + policy: defaultPolicy, + setup: func(root *mockfs.Directory) { + root.AddFileLines(".kopiaignore", []string{ + "pkg", + "file*", + }, 0) }, - { - desc: "non-root policy excludes files", - setup: func(root *mockfs.Directory) { - root.Subdir("src").AddFileLines(".extraignore", []string{ - "zzz", - }, 0) - root.Subdir("src").AddFile("yyy", dummyFileContents, 0) - root.Subdir("src").AddFile("zzz", dummyFileContents, 0) // ignored by .extraignore - root.Subdir("src").AddFile("another-yyy", dummyFileContents, 0) // ignored by policy rule - root.AddFile("zzz", dummyFileContents, 0) // not ignored, at parent level - }, - policy: ignorefs.FilesPolicyMap{ - "./src": &ignorefs.FilesPolicy{ - IgnoreRules: []string{ - "some-*", - "another-*", - }, - DotIgnoreFiles: []string{ - ".extraignore", - }, + addedFiles: []string{"./.kopiaignore"}, + ignoredFiles: []string{ + "./ignored-by-rule", + "./largefile1", + "./file1", + "./file2", + "./file3", + "./pkg/", + "./pkg/some-pkg", + }, + }, + { + desc: "default policy, have dotignore #4", + policy: defaultPolicy, + setup: func(root *mockfs.Directory) { + root.AddFileLines(".kopiaignore", []string{ + "file[12]", + "**/some-src", + "bin/", + }, 0) + }, + addedFiles: []string{"./.kopiaignore"}, + ignoredFiles: []string{ + "./ignored-by-rule", + "./largefile1", + "./file1", + "./file2", + "./bin/", + "./bin/some-bin", + "./src/some-src/", + "./src/some-src/f1", + }, + }, + { + desc: "two policies, nested policy excludes files", + policy: rootAndSrcPolicy, + ignoredFiles: []string{ + "./ignored-by-rule", + "./largefile1", + "./src/some-src/", // excluded by policy at './src' + "./src/some-src/f1", + }, + }, + { + desc: "non-root policy excludes files", + setup: func(root *mockfs.Directory) { + root.Subdir("src").AddFileLines(".extraignore", []string{ + "zzz", + }, 0) + root.Subdir("src").AddFile("yyy", dummyFileContents, 0) + root.Subdir("src").AddFile("zzz", dummyFileContents, 0) // ignored by .extraignore + root.Subdir("src").AddFile("another-yyy", dummyFileContents, 0) // ignored by policy rule + root.AddFile("zzz", dummyFileContents, 0) // not ignored, at parent level + }, + policy: ignorefs.FilesPolicyMap{ + "./src": &ignorefs.FilesPolicy{ + IgnoreRules: []string{ + "some-*", + "another-*", + }, + DotIgnoreFiles: []string{ + ".extraignore", }, }, - addedFiles: []string{ - "./src/.extraignore", - "./src/yyy", - "./zzz", - }, - ignoredFiles: []string{ - "./src/some-src/", // excluded by policy at './src' - "./src/some-src/f1", - }, }, - } + addedFiles: []string{ + "./src/.extraignore", + "./src/yyy", + "./zzz", + }, + ignoredFiles: []string{ + "./src/some-src/", // excluded by policy at './src' + "./src/some-src/f1", + }, + }, +} +func TestIgnoreFS(t *testing.T) { for _, tc := range cases { tc := tc t.Run(tc.desc, func(t *testing.T) { diff --git a/repo/content/cache_hmac.go b/repo/content/cache_hmac.go index a57d54a16..b40670c81 100644 --- a/repo/content/cache_hmac.go +++ b/repo/content/cache_hmac.go @@ -1,8 +1,10 @@ package content -import "crypto/hmac" -import "crypto/sha256" -import "errors" +import ( + "crypto/hmac" + "crypto/sha256" + "errors" +) func appendHMAC(data, secret []byte) []byte { h := hmac.New(sha256.New, secret) diff --git a/repo/content/content_manager.go b/repo/content/content_manager.go index 2de18fc20..e558aef84 100644 --- a/repo/content/content_manager.go +++ b/repo/content/content_manager.go @@ -129,7 +129,7 @@ func (bm *Manager) DeleteContent(contentID ID) error { } // Intentionally passing bi by value. -// nolint:hugeParam +// nolint:gocritic func (bm *Manager) deletePreexistingContent(ci Info) { if ci.Deleted { return diff --git a/repo/content/content_manager_test.go b/repo/content/content_manager_test.go index 7e8f9f935..8f72eb463 100644 --- a/repo/content/content_manager_test.go +++ b/repo/content/content_manager_test.go @@ -741,6 +741,7 @@ func TestDeleteAndRecreate(t *testing.T) { } } +// nolint:funlen func TestIterateContents(t *testing.T) { ctx := context.Background() data := blobtesting.DataMap{} diff --git a/repo/content/packindex_test.go b/repo/content/packindex_test.go index 333e13ea4..8a1d86deb 100644 --- a/repo/content/packindex_test.go +++ b/repo/content/packindex_test.go @@ -13,52 +13,48 @@ "github.com/kopia/kopia/repo/blob" ) -//nolint:gocyclo +func deterministicContentID(prefix string, id int) ID { + h := sha1.New() + fmt.Fprintf(h, "%v%v", prefix, id) + + prefix2 := "" + if id%2 == 0 { + prefix2 = "x" + } + if id%7 == 0 { + prefix2 = "y" + } + if id%5 == 0 { + prefix2 = "m" + } + return ID(fmt.Sprintf("%v%x", prefix2, h.Sum(nil))) +} +func deterministicPackBlobID(id int) blob.ID { + h := sha1.New() + fmt.Fprintf(h, "%v", id) + return blob.ID(fmt.Sprintf("%x", h.Sum(nil))) +} + +func deterministicPackedOffset(id int) uint32 { + s := rand.NewSource(int64(id + 1)) + rnd := rand.New(s) + return uint32(rnd.Int31()) +} +func deterministicPackedLength(id int) uint32 { + s := rand.NewSource(int64(id + 2)) + rnd := rand.New(s) + return uint32(rnd.Int31()) +} +func deterministicFormatVersion(id int) byte { + return byte(id % 100) +} + +func randomUnixTime() int64 { + return int64(rand.Int31()) +} + +//nolint:gocyclo,funlen func TestPackIndex(t *testing.T) { - contentNumber := 0 - - deterministicContentID := func(prefix string, id int) ID { - h := sha1.New() - fmt.Fprintf(h, "%v%v", prefix, id) - contentNumber++ - - prefix2 := "" - if id%2 == 0 { - prefix2 = "x" - } - if id%7 == 0 { - prefix2 = "y" - } - if id%5 == 0 { - prefix2 = "m" - } - return ID(fmt.Sprintf("%v%x", prefix2, h.Sum(nil))) - } - deterministicPackBlobID := func(id int) blob.ID { - h := sha1.New() - fmt.Fprintf(h, "%v", id) - contentNumber++ - return blob.ID(fmt.Sprintf("%x", h.Sum(nil))) - } - - deterministicPackedOffset := func(id int) uint32 { - s := rand.NewSource(int64(id + 1)) - rnd := rand.New(s) - return uint32(rnd.Int31()) - } - deterministicPackedLength := func(id int) uint32 { - s := rand.NewSource(int64(id + 2)) - rnd := rand.New(s) - return uint32(rnd.Int31()) - } - deterministicFormatVersion := func(id int) byte { - return byte(id % 100) - } - - randomUnixTime := func() int64 { - return int64(rand.Int31()) - } - var infos []Info // deleted contents with all information diff --git a/repo/manifest/manifest_manager_test.go b/repo/manifest/manifest_manager_test.go index d3fd3861e..dd0c15b1e 100644 --- a/repo/manifest/manifest_manager_test.go +++ b/repo/manifest/manifest_manager_test.go @@ -12,6 +12,7 @@ "github.com/kopia/kopia/repo/content" ) +//nolint:funlen func TestManifest(t *testing.T) { ctx := context.Background() data := blobtesting.DataMap{} diff --git a/tests/end_to_end_test/end_to_end_test.go b/tests/end_to_end_test/end_to_end_test.go index 0c6c1ab69..18f4e05bc 100644 --- a/tests/end_to_end_test/end_to_end_test.go +++ b/tests/end_to_end_test/end_to_end_test.go @@ -92,6 +92,7 @@ func (e *testenv) cleanup(t *testing.T) { } } +//nolint:funlen func TestEndToEnd(t *testing.T) { e := newTestEnv(t) defer e.cleanup(t)