add unit test
add unit test for install fix unit test add license add test
This commit is contained in:
parent
40bdcf21d4
commit
a5f358576d
25
cmd/build.go
25
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,6 +44,19 @@ goc build --output /to/this/path
|
||||
goc build --buildflags="-ldflags '-extldflags -static' -tags='embed kodo'"
|
||||
`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
runBuild()
|
||||
},
|
||||
}
|
||||
|
||||
var buildOutput string
|
||||
|
||||
func init() {
|
||||
addBuildFlags(buildCmd.Flags())
|
||||
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)
|
||||
@ -58,13 +72,4 @@ goc build --buildflags="-ldflags '-extldflags -static' -tags='embed kodo'"
|
||||
log.Fatalf("Fail to build: %v", err)
|
||||
}
|
||||
return
|
||||
},
|
||||
}
|
||||
|
||||
var buildOutput string
|
||||
|
||||
func init() {
|
||||
addBuildFlags(buildCmd.Flags())
|
||||
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)
|
||||
}
|
||||
|
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,6 +40,16 @@ 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) {
|
||||
runInstall()
|
||||
},
|
||||
}
|
||||
|
||||
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)
|
||||
@ -55,10 +65,4 @@ goc build --buildflags="-ldflags '-extldflags -static' -tags='embed kodo'"
|
||||
log.Fatalf("Fail to install: %v", err)
|
||||
}
|
||||
return
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
addBuildFlags(installCmd.Flags())
|
||||
rootCmd.AddCommand(installCmd)
|
||||
}
|
||||
|
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"
|
||||
)
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user