From 5892de106bae9ebda20d2dfba4b99b97c94cdb02 Mon Sep 17 00:00:00 2001 From: lyyyuna Date: Thu, 18 Jun 2020 18:08:48 +0800 Subject: [PATCH] fix unit test add test for cover package add unit test --- cmd/build_test.go | 2 +- cmd/root.go | 1 + cmd/run.go | 2 +- pkg/build/build.go | 2 ++ pkg/build/build_test.go | 54 ++++++++++++++++++++++++++++++++----- pkg/build/install_test.go | 26 +++++++++++++++--- pkg/build/tmpfolder.go | 3 ++- pkg/build/tmpfolder_test.go | 6 ++--- pkg/cover/cover_test.go | 38 ++++++++++++++++++++++++++ 9 files changed, 117 insertions(+), 17 deletions(-) diff --git a/cmd/build_test.go b/cmd/build_test.go index dcf02f6..9d46dee 100644 --- a/cmd/build_test.go +++ b/cmd/build_test.go @@ -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") diff --git a/cmd/root.go b/cmd/root.go index 09be748..3263c43 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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) { diff --git a/cmd/run.go b/cmd/run.go index 19d275a..1dc28ae 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -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) } diff --git a/pkg/build/build.go b/pkg/build/build.go index 3cb982d..53b8ce4 100644 --- a/pkg/build/build.go +++ b/pkg/build/build.go @@ -149,5 +149,7 @@ func checkParameters(args []string, workingDir string) error { if workingDir == "" { return ErrInvalidWorkingDir } + + log.Infof("Working directory: %v", workingDir) return nil } diff --git a/pkg/build/build_test.go b/pkg/build/build_test.go index fcc3142..82df7ea 100644 --- a/pkg/build/build_test.go +++ b/pkg/build/build_test.go @@ -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") + } } diff --git a/pkg/build/install_test.go b/pkg/build/install_test.go index 9b80bb1..30e996b 100644 --- a/pkg/build/install_test.go +++ b/pkg/build/install_test.go @@ -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") + } } diff --git a/pkg/build/tmpfolder.go b/pkg/build/tmpfolder.go index 1f64fd5..5998ebd 100644 --- a/pkg/build/tmpfolder.go +++ b/pkg/build/tmpfolder.go @@ -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) } diff --git a/pkg/build/tmpfolder_test.go b/pkg/build/tmpfolder_test.go index 81df11c..9373ac8 100644 --- a/pkg/build/tmpfolder_test.go +++ b/pkg/build/tmpfolder_test.go @@ -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) } diff --git a/pkg/cover/cover_test.go b/pkg/cover/cover_test.go index 1113b87..098cc67 100644 --- a/pkg/cover/cover_test.go +++ b/pkg/cover/cover_test.go @@ -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") + } + +}