From a5f358576dff005fc141b7c3c1fa487f7c42aa36 Mon Sep 17 00:00:00 2001 From: lyyyuna Date: Tue, 16 Jun 2020 17:00:38 +0800 Subject: [PATCH] add unit test add unit test for install fix unit test add license add test --- cmd/build.go | 37 ++++++++++------- cmd/build_test.go | 61 +++++++++++++++++++++++++++ cmd/install.go | 34 ++++++++------- cmd/install_test.go | 83 +++++++++++++++++++++++++++++++++++++ pkg/build/build_test.go | 53 +++++++++++++++++++++++ pkg/build/install_test.go | 24 +++++++++++ pkg/build/tmpfolder_test.go | 22 +++++++--- 7 files changed, 278 insertions(+), 36 deletions(-) create mode 100644 cmd/build_test.go create mode 100644 cmd/install_test.go create mode 100644 pkg/build/build_test.go create mode 100644 pkg/build/install_test.go diff --git a/cmd/build.go b/cmd/build.go index 533110a..9bc6869 100644 --- a/cmd/build.go +++ b/cmd/build.go @@ -17,10 +17,11 @@ package cmd import ( + "log" + "github.com/qiniu/goc/pkg/build" "github.com/qiniu/goc/pkg/cover" "github.com/spf13/cobra" - "log" ) var buildCmd = &cobra.Command{ @@ -43,21 +44,7 @@ goc build --output /to/this/path goc build --buildflags="-ldflags '-extldflags -static' -tags='embed kodo'" `, Run: func(cmd *cobra.Command, args []string) { - gocBuild, err := build.NewBuild(buildFlags, packages, buildOutput) - if err != nil { - log.Fatalf("Fail to NewBuild: %v", err) - } - // remove temporary directory if needed - defer gocBuild.Clean() - // doCover with original buildFlags, with new GOPATH( tmp:original ) - // in the tmp directory - cover.Execute(buildFlags, gocBuild.NewGOPATH, gocBuild.TmpDir, mode, center) - // do install in the temporary directory - err = gocBuild.Build() - if err != nil { - log.Fatalf("Fail to build: %v", err) - } - return + runBuild() }, } @@ -68,3 +55,21 @@ func init() { buildCmd.Flags().StringVar(&buildOutput, "output", "", "it forces build to write the resulting executable or object to the named output file or directory") rootCmd.AddCommand(buildCmd) } + +func runBuild() { + gocBuild, err := build.NewBuild(buildFlags, packages, buildOutput) + if err != nil { + log.Fatalf("Fail to NewBuild: %v", err) + } + // remove temporary directory if needed + defer gocBuild.Clean() + // doCover with original buildFlags, with new GOPATH( tmp:original ) + // in the tmp directory + cover.Execute(buildFlags, gocBuild.NewGOPATH, gocBuild.TmpDir, mode, center) + // do install in the temporary directory + err = gocBuild.Build() + if err != nil { + log.Fatalf("Fail to build: %v", err) + } + return +} diff --git a/cmd/build_test.go b/cmd/build_test.go new file mode 100644 index 0000000..db005d6 --- /dev/null +++ b/cmd/build_test.go @@ -0,0 +1,61 @@ +/* + Copyright 2020 Qiniu Cloud (qiniu.com) + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package cmd + +import ( + "github.com/stretchr/testify/assert" + "os" + "os/exec" + "path/filepath" + "strings" + "testing" + "time" +) + +var baseDir string + +func init() { + baseDir, _ = os.Getwd() +} + +func TestGeneratedBinary(t *testing.T) { + startTime := time.Now() + + workingDir := filepath.Join(baseDir, "../tests/samples/simple_project") + gopath := "" + + os.Chdir(workingDir) + os.Setenv("GOPATH", gopath) + os.Setenv("GO111MODULE", "on") + + buildFlags, packages, buildOutput = "", ".", "" + runBuild() + + obj := filepath.Join(".", "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") + + cmd := exec.Command("go", "tool", "objdump", "simple-project") + cmd.Dir = workingDir + out, _ := cmd.CombinedOutput() + cnt := strings.Count(string(out), "main.registerSelf") + assert.Equal(t, cnt > 0, true, "main.registerSelf function should be in the binary") + + cnt = strings.Count(string(out), "GoCover") + assert.Equal(t, cnt > 0, true, "GoCover varibale should be in the binary") +} diff --git a/cmd/install.go b/cmd/install.go index 080a66c..028230f 100644 --- a/cmd/install.go +++ b/cmd/install.go @@ -40,21 +40,7 @@ goc install --center=http://127.0.0.1:7777 goc build --buildflags="-ldflags '-extldflags -static' -tags='embed kodo'" `, Run: func(cmd *cobra.Command, args []string) { - gocBuild, err := build.NewInstall(buildFlags, packages) - if err != nil { - log.Fatalf("Fail to NewInstall: %v", err) - } - // remove temporary directory if needed - defer gocBuild.Clean() - // doCover with original buildFlags, with new GOPATH( tmp:original ) - // in the tmp directory - cover.Execute(buildFlags, gocBuild.NewGOPATH, gocBuild.TmpDir, mode, center) - // do install in the temporary directory - err = gocBuild.Install() - if err != nil { - log.Fatalf("Fail to install: %v", err) - } - return + runInstall() }, } @@ -62,3 +48,21 @@ func init() { addBuildFlags(installCmd.Flags()) rootCmd.AddCommand(installCmd) } + +func runInstall() { + gocBuild, err := build.NewInstall(buildFlags, packages) + if err != nil { + log.Fatalf("Fail to NewInstall: %v", err) + } + // remove temporary directory if needed + defer gocBuild.Clean() + // doCover with original buildFlags, with new GOPATH( tmp:original ) + // in the tmp directory + cover.Execute(buildFlags, gocBuild.NewGOPATH, gocBuild.TmpDir, mode, center) + // do install in the temporary directory + err = gocBuild.Install() + if err != nil { + log.Fatalf("Fail to install: %v", err) + } + return +} diff --git a/cmd/install_test.go b/cmd/install_test.go new file mode 100644 index 0000000..150d098 --- /dev/null +++ b/cmd/install_test.go @@ -0,0 +1,83 @@ +/* + Copyright 2020 Qiniu Cloud (qiniu.com) + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package cmd + +import ( + "github.com/stretchr/testify/assert" + "os" + "os/exec" + "path/filepath" + "strings" + "testing" + "time" +) + +func TestInstalledBinaryForMod(t *testing.T) { + startTime := time.Now() + + workingDir := filepath.Join(baseDir, "../tests/samples/simple_project") + gopath := filepath.Join(baseDir, "../tests/samples/simple_project", "testhome") + + os.Chdir(workingDir) + os.Setenv("GOPATH", gopath) + os.Setenv("GO111MODULE", "on") + + buildFlags, packages, buildOutput = "", ".", "" + runInstall() + + obj := filepath.Join(gopath, "bin", "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") + + cmd := exec.Command("go", "tool", "objdump", "simple-project") + cmd.Dir = workingDir + out, _ := cmd.CombinedOutput() + cnt := strings.Count(string(out), "main.registerSelf") + assert.Equal(t, cnt > 0, true, "main.registerSelf function should be in the binary") + + cnt = strings.Count(string(out), "GoCover") + assert.Equal(t, cnt > 0, true, "GoCover varibale should be in the binary") +} + +func TestInstalledBinaryForLegacy(t *testing.T) { + startTime := time.Now() + + workingDir := filepath.Join(baseDir, "../tests/samples/simple_gopath_project/src/qiniu.com/simple_gopath_project") + gopath := filepath.Join(baseDir, "../tests/samples/simple_gopath_project") + + os.Chdir(workingDir) + os.Setenv("GOPATH", gopath) + os.Setenv("GO111MODULE", "off") + + buildFlags, packages, buildOutput = "", ".", "" + runInstall() + + obj := filepath.Join(gopath, "bin", "simple_gopath_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") + + cmd := exec.Command("go", "tool", "objdump", obj) + cmd.Dir = workingDir + out, _ := cmd.CombinedOutput() + cnt := strings.Count(string(out), "main.registerSelf") + assert.Equal(t, cnt > 0, true, "main.registerSelf function should be in the binary") + + cnt = strings.Count(string(out), "GoCover") + assert.Equal(t, cnt > 0, true, "GoCover varibale should be in the binary") +} diff --git a/pkg/build/build_test.go b/pkg/build/build_test.go new file mode 100644 index 0000000..fa25263 --- /dev/null +++ b/pkg/build/build_test.go @@ -0,0 +1,53 @@ +/* + Copyright 2020 Qiniu Cloud (qiniu.com) + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package build + +import ( + "github.com/stretchr/testify/assert" + "os" + "path/filepath" + "testing" +) + +func TestInvalidPackage(t *testing.T) { + + workingDir := filepath.Join(baseDir, "../../tests/samples/simple_project") + gopath := "" + + os.Chdir(workingDir) + os.Setenv("GOPATH", gopath) + os.Setenv("GO111MODULE", "on") + + _, err := NewBuild("", "example.com/simple-project", "") + assert.Equal(t, err, ErrWrongPackageTypeForBuild, "the package name should be invalid") +} + +func TestBasicBuildForModProject(t *testing.T) { + workingDir := filepath.Join(baseDir, "../tests/samples/simple_project") + gopath := "" + + os.Chdir(workingDir) + os.Setenv("GOPATH", gopath) + os.Setenv("GO111MODULE", "on") + + buildFlags, packages, buildOutput := "", ".", "" + gocBuild, err := NewBuild(buildFlags, packages, buildOutput) + assert.Equal(t, err, nil, "should create temporary directory successfully") + + err = gocBuild.Build() + assert.Equal(t, err, nil, "temporary directory should build successfully") +} diff --git a/pkg/build/install_test.go b/pkg/build/install_test.go new file mode 100644 index 0000000..9108b3f --- /dev/null +++ b/pkg/build/install_test.go @@ -0,0 +1,24 @@ +package build + +import ( + "github.com/stretchr/testify/assert" + "os" + "path/filepath" + "testing" +) + +func TestBasicInstallForModProject(t *testing.T) { + workingDir := filepath.Join(baseDir, "../tests/samples/simple_project") + gopath := filepath.Join(baseDir, "../tests/samples/simple_project", "testhome") + + os.Chdir(workingDir) + os.Setenv("GOPATH", gopath) + os.Setenv("GO111MODULE", "on") + + buildFlags, packages := "", "." + gocBuild, err := NewInstall(buildFlags, packages) + assert.Equal(t, err, nil, "should create temporary directory successfully") + + err = gocBuild.Install() + assert.Equal(t, err, nil, "temporary directory should build successfully") +} diff --git a/pkg/build/tmpfolder_test.go b/pkg/build/tmpfolder_test.go index 6804fa2..ed4f4b8 100644 --- a/pkg/build/tmpfolder_test.go +++ b/pkg/build/tmpfolder_test.go @@ -24,12 +24,18 @@ import ( "testing" ) +var baseDir string + +func init() { + baseDir, _ = os.Getwd() +} + func TestNewDirParseInLegacyProject(t *testing.T) { - workingDir := "../../tests/samples/simple_gopath_project/src/qiniu.com/simple_gopath_project" - gopath, _ := filepath.Abs("../../tests/samples/simple_gopath_project") + workingDir := filepath.Join(baseDir, "../../tests/samples/simple_gopath_project/src/qiniu.com/simple_gopath_project") + gopath := filepath.Join(baseDir, "../../tests/samples/simple_gopath_project") os.Chdir(workingDir) - fmt.Println(gopath) + os.Setenv("GOPATH", gopath) os.Setenv("GO111MODULE", "off") @@ -53,7 +59,7 @@ func TestNewDirParseInLegacyProject(t *testing.T) { } func TestNewDirParseInModProject(t *testing.T) { - workingDir := "../../tests/samples/simple_project" + workingDir := filepath.Join(baseDir, "../../tests/samples/simple_project") gopath := "" os.Chdir(workingDir) @@ -82,15 +88,21 @@ func TestNewDirParseInModProject(t *testing.T) { // Test #14 func TestLegacyProjectNotInGoPATH(t *testing.T) { - workingDir := "../../tests/samples/simple_gopath_project/src/qiniu.com/simple_gopath_project" + workingDir := filepath.Join(baseDir, "../../tests/samples/simple_gopath_project/src/qiniu.com/simple_gopath_project") gopath := "" os.Chdir(workingDir) fmt.Println(gopath) os.Setenv("GOPATH", gopath) os.Setenv("GO111MODULE", "off") + b, _ := NewBuild("", ".", "") 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) } + + _, err := os.Stat(filepath.Join(b.TmpDir, "main.go")) + if err != nil { + t.Fatalf("There should be a main.go in temporary directory directly, the error: %v", err) + } }