fix unit test
add test for cover package add unit test
This commit is contained in:
parent
7916d1ee68
commit
5892de106b
@ -46,7 +46,7 @@ func TestGeneratedBinary(t *testing.T) {
|
||||
args := []string{"."}
|
||||
runBuild(args, workingDir)
|
||||
|
||||
obj := filepath.Join(".", "simple-project")
|
||||
obj := filepath.Join(workingDir, "simple-project")
|
||||
fInfo, err := os.Lstat(obj)
|
||||
assert.Equal(t, err, nil, "the binary should be generated.")
|
||||
assert.Equal(t, startTime.Before(fInfo.ModTime()), true, obj+"new binary should be generated, not the old one")
|
||||
|
@ -36,6 +36,7 @@ Find more information at:
|
||||
`,
|
||||
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
||||
log.SetReportCaller(true)
|
||||
log.SetLevel(log.InfoLevel)
|
||||
log.SetFormatter(&log.TextFormatter{
|
||||
FullTimestamp: true,
|
||||
CallerPrettyfier: func(f *runtime.Frame) (string, string) {
|
||||
|
@ -41,7 +41,7 @@ goc run .
|
||||
if err != nil {
|
||||
log.Fatalf("Fail to build: %v", err)
|
||||
}
|
||||
gocBuild, err := build.NewBuild(buildFlags, args, buildOutput, wd)
|
||||
gocBuild, err := build.NewBuild(buildFlags, args, wd, buildOutput)
|
||||
if err != nil {
|
||||
log.Fatalf("Fail to run: %v", err)
|
||||
}
|
||||
|
@ -149,5 +149,7 @@ func checkParameters(args []string, workingDir string) error {
|
||||
if workingDir == "" {
|
||||
return ErrInvalidWorkingDir
|
||||
}
|
||||
|
||||
log.Infof("Working directory: %v", workingDir)
|
||||
return nil
|
||||
}
|
||||
|
@ -17,6 +17,8 @@
|
||||
package build
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
@ -32,21 +34,59 @@ func TestInvalidPackage(t *testing.T) {
|
||||
os.Setenv("GOPATH", gopath)
|
||||
os.Setenv("GO111MODULE", "on")
|
||||
|
||||
_, err := NewBuild("", []string{"example.com/simple-project"}, "", workingDir)
|
||||
assert.Equal(t, err, ErrWrongPackageTypeForBuild, "the package name should be invalid")
|
||||
_, err := NewBuild("", []string{"example.com/simple-project"}, workingDir, "")
|
||||
if !assert.Equal(t, err, ErrWrongPackageTypeForBuild) {
|
||||
assert.FailNow(t, "the package name should be invalid")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBasicBuildForModProject(t *testing.T) {
|
||||
workingDir := filepath.Join(baseDir, "../tests/samples/simple_project")
|
||||
workingDir := filepath.Join(baseDir, "../../tests/samples/simple_project")
|
||||
gopath := ""
|
||||
|
||||
os.Setenv("GOPATH", gopath)
|
||||
os.Setenv("GO111MODULE", "on")
|
||||
|
||||
fmt.Println(workingDir)
|
||||
buildFlags, args, buildOutput := "", []string{"."}, ""
|
||||
gocBuild, err := NewBuild(buildFlags, args, buildOutput, workingDir)
|
||||
assert.Equal(t, err, nil, "should create temporary directory successfully")
|
||||
gocBuild, err := NewBuild(buildFlags, args, workingDir, buildOutput)
|
||||
if !assert.Equal(t, err, nil) {
|
||||
assert.FailNow(t, "should create temporary directory successfully")
|
||||
}
|
||||
|
||||
err = gocBuild.Build()
|
||||
assert.Equal(t, err, nil, "temporary directory should build successfully")
|
||||
if !assert.Equal(t, err, nil) {
|
||||
assert.FailNow(t, "temporary directory should build successfully")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckParameters(t *testing.T) {
|
||||
err := checkParameters([]string{"aa", "bb"}, "aa")
|
||||
assert.Equal(t, err, ErrTooManyArgs, "too many arguments should failed")
|
||||
|
||||
err = checkParameters([]string{"aa"}, "")
|
||||
assert.Equal(t, err, ErrInvalidWorkingDir, "empty working directory should failed")
|
||||
}
|
||||
|
||||
func TestDetermineOutputDir(t *testing.T) {
|
||||
b := &Build{}
|
||||
_, err := b.determineOutputDir("")
|
||||
assert.Equal(t, errors.Is(err, ErrWrongCallSequence), true, "called before Build.MvProjectsToTmp() should fail")
|
||||
|
||||
b.TmpDir = "fake"
|
||||
_, err = b.determineOutputDir("xx")
|
||||
assert.Equal(t, err, nil, "should return a directory")
|
||||
}
|
||||
|
||||
func TestInvalidPackageNameForBuild(t *testing.T) {
|
||||
workingDir := filepath.Join(baseDir, "../../tests/samples/simple_project")
|
||||
gopath := filepath.Join(baseDir, "../../tests/samples/simple_project", "testhome")
|
||||
|
||||
os.Setenv("GOPATH", gopath)
|
||||
os.Setenv("GO111MODULE", "on")
|
||||
|
||||
buildFlags, packages := "", []string{"main.go"}
|
||||
_, err := NewBuild(buildFlags, packages, workingDir, "")
|
||||
if !assert.Equal(t, err, ErrWrongPackageTypeForBuild) {
|
||||
assert.FailNow(t, "should not success with non . or ./... package")
|
||||
}
|
||||
}
|
||||
|
@ -9,16 +9,34 @@ import (
|
||||
)
|
||||
|
||||
func TestBasicInstallForModProject(t *testing.T) {
|
||||
workingDir := filepath.Join(baseDir, "../tests/samples/simple_project")
|
||||
gopath := filepath.Join(baseDir, "../tests/samples/simple_project", "testhome")
|
||||
workingDir := filepath.Join(baseDir, "../../tests/samples/simple_project")
|
||||
gopath := filepath.Join(baseDir, "../../tests/samples/simple_project", "testhome")
|
||||
|
||||
os.Setenv("GOPATH", gopath)
|
||||
os.Setenv("GO111MODULE", "on")
|
||||
|
||||
buildFlags, packages := "", []string{"."}
|
||||
gocBuild, err := NewInstall(buildFlags, packages, workingDir)
|
||||
assert.Equal(t, err, nil, "should create temporary directory successfully")
|
||||
if !assert.Equal(t, err, nil) {
|
||||
assert.FailNow(t, "should create temporary directory successfully")
|
||||
}
|
||||
|
||||
err = gocBuild.Install()
|
||||
assert.Equal(t, err, nil, "temporary directory should build successfully")
|
||||
if !assert.Equal(t, err, nil) {
|
||||
assert.FailNow(t, "temporary directory should build successfully")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInvalidPackageNameForInstall(t *testing.T) {
|
||||
workingDir := filepath.Join(baseDir, "../../tests/samples/simple_project")
|
||||
gopath := filepath.Join(baseDir, "../../tests/samples/simple_project", "testhome")
|
||||
|
||||
os.Setenv("GOPATH", gopath)
|
||||
os.Setenv("GO111MODULE", "on")
|
||||
|
||||
buildFlags, packages := "", []string{"main.go"}
|
||||
_, err := NewInstall(buildFlags, packages, workingDir)
|
||||
if !assert.Equal(t, err, ErrWrongPackageTypeForInstall) {
|
||||
assert.FailNow(t, "should not success with non . or ./... package")
|
||||
}
|
||||
}
|
||||
|
@ -36,7 +36,7 @@ func (b *Build) MvProjectsToTmp() error {
|
||||
}
|
||||
listArgs = append(listArgs, "./...")
|
||||
var err error
|
||||
b.Pkgs, err = cover.ListPackages(".", strings.Join(listArgs, " "), "")
|
||||
b.Pkgs, err = cover.ListPackages(b.WorkingDir, strings.Join(listArgs, " "), "")
|
||||
if err != nil {
|
||||
log.Errorln(err)
|
||||
return err
|
||||
@ -81,6 +81,7 @@ func (b *Build) mvProjectsToTmp() error {
|
||||
|
||||
// traverse pkg list to get project meta info
|
||||
b.IsMod, b.Root, err = b.traversePkgsList()
|
||||
log.Infof("mod project? %v", b.IsMod)
|
||||
if errors.Is(err, ErrShouldNotReached) {
|
||||
return fmt.Errorf("mvProjectsToTmp with a empty project: %w", err)
|
||||
}
|
||||
|
@ -46,7 +46,7 @@ func TestNewDirParseInLegacyProject(t *testing.T) {
|
||||
t.Fatalf("The New GOPATH is wrong. newgopath: %v, tmpdir: %v", b.NewGOPATH, b.TmpDir)
|
||||
}
|
||||
|
||||
b, _ = NewBuild("", []string{"."}, "", workingDir)
|
||||
b, _ = NewBuild("", []string{"."}, workingDir, "")
|
||||
if -1 == strings.Index(b.TmpWorkingDir, b.TmpDir) {
|
||||
t.Fatalf("Directory parse error. newwd: %v, tmpdir: %v", b.TmpWorkingDir, b.TmpDir)
|
||||
}
|
||||
@ -73,7 +73,7 @@ func TestNewDirParseInModProject(t *testing.T) {
|
||||
t.Fatalf("The New GOPATH is wrong. newgopath: %v, tmpdir: %v", b.NewGOPATH, b.TmpDir)
|
||||
}
|
||||
|
||||
b, _ = NewBuild("", []string{"."}, "", workingDir)
|
||||
b, _ = NewBuild("", []string{"."}, workingDir, "")
|
||||
if -1 == strings.Index(b.TmpWorkingDir, b.TmpDir) {
|
||||
t.Fatalf("Directory parse error. newwd: %v, tmpdir: %v", b.TmpWorkingDir, b.TmpDir)
|
||||
}
|
||||
@ -92,7 +92,7 @@ func TestLegacyProjectNotInGoPATH(t *testing.T) {
|
||||
os.Setenv("GOPATH", gopath)
|
||||
os.Setenv("GO111MODULE", "off")
|
||||
|
||||
b, _ := NewBuild("", []string{"."}, "", workingDir)
|
||||
b, _ := NewBuild("", []string{"."}, workingDir, "")
|
||||
if b.OriGOPATH != b.NewGOPATH {
|
||||
t.Fatalf("New GOPATH should be same with old GOPATH, for this kind of project. New: %v, old: %v", b.NewGOPATH, b.OriGOPATH)
|
||||
}
|
||||
|
@ -27,6 +27,7 @@ import (
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/otiai10/copy"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
@ -304,3 +305,40 @@ func TestFindInternal(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecuteForSimpleModProject(t *testing.T) {
|
||||
workingDir := "../../tests/samples/simple_project"
|
||||
gopath := ""
|
||||
|
||||
os.Setenv("GOPATH", gopath)
|
||||
os.Setenv("GO111MODULE", "on")
|
||||
|
||||
testDir := filepath.Join(os.TempDir(), "goc-build-test")
|
||||
copy.Copy(workingDir, testDir)
|
||||
|
||||
Execute("", gopath, testDir, "count", "http://127.0.0.1:7777")
|
||||
|
||||
_, err := os.Lstat(filepath.Join(testDir, "http_cover_apis_auto_generated.go"))
|
||||
if !assert.Equal(t, err, nil) {
|
||||
assert.FailNow(t, "should generate http_cover_apis_auto_generated.go")
|
||||
}
|
||||
}
|
||||
|
||||
func TestListPackagesForSimpleModProject(t *testing.T) {
|
||||
workingDir := "../../tests/samples/simple_project"
|
||||
gopath := ""
|
||||
|
||||
os.Setenv("GOPATH", gopath)
|
||||
os.Setenv("GO111MODULE", "on")
|
||||
|
||||
pkgs, _ := ListPackages(workingDir, "-json ./...", "")
|
||||
if !assert.Equal(t, len(pkgs), 1) {
|
||||
assert.FailNow(t, "should only have one pkg")
|
||||
}
|
||||
if pkg, ok := pkgs["example.com/simple-project"]; ok {
|
||||
assert.Equal(t, pkg.Module.Path, "example.com/simple-project")
|
||||
} else {
|
||||
assert.FailNow(t, "cannot get the pkg: example.com/simple-project")
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user