This commit is contained in:
tongjingran 2020-12-15 20:38:18 +08:00
parent 1fb530c9ab
commit 2493847661
4 changed files with 54 additions and 9 deletions

View File

@ -133,9 +133,9 @@ func (b *Build) determineOutputDir(outputDir string) (string, error) {
targetName := ""
for _, pkg := range b.Pkgs {
if pkg.Name == "main" {
if pkg.Target != ""{
if pkg.Target != "" {
targetName = filepath.Base(pkg.Target)
}else {
} else {
targetName = filepath.Base(pkg.Dir)
}
break

View File

@ -46,6 +46,28 @@ func TestLegacyProjectCopyWithUnexistedDir(t *testing.T) {
assert.Equal(t, strings.Contains(output, "Failed to Copy"), true)
}
// copy goMod project without go.mod
func TestGoModProjectCopyWithUnexistedModFile(t *testing.T) {
pkgs := make(map[string]*cover.Package)
pkgs["main"] = &cover.Package{
Module: &cover.ModulePublic{
Dir: "not exied, ia mas duser", // not real one, should fail copy
},
Dir: "not exit, iasdfs",
Name: "main",
}
pkgs["another"] = &cover.Package{}
b := &Build{
TmpDir: "sdfsfev2234444", // not real one, should fail copy
Pkgs: pkgs,
IsMod: true,
}
output := captureOutput(b.cpLegacyProject)
assert.Equal(t, strings.Contains(output, "Failed to Copy the go mod file"), true)
}
// copy needed files to tmpDir
func TestCopyDir(t *testing.T) {
wd, _ := os.Getwd()
pkg := &cover.Package{Dir: wd, Root: wd, GoFiles: []string{"build.go", "legacy.go"}, CgoFiles: []string{"run.go"}}

View File

@ -108,8 +108,6 @@ func (b *Build) mvProjectsToTmp() error {
} else if b.IsMod == false && b.Root == "" {
b.TmpWorkingDir = b.TmpDir
b.cpLegacyProject()
} else {
return fmt.Errorf("unknown project type: %w", ErrShouldNotReached)
}
log.Infof("New workingdir in tmp directory in: %v", b.TmpWorkingDir)

View File

@ -23,6 +23,7 @@ import (
"strings"
"testing"
"github.com/qiniu/goc/pkg/cover"
"github.com/stretchr/testify/assert"
)
@ -95,7 +96,7 @@ func TestLegacyProjectNotInGoPATH(t *testing.T) {
os.Setenv("GO111MODULE", "off")
b, _ := NewBuild("", []string{"."}, workingDir, "")
if !strings.Contains(b.NewGOPATH,b.OriGOPATH) {
if !strings.Contains(b.NewGOPATH, b.OriGOPATH) {
t.Fatalf("New GOPATH should be same with old GOPATH, for this kind of project. New: %v, old: %v", b.NewGOPATH, b.OriGOPATH)
}
@ -105,13 +106,32 @@ func TestLegacyProjectNotInGoPATH(t *testing.T) {
}
}
// test traversePkgsList error case
func TestTraversePkgsList(t *testing.T) {
// test mvProjectsToTmp failed by traversePkgsList error
func TestMvProjectsToTmpFailByTraversePkgsList(t *testing.T) {
b := &Build{
Pkgs: nil,
}
_, _, err := b.traversePkgsList()
assert.EqualError(t, err, ErrShouldNotReached.Error())
err := b.mvProjectsToTmp()
assert.Contains(t, err.Error(), ErrShouldNotReached.Error())
}
// test mvProjectsToTmp failed by getTmpwd error
func TestMvProjectsToTmpFailByGetTmpwd(t *testing.T) {
pkgs := make(map[string]*cover.Package)
pkgs["main"] = &cover.Package{
Module: &cover.ModulePublic{
Dir: "just for test",
Path: "just for test",
},
Dir: "not exist",
Name: "main",
}
b := &Build{
Pkgs: pkgs,
WorkingDir: "not exist",
}
err := b.mvProjectsToTmp()
assert.Contains(t, err.Error(), ErrGocShouldExecInProject.Error())
}
// test getTmpwd error case
@ -148,3 +168,8 @@ func TestFindWhereToInstall(t *testing.T) {
expectedPlace := filepath.Join(os.Getenv("HOME"), "go", "bin")
assert.Equal(t, placeToInstall, expectedPlace)
}
func TestMvProjectsToTmp(t *testing.T) {
b := &Build{TmpDir: "/tmp/test"}
fmt.Println(b.MvProjectsToTmp())
}