From 2c101f62f10360bf6c71cf5ee58cb6f728c2e3ae Mon Sep 17 00:00:00 2001 From: jichangjun Date: Mon, 15 Jun 2020 19:22:38 +0800 Subject: [PATCH] add test cases --- pkg/cover/cover.go | 10 ++--- pkg/cover/cover_test.go | 81 ++++++++++++++++++++++++++++++++++++++++- pkg/cover/store_test.go | 2 +- 3 files changed, 86 insertions(+), 7 deletions(-) diff --git a/pkg/cover/cover.go b/pkg/cover/cover.go index b013a6d..e28ae66 100644 --- a/pkg/cover/cover.go +++ b/pkg/cover/cover.go @@ -155,7 +155,7 @@ func Execute(args, newGopath, target, mode, center string) { //only focus package neither standard Go library nor dependency library if depPkg, ok := pkgs[dep]; ok { - if findInternal(dep) { + if hasInternalPath(dep) { //scan exist cache cover to tc.CacheCover if cache, ok := seenCache[dep]; ok { log.Printf("cache cover exist: %s", cache.Package.ImportPath) @@ -296,10 +296,10 @@ func isDirExist(path string) bool { } // Refer: https://github.com/golang/go/blob/master/src/cmd/go/internal/load/pkg.go#L1334:6 -// findInternal looks for the final "internal" path element in the given import path. -// If there isn't one, findInternal returns ok=false. -// Otherwise, findInternal returns ok=true and the index of the "internal". -func findInternal(path string) bool { +// hasInternalPath looks for the final "internal" path element in the given import path. +// If there isn't one, hasInternalPath returns ok=false. +// Otherwise, hasInternalPath returns ok=true and the index of the "internal". +func hasInternalPath(path string) bool { // Three cases, depending on internal at start/end of string or not. // The order matters: we must return the index of the final element, // because the final one produces the most restrictive requirement diff --git a/pkg/cover/cover_test.go b/pkg/cover/cover_test.go index 05dfb3d..1113b87 100644 --- a/pkg/cover/cover_test.go +++ b/pkg/cover/cover_test.go @@ -18,7 +18,6 @@ package cover import ( "fmt" - log "github.com/sirupsen/logrus" "os" "os/exec" "path/filepath" @@ -26,6 +25,8 @@ import ( "strings" "testing" + log "github.com/sirupsen/logrus" + "github.com/stretchr/testify/assert" ) @@ -225,3 +226,81 @@ func TestDeclareCoverVars(t *testing.T) { } } + +func TestGetInternalParent(t *testing.T) { + var tcs = []struct { + ImportPath string + expectedParent string + }{ + { + ImportPath: "a/internal/b", + expectedParent: "a", + }, + { + ImportPath: "internal/b", + expectedParent: "", + }, + { + ImportPath: "a/b/internal/b", + expectedParent: "a/b", + }, + { + ImportPath: "a/b/internal", + expectedParent: "a/b", + }, + { + ImportPath: "a/b/internal/c", + expectedParent: "a/b", + }, + { + ImportPath: "a/b/c", + expectedParent: "", + }, + { + ImportPath: "", + expectedParent: "", + }, + } + + for _, tc := range tcs { + actual := getInternalParent(tc.ImportPath) + if actual != tc.expectedParent { + t.Errorf("getInternalParent failed for importPath %s, expected %s, got %s", tc.ImportPath, tc.expectedParent, actual) + } + } +} + +func TestFindInternal(t *testing.T) { + var tcs = []struct { + ImportPath string + expectedParent bool + }{ + { + ImportPath: "a/internal/b", + expectedParent: true, + }, + { + ImportPath: "internal/b", + expectedParent: true, + }, + { + ImportPath: "a/b/internal", + expectedParent: true, + }, + { + ImportPath: "a/b/c", + expectedParent: false, + }, + { + ImportPath: "internal", + expectedParent: true, + }, + } + + for _, tc := range tcs { + actual := hasInternalPath(tc.ImportPath) + if actual != tc.expectedParent { + t.Errorf("hasInternalPath check failed for importPath %s", tc.ImportPath) + } + } +} diff --git a/pkg/cover/store_test.go b/pkg/cover/store_test.go index bd61e9f..f4bd8aa 100644 --- a/pkg/cover/store_test.go +++ b/pkg/cover/store_test.go @@ -44,7 +44,7 @@ func TestLocalStore(t *testing.T) { localStore.Add(tc4) addrs := localStore.Get(tc1.Name) if len(addrs) != 2 { - t.Error("unexpect result") + t.Error("unexpected result") } for _, addr := range addrs {