add test cases

This commit is contained in:
jichangjun 2020-06-15 19:22:38 +08:00
parent 76b03178c0
commit 2c101f62f1
3 changed files with 86 additions and 7 deletions

View File

@ -155,7 +155,7 @@ func Execute(args, newGopath, target, mode, center string) {
//only focus package neither standard Go library nor dependency library //only focus package neither standard Go library nor dependency library
if depPkg, ok := pkgs[dep]; ok { if depPkg, ok := pkgs[dep]; ok {
if findInternal(dep) { if hasInternalPath(dep) {
//scan exist cache cover to tc.CacheCover //scan exist cache cover to tc.CacheCover
if cache, ok := seenCache[dep]; ok { if cache, ok := seenCache[dep]; ok {
log.Printf("cache cover exist: %s", cache.Package.ImportPath) 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 // 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. // hasInternalPath looks for the final "internal" path element in the given import path.
// If there isn't one, findInternal returns ok=false. // If there isn't one, hasInternalPath returns ok=false.
// Otherwise, findInternal returns ok=true and the index of the "internal". // Otherwise, hasInternalPath returns ok=true and the index of the "internal".
func findInternal(path string) bool { func hasInternalPath(path string) bool {
// Three cases, depending on internal at start/end of string or not. // Three cases, depending on internal at start/end of string or not.
// The order matters: we must return the index of the final element, // The order matters: we must return the index of the final element,
// because the final one produces the most restrictive requirement // because the final one produces the most restrictive requirement

View File

@ -18,7 +18,6 @@ package cover
import ( import (
"fmt" "fmt"
log "github.com/sirupsen/logrus"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
@ -26,6 +25,8 @@ import (
"strings" "strings"
"testing" "testing"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert" "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)
}
}
}

View File

@ -44,7 +44,7 @@ func TestLocalStore(t *testing.T) {
localStore.Add(tc4) localStore.Add(tc4)
addrs := localStore.Get(tc1.Name) addrs := localStore.Get(tc1.Name)
if len(addrs) != 2 { if len(addrs) != 2 {
t.Error("unexpect result") t.Error("unexpected result")
} }
for _, addr := range addrs { for _, addr := range addrs {