From fdfd336d6e9edea16955af671a27622801076ec2 Mon Sep 17 00:00:00 2001 From: Jarek Kowalski Date: Mon, 1 Apr 2019 18:59:48 -0700 Subject: [PATCH] lint: fixed various linter issues --- fs/cachefs/cache_test.go | 22 +++++----- fs/localfs/local_fs_test.go | 19 ++++++--- internal/jsonstream/stream_test.go | 11 ++++- snapshot/policy/policy_manager.go | 3 -- snapshot/snapshot_test.go | 4 +- tests/end_to_end_test/end_to_end_test.go | 54 ++++++++++++------------ 6 files changed, 62 insertions(+), 51 deletions(-) diff --git a/fs/cachefs/cache_test.go b/fs/cachefs/cache_test.go index 03064c42b..83941521e 100644 --- a/fs/cachefs/cache_test.go +++ b/fs/cachefs/cache_test.go @@ -149,57 +149,57 @@ func TestCache(t *testing.T) { cv.verifyCacheOrdering(t) // fetch id1 - c.getEntries(ctx, id1, expirationTime, cs.get(id1)) + _, _ = c.getEntries(ctx, id1, expirationTime, cs.get(id1)) cv.verifyCacheMiss(t, id1) cv.verifyCacheOrdering(t, id1) // fetch id1 again - cache hit, no change - c.getEntries(ctx, id1, expirationTime, cs.get(id1)) + _, _ = c.getEntries(ctx, id1, expirationTime, cs.get(id1)) cv.verifyCacheHit(t, id1) cv.verifyCacheOrdering(t, id1) // fetch id2 - c.getEntries(ctx, id2, expirationTime, cs.get(id2)) + _, _ = c.getEntries(ctx, id2, expirationTime, cs.get(id2)) cv.verifyCacheMiss(t, id2) cv.verifyCacheOrdering(t, id2, id1) // fetch id1 again - cache hit, id1 moved to the top of the LRU list - c.getEntries(ctx, id1, expirationTime, cs.get(id1)) + _, _ = c.getEntries(ctx, id1, expirationTime, cs.get(id1)) cv.verifyCacheHit(t, id1) cv.verifyCacheOrdering(t, id1, id2) // fetch id2 again - c.getEntries(ctx, id2, expirationTime, cs.get(id2)) + _, _ = c.getEntries(ctx, id2, expirationTime, cs.get(id2)) cv.verifyCacheHit(t, id2) cv.verifyCacheOrdering(t, id2, id1) // fetch id3 - c.getEntries(ctx, id3, expirationTime, cs.get(id3)) + _, _ = c.getEntries(ctx, id3, expirationTime, cs.get(id3)) cv.verifyCacheMiss(t, id3) cv.verifyCacheOrdering(t, id3, id2, id1) // fetch id4 - c.getEntries(ctx, id4, expirationTime, cs.get(id4)) + _, _ = c.getEntries(ctx, id4, expirationTime, cs.get(id4)) cv.verifyCacheMiss(t, id4) cv.verifyCacheOrdering(t, id4, id3) // fetch id1 again - c.getEntries(ctx, id1, expirationTime, cs.get(id1)) + _, _ = c.getEntries(ctx, id1, expirationTime, cs.get(id1)) cv.verifyCacheMiss(t, id1) cv.verifyCacheOrdering(t, id1, id4) // fetch id5, it's a big one that expels all but one - c.getEntries(ctx, id5, expirationTime, cs.get(id5)) + _, _ = c.getEntries(ctx, id5, expirationTime, cs.get(id5)) cv.verifyCacheMiss(t, id5) cv.verifyCacheOrdering(t, id5, id1) // fetch id6 - c.getEntries(ctx, id6, expirationTime, cs.get(id6)) + _, _ = c.getEntries(ctx, id6, expirationTime, cs.get(id6)) cv.verifyCacheMiss(t, id6) cv.verifyCacheOrdering(t, id6) // fetch id7 - c.getEntries(ctx, id7, expirationTime, cs.get(id7)) + _, _ = c.getEntries(ctx, id7, expirationTime, cs.get(id7)) cv.verifyCacheMiss(t, id7) cv.verifyCacheOrdering(t, id6) } diff --git a/fs/localfs/local_fs_test.go b/fs/localfs/local_fs_test.go index 7b9a30adc..5efa5b480 100644 --- a/fs/localfs/local_fs_test.go +++ b/fs/localfs/local_fs_test.go @@ -28,7 +28,7 @@ func TestFiles(t *testing.T) { var dir fs.Directory // Try listing directory that does not exist. - dir, err = Directory(fmt.Sprintf("/no-such-dir-%v", time.Now().Nanosecond())) + _, err = Directory(fmt.Sprintf("/no-such-dir-%v", time.Now().Nanosecond())) if err == nil { t.Errorf("expected error when dir directory that does not exist.") } @@ -49,12 +49,12 @@ func TestFiles(t *testing.T) { } // Now list a directory with 3 files. - ioutil.WriteFile(filepath.Join(tmp, "f3"), []byte{1, 2, 3}, 0777) - ioutil.WriteFile(filepath.Join(tmp, "f2"), []byte{1, 2, 3, 4}, 0777) - ioutil.WriteFile(filepath.Join(tmp, "f1"), []byte{1, 2, 3, 4, 5}, 0777) + assertNoError(t, ioutil.WriteFile(filepath.Join(tmp, "f3"), []byte{1, 2, 3}, 0777)) + assertNoError(t, ioutil.WriteFile(filepath.Join(tmp, "f2"), []byte{1, 2, 3, 4}, 0777)) + assertNoError(t, ioutil.WriteFile(filepath.Join(tmp, "f1"), []byte{1, 2, 3, 4, 5}, 0777)) - os.Mkdir(filepath.Join(tmp, "z"), 0777) - os.Mkdir(filepath.Join(tmp, "y"), 0777) + assertNoError(t, os.Mkdir(filepath.Join(tmp, "z"), 0777)) + assertNoError(t, os.Mkdir(filepath.Join(tmp, "y"), 0777)) dir, err = Directory(tmp) if err != nil { @@ -90,3 +90,10 @@ func TestFiles(t *testing.T) { } } } + +func assertNoError(t *testing.T, err error) { + t.Helper() + if err != nil { + t.Errorf("err: %v", err) + } +} diff --git a/internal/jsonstream/stream_test.go b/internal/jsonstream/stream_test.go index c7e24d633..37d16ea58 100644 --- a/internal/jsonstream/stream_test.go +++ b/internal/jsonstream/stream_test.go @@ -34,7 +34,7 @@ func TestStream(t *testing.T) { t.Errorf("write error: %v", err) } } - w.Finalize() + assertNoError(t, w.Finalize()) log.Printf("wrote: %v", buf.String()) r, err := NewReader(bufio.NewReader(&buf), testHeader1, nil) if err != nil { @@ -72,7 +72,7 @@ func TestStreamWithSummary(t *testing.T) { t.Errorf("write error: %v", err) } } - w.FinalizeWithSummary(TestSummary{Value: 123}) + assertNoError(t, w.FinalizeWithSummary(TestSummary{Value: 123})) log.Printf("wrote: %v", buf.String()) var summary TestSummary @@ -115,3 +115,10 @@ func TestInvalidHeader(t *testing.T) { t.Errorf("got incorrect error: %v", err) } } + +func assertNoError(t *testing.T, err error) { + t.Helper() + if err != nil { + t.Errorf("err: %v", err) + } +} diff --git a/snapshot/policy/policy_manager.go b/snapshot/policy/policy_manager.go index 78f4eefa8..20ed7d69d 100644 --- a/snapshot/policy/policy_manager.go +++ b/snapshot/policy/policy_manager.go @@ -49,9 +49,6 @@ func GetEffectivePolicy(ctx context.Context, rep *repo.Repository, si snapshot.S md = append(md, userHostManifests...) // Try host-level policy. - if err != nil { - return nil, nil, err - } hostManifests, err := rep.Manifests.Find(ctx, labelsForSource(snapshot.SourceInfo{Host: si.Host})) if err != nil { return nil, nil, err diff --git a/snapshot/snapshot_test.go b/snapshot/snapshot_test.go index a1dba384a..234d60231 100644 --- a/snapshot/snapshot_test.go +++ b/snapshot/snapshot_test.go @@ -94,12 +94,12 @@ func mustSaveSnapshot(t *testing.T, rep *repo.Repository, man *snapshot.Manifest } func verifySources(t *testing.T, rep *repo.Repository, sources ...snapshot.SourceInfo) { - sources, err := snapshot.ListSources(context.Background(), rep) + actualSources, err := snapshot.ListSources(context.Background(), rep) if err != nil { t.Errorf("error listing sources: %v", err) } - if got, want := sorted(sourcesToStrings(sources...)), sorted(sourcesToStrings(sources...)); !reflect.DeepEqual(got, want) { + if got, want := sorted(sourcesToStrings(actualSources...)), sorted(sourcesToStrings(sources...)); !reflect.DeepEqual(got, want) { t.Errorf("unexpected sources: %v want %v", got, want) } } 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 4680c0a7b..979372d17 100644 --- a/tests/end_to_end_test/end_to_end_test.go +++ b/tests/end_to_end_test/end_to_end_test.go @@ -124,12 +124,12 @@ func TestEndToEnd(t *testing.T) { e.runAndExpectSuccess(t, "snapshot", "list", ".") dir1 := filepath.Join(e.dataDir, "dir1") - createDirectory(dir1, 3) + createDirectory(t, dir1, 3) e.runAndExpectSuccess(t, "snapshot", "create", dir1) e.runAndExpectSuccess(t, "snapshot", "create", dir1) dir2 := filepath.Join(e.dataDir, "dir2") - createDirectory(dir2, 3) + createDirectory(t, dir2, 3) e.runAndExpectSuccess(t, "snapshot", "create", dir2) e.runAndExpectSuccess(t, "snapshot", "create", dir2) sources := listSnapshotsAndExpectSuccess(t, e) @@ -210,38 +210,38 @@ func TestDiff(t *testing.T) { dataDir := filepath.Join(e.dataDir, "dir1") // initial snapshot - os.MkdirAll(dataDir, 0777) + assertNoError(t, os.MkdirAll(dataDir, 0777)) e.runAndExpectSuccess(t, "snapshot", "create", dataDir) // create some directories and files - os.MkdirAll(filepath.Join(dataDir, "foo"), 0700) - ioutil.WriteFile(filepath.Join(dataDir, "some-file1"), []byte(` + assertNoError(t, os.MkdirAll(filepath.Join(dataDir, "foo"), 0700)) + assertNoError(t, ioutil.WriteFile(filepath.Join(dataDir, "some-file1"), []byte(` hello world how are you -`), 0600) - ioutil.WriteFile(filepath.Join(dataDir, "some-file2"), []byte(` +`), 0600)) + assertNoError(t, ioutil.WriteFile(filepath.Join(dataDir, "some-file2"), []byte(` quick brown fox jumps over the lazy dog -`), 0600) +`), 0600)) e.runAndExpectSuccess(t, "snapshot", "create", dataDir) // change some files - ioutil.WriteFile(filepath.Join(dataDir, "some-file2"), []byte(` + assertNoError(t, ioutil.WriteFile(filepath.Join(dataDir, "some-file2"), []byte(` quick brown fox jumps over the lazy canary -`), 0600) +`), 0600)) - os.MkdirAll(filepath.Join(dataDir, "bar"), 0700) + assertNoError(t, os.MkdirAll(filepath.Join(dataDir, "bar"), 0700)) e.runAndExpectSuccess(t, "snapshot", "create", dataDir) // change some files os.Remove(filepath.Join(dataDir, "some-file1")) - os.MkdirAll(filepath.Join(dataDir, "bar"), 0700) + assertNoError(t, os.MkdirAll(filepath.Join(dataDir, "bar"), 0700)) e.runAndExpectSuccess(t, "snapshot", "create", dataDir) si := listSnapshotsAndExpectSuccess(t, e, dataDir) @@ -329,9 +329,9 @@ func listSnapshotsAndExpectSuccess(t *testing.T, e *testenv, targets ...string) return mustParseSnapshots(t, lines) } -func createDirectory(dirname string, depth int) error { +func createDirectory(t *testing.T, dirname string, depth int) { if err := os.MkdirAll(dirname, 0700); err != nil { - return err + t.Fatalf("unable to create directory %v: %v", dirname, err) } if depth > 0 { @@ -339,9 +339,7 @@ func createDirectory(dirname string, depth int) error { for i := 0; i < numSubDirs; i++ { subdirName := randomName() - if err := createDirectory(filepath.Join(dirname, subdirName), depth-1); err != nil { - return err - } + createDirectory(t, filepath.Join(dirname, subdirName), depth-1) } } @@ -349,25 +347,20 @@ func createDirectory(dirname string, depth int) error { for i := 0; i < numFiles; i++ { fileName := randomName() - if err := createRandomFile(filepath.Join(dirname, fileName)); err != nil { - return err - } + createRandomFile(t, filepath.Join(dirname, fileName)) } - - return nil } -func createRandomFile(filename string) error { +func createRandomFile(t *testing.T, filename string) { f, err := os.Create(filename) if err != nil { - return err + t.Fatalf("unable to create random file: %v", err) } defer f.Close() length := rand.Int63n(100000) - io.Copy(f, io.LimitReader(rand.New(rand.NewSource(1)), length)) - - return nil + _, err = io.Copy(f, io.LimitReader(rand.New(rand.NewSource(1)), length)) + assertNoError(t, err) } func mustParseSnapshots(t *testing.T, lines []string) []sourceInfo { @@ -438,3 +431,10 @@ func splitLines(s string) []string { } return result } + +func assertNoError(t *testing.T, err error) { + t.Helper() + if err != nil { + t.Errorf("err: %v", err) + } +}