add ut
This commit is contained in:
parent
1fb530c9ab
commit
2493847661
@ -133,9 +133,9 @@ func (b *Build) determineOutputDir(outputDir string) (string, error) {
|
|||||||
targetName := ""
|
targetName := ""
|
||||||
for _, pkg := range b.Pkgs {
|
for _, pkg := range b.Pkgs {
|
||||||
if pkg.Name == "main" {
|
if pkg.Name == "main" {
|
||||||
if pkg.Target != ""{
|
if pkg.Target != "" {
|
||||||
targetName = filepath.Base(pkg.Target)
|
targetName = filepath.Base(pkg.Target)
|
||||||
}else {
|
} else {
|
||||||
targetName = filepath.Base(pkg.Dir)
|
targetName = filepath.Base(pkg.Dir)
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
|
@ -46,6 +46,28 @@ func TestLegacyProjectCopyWithUnexistedDir(t *testing.T) {
|
|||||||
assert.Equal(t, strings.Contains(output, "Failed to Copy"), true)
|
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) {
|
func TestCopyDir(t *testing.T) {
|
||||||
wd, _ := os.Getwd()
|
wd, _ := os.Getwd()
|
||||||
pkg := &cover.Package{Dir: wd, Root: wd, GoFiles: []string{"build.go", "legacy.go"}, CgoFiles: []string{"run.go"}}
|
pkg := &cover.Package{Dir: wd, Root: wd, GoFiles: []string{"build.go", "legacy.go"}, CgoFiles: []string{"run.go"}}
|
||||||
|
@ -108,8 +108,6 @@ func (b *Build) mvProjectsToTmp() error {
|
|||||||
} else if b.IsMod == false && b.Root == "" {
|
} else if b.IsMod == false && b.Root == "" {
|
||||||
b.TmpWorkingDir = b.TmpDir
|
b.TmpWorkingDir = b.TmpDir
|
||||||
b.cpLegacyProject()
|
b.cpLegacyProject()
|
||||||
} else {
|
|
||||||
return fmt.Errorf("unknown project type: %w", ErrShouldNotReached)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof("New workingdir in tmp directory in: %v", b.TmpWorkingDir)
|
log.Infof("New workingdir in tmp directory in: %v", b.TmpWorkingDir)
|
||||||
|
@ -23,6 +23,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/qiniu/goc/pkg/cover"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -95,7 +96,7 @@ func TestLegacyProjectNotInGoPATH(t *testing.T) {
|
|||||||
os.Setenv("GO111MODULE", "off")
|
os.Setenv("GO111MODULE", "off")
|
||||||
|
|
||||||
b, _ := NewBuild("", []string{"."}, workingDir, "")
|
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)
|
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
|
// test mvProjectsToTmp failed by traversePkgsList error
|
||||||
func TestTraversePkgsList(t *testing.T) {
|
func TestMvProjectsToTmpFailByTraversePkgsList(t *testing.T) {
|
||||||
b := &Build{
|
b := &Build{
|
||||||
Pkgs: nil,
|
Pkgs: nil,
|
||||||
}
|
}
|
||||||
_, _, err := b.traversePkgsList()
|
err := b.mvProjectsToTmp()
|
||||||
assert.EqualError(t, err, ErrShouldNotReached.Error())
|
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
|
// test getTmpwd error case
|
||||||
@ -148,3 +168,8 @@ func TestFindWhereToInstall(t *testing.T) {
|
|||||||
expectedPlace := filepath.Join(os.Getenv("HOME"), "go", "bin")
|
expectedPlace := filepath.Join(os.Getenv("HOME"), "go", "bin")
|
||||||
assert.Equal(t, placeToInstall, expectedPlace)
|
assert.Equal(t, placeToInstall, expectedPlace)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMvProjectsToTmp(t *testing.T) {
|
||||||
|
b := &Build{TmpDir: "/tmp/test"}
|
||||||
|
fmt.Println(b.MvProjectsToTmp())
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user