add unit test
add unit test for install fix unit test add license add test
This commit is contained in:
parent
40bdcf21d4
commit
a5f358576d
37
cmd/build.go
37
cmd/build.go
@ -17,10 +17,11 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
"github.com/qiniu/goc/pkg/build"
|
"github.com/qiniu/goc/pkg/build"
|
||||||
"github.com/qiniu/goc/pkg/cover"
|
"github.com/qiniu/goc/pkg/cover"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"log"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var buildCmd = &cobra.Command{
|
var buildCmd = &cobra.Command{
|
||||||
@ -43,21 +44,7 @@ goc build --output /to/this/path
|
|||||||
goc build --buildflags="-ldflags '-extldflags -static' -tags='embed kodo'"
|
goc build --buildflags="-ldflags '-extldflags -static' -tags='embed kodo'"
|
||||||
`,
|
`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
gocBuild, err := build.NewBuild(buildFlags, packages, buildOutput)
|
runBuild()
|
||||||
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
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -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")
|
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)
|
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
|
||||||
|
}
|
||||||
|
61
cmd/build_test.go
Normal file
61
cmd/build_test.go
Normal file
@ -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")
|
||||||
|
}
|
@ -40,21 +40,7 @@ goc install --center=http://127.0.0.1:7777
|
|||||||
goc build --buildflags="-ldflags '-extldflags -static' -tags='embed kodo'"
|
goc build --buildflags="-ldflags '-extldflags -static' -tags='embed kodo'"
|
||||||
`,
|
`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
gocBuild, err := build.NewInstall(buildFlags, packages)
|
runInstall()
|
||||||
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
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,3 +48,21 @@ func init() {
|
|||||||
addBuildFlags(installCmd.Flags())
|
addBuildFlags(installCmd.Flags())
|
||||||
rootCmd.AddCommand(installCmd)
|
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
|
||||||
|
}
|
||||||
|
83
cmd/install_test.go
Normal file
83
cmd/install_test.go
Normal file
@ -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")
|
||||||
|
}
|
53
pkg/build/build_test.go
Normal file
53
pkg/build/build_test.go
Normal file
@ -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")
|
||||||
|
}
|
24
pkg/build/install_test.go
Normal file
24
pkg/build/install_test.go
Normal file
@ -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")
|
||||||
|
}
|
@ -24,12 +24,18 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var baseDir string
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
baseDir, _ = os.Getwd()
|
||||||
|
}
|
||||||
|
|
||||||
func TestNewDirParseInLegacyProject(t *testing.T) {
|
func TestNewDirParseInLegacyProject(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, _ := filepath.Abs("../../tests/samples/simple_gopath_project")
|
gopath := filepath.Join(baseDir, "../../tests/samples/simple_gopath_project")
|
||||||
|
|
||||||
os.Chdir(workingDir)
|
os.Chdir(workingDir)
|
||||||
fmt.Println(gopath)
|
|
||||||
os.Setenv("GOPATH", gopath)
|
os.Setenv("GOPATH", gopath)
|
||||||
os.Setenv("GO111MODULE", "off")
|
os.Setenv("GO111MODULE", "off")
|
||||||
|
|
||||||
@ -53,7 +59,7 @@ func TestNewDirParseInLegacyProject(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestNewDirParseInModProject(t *testing.T) {
|
func TestNewDirParseInModProject(t *testing.T) {
|
||||||
workingDir := "../../tests/samples/simple_project"
|
workingDir := filepath.Join(baseDir, "../../tests/samples/simple_project")
|
||||||
gopath := ""
|
gopath := ""
|
||||||
|
|
||||||
os.Chdir(workingDir)
|
os.Chdir(workingDir)
|
||||||
@ -82,15 +88,21 @@ func TestNewDirParseInModProject(t *testing.T) {
|
|||||||
|
|
||||||
// Test #14
|
// Test #14
|
||||||
func TestLegacyProjectNotInGoPATH(t *testing.T) {
|
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 := ""
|
gopath := ""
|
||||||
|
|
||||||
os.Chdir(workingDir)
|
os.Chdir(workingDir)
|
||||||
fmt.Println(gopath)
|
fmt.Println(gopath)
|
||||||
os.Setenv("GOPATH", gopath)
|
os.Setenv("GOPATH", gopath)
|
||||||
os.Setenv("GO111MODULE", "off")
|
os.Setenv("GO111MODULE", "off")
|
||||||
|
|
||||||
b, _ := NewBuild("", ".", "")
|
b, _ := NewBuild("", ".", "")
|
||||||
if b.OriGOPATH != b.NewGOPATH {
|
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)
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user