This commit is contained in:
lyyyuna 2020-06-12 17:51:10 +08:00
parent d80dfa6ef4
commit cd302c790d
19 changed files with 250 additions and 256 deletions

View File

@ -25,7 +25,6 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/qiniu/goc/pkg/build"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -51,8 +50,12 @@ goc build -- -o /to/this/path
goc build -- -ldflags "-extldflags -static" -tags="embed kodo" goc build -- -ldflags "-extldflags -static" -tags="embed kodo"
`, `,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
newgopath, newwd, tmpdir, pkgs := build.MvProjectsToTmp(target, args)
doCover(cmd, args, newgopath, tmpdir) return
/*
gocbuild := build.NewInstall(buildFlags, packages)
newgopath, newwd, tmpdir, _ := gocbuild.MvProjectsToTmp()
doCover("args", newgopath, tmpdir)
newArgs, modified := modifyOutputArg(args) newArgs, modified := modifyOutputArg(args)
doBuild(newArgs, newgopath, newwd) doBuild(newArgs, newgopath, newwd)
@ -60,14 +63,15 @@ goc build -- -ldflags "-extldflags -static" -tags="embed kodo"
// find the binary in temp build dir // find the binary in temp build dir
// and copy them into original dir // and copy them into original dir
if false == modified { if false == modified {
build.MvBinaryToOri(pkgs, tmpdir) // build.MvBinaryToOri(pkgs, tmpdir)
} }
*/
}, },
} }
func init() { func init() {
buildCmd.Flags().StringVarP(&center, "center", "", "http://127.0.0.1:7777", "cover profile host center") addBuildFlags(buildCmd.Flags())
rootCmd.AddCommand(buildCmd) rootCmd.AddCommand(buildCmd)
} }

View File

@ -46,6 +46,6 @@ goc clear --center=http://192.168.1.1:8080
} }
func init() { func init() {
clearCmd.Flags().StringVarP(&center, "center", "", "http://127.0.0.1:7777", "cover profile host center") addBasicFlags(clearCmd.Flags())
rootCmd.AddCommand(clearCmd) rootCmd.AddCommand(clearCmd)
} }

49
cmd/commonflags.go Normal file
View File

@ -0,0 +1,49 @@
package cmd
import (
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
var (
target string
center string
mode string
debugGoc bool
buildFlags string
packages string
appArgs string
)
// addBasicFlags adds a
func addBasicFlags(cmdset *pflag.FlagSet) *pflag.FlagSet {
cmdset.StringVar(&center, "center", "http://127.0.0.1:7777", "cover profile host center")
// bind to viper
viper.BindPFlags(cmdset)
return cmdset
}
func addCommonFlags(cmdset *pflag.FlagSet) *pflag.FlagSet {
addBasicFlags(cmdset)
cmdset.StringVar(&mode, "mode", "count", "coverage mode: set, count, atomic")
cmdset.StringVar(&buildFlags, "buildflags", "", "specify the build flags")
// bind to viper
viper.BindPFlags(cmdset)
return cmdset
}
func addBuildFlags(cmdset *pflag.FlagSet) *pflag.FlagSet {
addCommonFlags(cmdset)
cmdset.StringVar(&packages, "packages", ".", "specify the package name, only . and ./... are supported")
// bind to viper
viper.BindPFlags(cmdset)
return cmdset
}
func addRunFlags(cmdset *pflag.FlagSet) {
addBuildFlags(cmdset)
cmdset.Lookup("packages").Usage = "specify the package name, only ., ./... and *.go are supported"
cmdset.StringVar(&appArgs, "appargs", "", "specify the application's arguments")
// bind to viper
viper.BindPFlags(cmdset)
}

View File

@ -18,6 +18,7 @@ package cmd
import ( import (
"fmt" "fmt"
"github.com/spf13/viper"
"log" "log"
"os" "os"
"strings" "strings"
@ -40,7 +41,10 @@ goc cover --center=http://127.0.0.1:7777
# Do cover for the target path, cover mode: atomic. # Do cover for the target path, cover mode: atomic.
goc cover --center=http://127.0.0.1:7777 --target=/path/to/target --mode=atomic goc cover --center=http://127.0.0.1:7777 --target=/path/to/target --mode=atomic
`, `,
Hidden: true,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
var buildFlags string
buildFlags = viper.GetString("buildflags")
if mode == "" { if mode == "" {
log.Fatalf("Error: flag needs an argument: -mode %v", mode) log.Fatalf("Error: flag needs an argument: -mode %v", mode)
} }
@ -48,39 +52,27 @@ goc cover --center=http://127.0.0.1:7777 --target=/path/to/target --mode=atomic
log.Fatalf("unknown -mode %v", mode) log.Fatalf("unknown -mode %v", mode)
} }
doCover(cmd, args, "", "") doCover(buildFlags, "", target)
}, },
} }
var (
target string
center string
mode string
)
func init() { func init() {
coverCmd.Flags().StringVarP(&center, "center", "", "http://127.0.0.1:7777", "cover profile host center") coverCmd.Flags().StringVar(&target, "target", ".", "target folder to cover")
coverCmd.Flags().StringVarP(&target, "target", "", ".", "target folder to cover") addCommonFlags(coverCmd.Flags())
coverCmd.Flags().StringVarP(&mode, "mode", "", "count", "coverage mode: set, count, atomic")
rootCmd.AddCommand(coverCmd) rootCmd.AddCommand(coverCmd)
log.SetFlags(log.LstdFlags | log.Lshortfile)
} }
func doCover(cmd *cobra.Command, args []string, newgopath string, newtarget string) { func doCover(args string, newgopath string, target string) {
if newtarget != "" {
target = newtarget
}
if !isDirExist(target) { if !isDirExist(target) {
log.Fatalf("target directory %s not exist", target) log.Fatalf("target directory %s not exist", target)
} }
listArgs := []string{"list", "-json"} listArgs := []string{"-json"}
if len(args) != 0 { if len(args) != 0 {
listArgs = append(listArgs, args...) listArgs = append(listArgs, args)
} }
listArgs = append(listArgs, "./...") listArgs = append(listArgs, "./...")
pkgs := cover.ListPackages(target, listArgs, newgopath) pkgs := cover.ListPackages(target, strings.Join(listArgs, " "), newgopath)
var seen = make(map[string]*cover.PackageCover) var seen = make(map[string]*cover.PackageCover)
var seenCache = make(map[string]*cover.PackageCover) var seenCache = make(map[string]*cover.PackageCover)

View File

@ -34,6 +34,6 @@ var initCmd = &cobra.Command{
} }
func init() { func init() {
initCmd.Flags().StringVarP(&center, "center", "", "http://127.0.0.1:7777", "cover profile host center") addBasicFlags(initCmd.Flags())
rootCmd.AddCommand(initCmd) rootCmd.AddCommand(initCmd)
} }

View File

@ -17,14 +17,7 @@
package cmd package cmd
import ( import (
"fmt"
"log"
"os"
"os/exec"
"strings"
"github.com/qiniu/goc/pkg/build" "github.com/qiniu/goc/pkg/build"
"github.com/qiniu/goc/pkg/cover"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -38,44 +31,26 @@ To pass origial go build flags to goc command, place them after "--", see exampl
`, `,
Example: ` Example: `
# Install all binaries with cover variables injected. The binary will be installed in $GOPATH/bin or $HOME/go/bin if directory existed. # Install all binaries with cover variables injected. The binary will be installed in $GOPATH/bin or $HOME/go/bin if directory existed.
goc install -- ./... goc install --packages="./..."
# Install the current binary with cover variables injected, and set the registry center to http://127.0.0.1:7777. # Install the current binary with cover variables injected, and set the registry center to http://127.0.0.1:7777.
goc install --center=http://127.0.0.1:7777 goc install --center=http://127.0.0.1:7777
# Install the current binary with cover variables injected, and set necessary build flags: -ldflags "-extldflags -static" -tags="embed kodo". # Install the current binary with cover variables injected, and set necessary build flags: -ldflags "-extldflags -static" -tags="embed kodo".
goc build -- -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) {
newgopath, newwd, tmpdir, pkgs := build.MvProjectsToTmp(target, args) gocbuild := build.NewInstall(buildFlags, packages)
doCover(cmd, args, newgopath, tmpdir) gocbuild.MvProjectsToTmp()
doInstall(args, newgopath, newwd, pkgs) // doCover with original buildFlags, with new GOPATH( tmp:original )
// in the tmp directory
doCover(buildFlags, gocbuild.NewGOPATH, gocbuild.TmpDir)
//
gocbuild.Install()
}, },
} }
func init() { func init() {
installCmd.Flags().StringVarP(&center, "center", "", "http://127.0.0.1:7777", "cover profile host center") addBuildFlags(installCmd.Flags())
rootCmd.AddCommand(installCmd) rootCmd.AddCommand(installCmd)
} }
func doInstall(args []string, newgopath string, newworkingdir string, pkgs map[string]*cover.Package) {
log.Println("Go building in temp...")
newArgs := []string{"install"}
newArgs = append(newArgs, args...)
cmd := exec.Command("go", newArgs...)
cmd.Dir = newworkingdir
// Change the temp GOBIN, to force binary install to original place
cmd.Env = append(os.Environ(), fmt.Sprintf("GOBIN=%v", build.FindWhereToInstall(pkgs)))
if newgopath != "" {
// Change to temp GOPATH for go install command
cmd.Env = append(cmd.Env, fmt.Sprintf("GOPATH=%v", newgopath))
}
out, err := cmd.CombinedOutput()
if err != nil {
log.Fatalf("Fail to execute: go install %v. The error is: %v, the stdout/stderr is: %v", strings.Join(args, " "), err, string(out))
}
log.Printf("Go install successful. Binary installed in: %v", build.FindWhereToInstall(pkgs))
}

View File

@ -70,6 +70,6 @@ var output string
func init() { func init() {
profileCmd.Flags().StringVarP(&output, "output", "o", "", "download cover profile") profileCmd.Flags().StringVarP(&output, "output", "o", "", "download cover profile")
profileCmd.Flags().StringVarP(&center, "center", "", "http://127.0.0.1:7777", "cover profile host center") addBasicFlags(profileCmd.Flags())
rootCmd.AddCommand(profileCmd) rootCmd.AddCommand(profileCmd)
} }

View File

@ -17,6 +17,7 @@
package cmd package cmd
import ( import (
"io/ioutil"
"log" "log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -28,8 +29,20 @@ var rootCmd = &cobra.Command{
Long: `goc is a comprehensive coverage testing tool for go language. Long: `goc is a comprehensive coverage testing tool for go language.
Find more information at: Find more information at:
https://github.com/qbox/goc https://github.com/qiniu/goc
`, `,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
log.SetFlags(log.LstdFlags | log.Lshortfile)
if debugGoc == false {
// we only need log in debug mode
log.SetOutput(ioutil.Discard)
}
},
}
func init() {
rootCmd.PersistentFlags().BoolVar(&debugGoc, "debuggoc", false, "turn goc into debug mode")
rootCmd.PersistentFlags().MarkHidden("debuggoc")
} }
// Execute the goc tool // Execute the goc tool

2
go.mod
View File

@ -16,6 +16,8 @@ require (
github.com/qiniu/api.v7/v7 v7.5.0 github.com/qiniu/api.v7/v7 v7.5.0
github.com/sirupsen/logrus v1.4.2 github.com/sirupsen/logrus v1.4.2
github.com/spf13/cobra v1.0.0 github.com/spf13/cobra v1.0.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.6.2
github.com/stretchr/testify v1.5.1 github.com/stretchr/testify v1.5.1
golang.org/x/net v0.0.0-20200301022130-244492dfa37a golang.org/x/net v0.0.0-20200301022130-244492dfa37a
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d

9
go.sum
View File

@ -410,6 +410,7 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.3/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= github.com/hashicorp/golang-lru v0.5.3/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
@ -512,6 +513,7 @@ github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFW
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/go-wordwrap v1.0.0/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo= github.com/mitchellh/go-wordwrap v1.0.0/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo=
github.com/mitchellh/ioprogress v0.0.0-20180201004757-6a23b12fa88e/go.mod h1:waEya8ee1Ro/lgxpVhkJI4BVASzkm3UZqkx/cFJiYHM= github.com/mitchellh/ioprogress v0.0.0-20180201004757-6a23b12fa88e/go.mod h1:waEya8ee1Ro/lgxpVhkJI4BVASzkm3UZqkx/cFJiYHM=
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/osext v0.0.0-20151018003038-5e2d6d41470f/go.mod h1:OkQIRizQZAeMln+1tSwduZz7+Af5oFlKirV/MSYes2A= github.com/mitchellh/osext v0.0.0-20151018003038-5e2d6d41470f/go.mod h1:OkQIRizQZAeMln+1tSwduZz7+Af5oFlKirV/MSYes2A=
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw= github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
@ -576,6 +578,7 @@ github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT9
github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k= github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k=
github.com/pelletier/go-buffruneio v0.2.0/go.mod h1:JkE26KsDizTr40EUHkXVtNPvgGtbSNq5BcowyYOWdKo= github.com/pelletier/go-buffruneio v0.2.0/go.mod h1:JkE26KsDizTr40EUHkXVtNPvgGtbSNq5BcowyYOWdKo=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pelletier/go-toml v1.6.0 h1:aetoXYr0Tv7xRU/V4B4IZJ2QcbtMUFoNb3ORp7TzIK4=
github.com/pelletier/go-toml v1.6.0/go.mod h1:5N711Q9dKgbdkxHL+MEfF31hpT7l0S0s/t2kKREewys= github.com/pelletier/go-toml v1.6.0/go.mod h1:5N711Q9dKgbdkxHL+MEfF31hpT7l0S0s/t2kKREewys=
github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU= github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU=
github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2/go.mod h1:iIss55rKnNBTvrwdmkUpLnDpZoAHvWaiq5+iMmen4AE= github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2/go.mod h1:iIss55rKnNBTvrwdmkUpLnDpZoAHvWaiq5+iMmen4AE=
@ -656,8 +659,10 @@ github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM= github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/afero v1.2.2 h1:5jhuqJyZCZf2JRofRvN/nIFgIWNzPa3/Vz8mYylgbWc=
github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk= github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk=
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cast v1.3.1 h1:nFm6S0SMdyzrzcmThSipiEubIDy8WEXKNZ0UOgiRpng=
github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cobra v0.0.2-0.20171109065643-2da4a54c5cee/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ= github.com/spf13/cobra v0.0.2-0.20171109065643-2da4a54c5cee/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ= github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
@ -666,6 +671,7 @@ github.com/spf13/cobra v0.0.6/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHN
github.com/spf13/cobra v1.0.0 h1:6m/oheQuQ13N9ks4hubMG6BnvwOeaJrqSPLahSnczz8= github.com/spf13/cobra v1.0.0 h1:6m/oheQuQ13N9ks4hubMG6BnvwOeaJrqSPLahSnczz8=
github.com/spf13/cobra v1.0.0/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE= github.com/spf13/cobra v1.0.0/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE=
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
github.com/spf13/jwalterweatherman v1.1.0 h1:ue6voC5bR5F8YxI5S67j9i582FU4Qvo2bmqnqMYADFk=
github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo= github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo=
github.com/spf13/pflag v0.0.0-20170130214245-9ff6c6923cff/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/spf13/pflag v0.0.0-20170130214245-9ff6c6923cff/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/pflag v1.0.1-0.20171106142849-4c012f6dcd95/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/spf13/pflag v1.0.1-0.20171106142849-4c012f6dcd95/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
@ -676,6 +682,7 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s= github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE= github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE=
github.com/spf13/viper v1.6.2 h1:7aKfF+e8/k68gda3LOjo5RxiUqddoFxVq4BKBPrxk5E=
github.com/spf13/viper v1.6.2/go.mod h1:t3iDnF5Jlj76alVNuyFBk5oUMCvsrkbvZK0WQdfDi5k= github.com/spf13/viper v1.6.2/go.mod h1:t3iDnF5Jlj76alVNuyFBk5oUMCvsrkbvZK0WQdfDi5k=
github.com/src-d/gcfg v1.4.0/go.mod h1:p/UMsR43ujA89BJY9duynAwIpvqEujIH/jFlfL7jWoI= github.com/src-d/gcfg v1.4.0/go.mod h1:p/UMsR43ujA89BJY9duynAwIpvqEujIH/jFlfL7jWoI=
github.com/streadway/amqp v0.0.0-20190404075320-75d898a42a94/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw= github.com/streadway/amqp v0.0.0-20190404075320-75d898a42a94/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw=
@ -688,6 +695,7 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/syndtr/gocapability v0.0.0-20170704070218-db04d3cc01c8/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww= github.com/syndtr/gocapability v0.0.0-20170704070218-db04d3cc01c8/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
github.com/tektoncd/pipeline v0.11.0/go.mod h1:hlkH32S92+/UODROH0dmxzyuMxfRFp/Nc3e29MewLn8= github.com/tektoncd/pipeline v0.11.0/go.mod h1:hlkH32S92+/UODROH0dmxzyuMxfRFp/Nc3e29MewLn8=
@ -1009,6 +1017,7 @@ gopkg.in/inf.v0 v0.9.0/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw= gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
gopkg.in/ini.v1 v1.46.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/ini.v1 v1.46.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/ini.v1 v1.52.0 h1:j+Lt/M1oPPejkniCg1TkWE2J3Eh1oZTsHSXzMTzUXn4=
gopkg.in/ini.v1 v1.52.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/ini.v1 v1.52.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/jcmturner/aescts.v1 v1.0.1/go.mod h1:nsR8qBOg+OucoIW+WMhB3GspUQXq9XorLnQb9XtvcOo= gopkg.in/jcmturner/aescts.v1 v1.0.1/go.mod h1:nsR8qBOg+OucoIW+WMhB3GspUQXq9XorLnQb9XtvcOo=
gopkg.in/jcmturner/dnsutils.v1 v1.0.1/go.mod h1:m3v+5svpVOhtFAP/wSz+yzh4Mc0Fg7eRhxkJMWSIz9Q= gopkg.in/jcmturner/dnsutils.v1 v1.0.1/go.mod h1:m3v+5svpVOhtFAP/wSz+yzh4Mc0Fg7eRhxkJMWSIz9Q=

View File

@ -25,12 +25,12 @@ import (
"github.com/qiniu/goc/pkg/cover" "github.com/qiniu/goc/pkg/cover"
) )
func MvBinaryToOri(pkgs map[string]*cover.Package, newgopath string) { func (b *Build) MvBinaryToOri(pkgs map[string]*cover.Package) {
for _, pkg := range pkgs { for _, pkg := range b.Pkgs {
if pkg.Name == "main" { if pkg.Name == "main" {
_, binaryTarget := filepath.Split(pkg.Target) _, binaryTarget := filepath.Split(pkg.Target)
binaryTmpPath := filepath.Join(getTmpwd(newgopath, pkgs, !checkIfLegacyProject(pkgs)), binaryTarget) binaryTmpPath := filepath.Join(b.TmpWorkingDir)
if false == checkIfFileExist(binaryTmpPath) { if false == checkIfFileExist(binaryTmpPath) {
continue continue

40
pkg/build/build.go Normal file
View File

@ -0,0 +1,40 @@
/*
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/qiniu/goc/pkg/cover"
// Build is to describe the building/installing process of a goc build/install
type Build struct {
Pkgs map[string]*cover.Package //
NewGOPATH string
OriGOPATH string
TmpDir string
TmpWorkingDir string
IsMod bool
BuildFlags string
Packages string
Root string // Project Root
}
func NewInstall(buildflags string, packages string) *Build {
return &Build{
BuildFlags: buildflags,
Packages: packages,
}
}

View File

@ -20,13 +20,12 @@ import (
"log" "log"
"github.com/otiai10/copy" "github.com/otiai10/copy"
"github.com/qiniu/goc/pkg/cover"
) )
func cpGoModulesProject(tmpBuildDir string, pkgs map[string]*cover.Package) { func (b *Build) cpGoModulesProject() {
for _, v := range pkgs { for _, v := range b.Pkgs {
if v.Name == "main" { if v.Name == "main" {
dst := tmpBuildDir dst := b.TmpDir
src := v.Module.Dir src := v.Module.Dir
if err := copy.Copy(src, dst); err != nil { if err := copy.Copy(src, dst); err != nil {

44
pkg/build/install.go Normal file
View File

@ -0,0 +1,44 @@
/*
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 (
"fmt"
"log"
"os"
"os/exec"
)
func (b *Build) Install() {
log.Println("Go building in temp...")
cmd := exec.Command("/bin/bash", "-c", "go install " + b.BuildFlags + " " + b.Packages)
cmd.Dir = b.TmpWorkingDir
// Change the temp GOBIN, to force binary install to original place
cmd.Env = append(os.Environ(), fmt.Sprintf("GOBIN=%v", b.findWhereToInstall()))
if b.NewGOPATH != "" {
// Change to temp GOPATH for go install command
cmd.Env = append(cmd.Env, fmt.Sprintf("GOPATH=%v", b.NewGOPATH))
}
log.Printf("go install cmd is: %v", cmd.Args)
out, err := cmd.CombinedOutput()
if err != nil {
log.Fatalf("Fail to execute: %v. The error is: %v, the stdout/stderr is: %v", cmd.Args, err, string(out))
}
log.Printf("Go install successful. Binary installed in: %v", b.findWhereToInstall())
}

View File

@ -25,10 +25,10 @@ import (
"github.com/qiniu/goc/pkg/cover" "github.com/qiniu/goc/pkg/cover"
) )
func cpLegacyProject(tmpBuildDir string, pkgs map[string]*cover.Package) { func (b *Build) cpLegacyProject() {
visited := make(map[string]bool) visited := make(map[string]bool)
for k, v := range pkgs { for k, v := range b.Pkgs {
dst := filepath.Join(tmpBuildDir, "src", k) dst := filepath.Join(b.TmpDir, "src", k)
src := v.Dir src := v.Dir
if _, ok := visited[src]; ok { if _, ok := visited[src]; ok {
@ -42,13 +42,13 @@ func cpLegacyProject(tmpBuildDir string, pkgs map[string]*cover.Package) {
visited[src] = true visited[src] = true
cpDepPackages(tmpBuildDir, v, visited) b.cpDepPackages(v, visited)
} }
} }
// only cp dependency in root(current gopath), // only cp dependency in root(current gopath),
// skip deps in other GOPATHs // skip deps in other GOPATHs
func cpDepPackages(tmpBuildDir string, pkg *cover.Package, visited map[string]bool) { func (b *Build) cpDepPackages( pkg *cover.Package, visited map[string]bool) {
/* /*
oriGOPATH := os.Getenv("GOPATH") oriGOPATH := os.Getenv("GOPATH")
if oriGOPATH == "" { if oriGOPATH == "" {
@ -70,7 +70,7 @@ func cpDepPackages(tmpBuildDir string, pkg *cover.Package, visited map[string]bo
continue continue
} }
dst := filepath.Join(tmpBuildDir, "src", dep) dst := filepath.Join(b.TmpDir, "src", dep)
if err := copy.Copy(src, dst); err != nil { if err := copy.Copy(src, dst); err != nil {
log.Printf("Failed to Copy the folder from %v to %v, the error is: %v ", src, dst, err) log.Printf("Failed to Copy the folder from %v to %v, the error is: %v ", src, dst, err)

View File

@ -27,56 +27,53 @@ import (
"github.com/qiniu/goc/pkg/cover" "github.com/qiniu/goc/pkg/cover"
) )
func MvProjectsToTmp(target string, args []string) (newGopath string, newWorkingDir string, tmpBuildDir string, pkgs map[string]*cover.Package) { func (b *Build) MvProjectsToTmp() (newGopath string, newWorkingDir string, tmpBuildDir string, pkgs map[string]*cover.Package) {
listArgs := []string{"list", "-json"} listArgs := []string{"-json"}
if len(args) != 0 { if len(b.BuildFlags) != 0 {
listArgs = append(listArgs, args...) listArgs = append(listArgs, b.BuildFlags)
} }
listArgs = append(listArgs, "./...") listArgs = append(listArgs, "./...")
pkgs = cover.ListPackages(target, listArgs, "") b.Pkgs = cover.ListPackages(".", strings.Join(listArgs, " "), "")
tmpBuildDir, newWorkingDir, isMod := mvProjectsToTmp(pkgs) b.mvProjectsToTmp()
oriGopath := os.Getenv("GOPATH") oriGopath := os.Getenv("GOPATH")
if isMod == true { if b.IsMod == true {
newGopath = "" b.NewGOPATH = ""
} else if oriGopath == "" { } else if oriGopath == "" {
newGopath = tmpBuildDir b.NewGOPATH = b.TmpDir
} else { } else {
newGopath = fmt.Sprintf("%v:%v", tmpBuildDir, oriGopath) b.NewGOPATH = fmt.Sprintf("%v:%v", b.TmpDir, oriGopath)
} }
log.Printf("New GOPATH: %v", newGopath) log.Printf("New GOPATH: %v", b.NewGOPATH)
return return
} }
func mvProjectsToTmp(pkgs map[string]*cover.Package) (string, string, bool) { func (b *Build) mvProjectsToTmp() {
path, err := os.Getwd() path, err := os.Getwd()
if err != nil { if err != nil {
log.Fatalf("Cannot get current working directoy, the error is: %v", err) log.Fatalf("Cannot get current working directoy, the error is: %v", err)
} }
tmpBuildDir := filepath.Join(os.TempDir(), TmpFolderName(path)) b.TmpDir = filepath.Join(os.TempDir(), TmpFolderName(path))
// Delete previous tmp folder and its content // Delete previous tmp folder and its content
os.RemoveAll(tmpBuildDir) os.RemoveAll(b.TmpDir)
// Create a new tmp folder // Create a new tmp folder
err = os.MkdirAll(filepath.Join(tmpBuildDir, "src"), os.ModePerm) err = os.MkdirAll(filepath.Join(b.TmpDir, "src"), os.ModePerm)
if err != nil { if err != nil {
log.Fatalf("Fail to create the temporary build directory. The err is: %v", err) log.Fatalf("Fail to create the temporary build directory. The err is: %v", err)
} }
log.Printf("Temp project generated in: %v", tmpBuildDir) log.Printf("Tmp project generated in: %v", b.TmpDir)
isMod := false // set Build.IsMod flag, so we dont have to call checkIfLegacyProject another time
var tmpWorkingDir string if b.checkIfLegacyProject() {
if checkIfLegacyProject(pkgs) { b.cpLegacyProject()
cpLegacyProject(tmpBuildDir, pkgs)
tmpWorkingDir = getTmpwd(tmpBuildDir, pkgs, false)
} else { } else {
cpGoModulesProject(tmpBuildDir, pkgs) b.IsMod = true
tmpWorkingDir = getTmpwd(tmpBuildDir, pkgs, true) b.cpGoModulesProject()
isMod = true
} }
b.getTmpwd()
log.Printf("New working/building directory in: %v", tmpWorkingDir) log.Printf("New workingdir in tmp directory in: %v", b.TmpWorkingDir)
return tmpBuildDir, tmpWorkingDir, isMod
} }
func TmpFolderName(path string) string { func TmpFolderName(path string) string {
@ -86,11 +83,11 @@ func TmpFolderName(path string) string {
return "goc-" + h return "goc-" + h
} }
// Check if it is go module project // checkIfLegacyProject Check if it is go module project
// true legacy // true legacy
// false go mod // false go mod
func checkIfLegacyProject(pkgs map[string]*cover.Package) bool { func (b *Build) checkIfLegacyProject() bool {
for _, v := range pkgs { for _, v := range b.Pkgs {
if v.Module == nil { if v.Module == nil {
return true return true
} }
@ -100,8 +97,10 @@ func checkIfLegacyProject(pkgs map[string]*cover.Package) bool {
return false return false
} }
func getTmpwd(tmpBuildDir string, pkgs map[string]*cover.Package, isMod bool) string { // getTmpwd get the corresponding working directory in the temporary working directory
for _, pkg := range pkgs { // and store it in the Build.tmpWorkdingDir
func (b *Build) getTmpwd() {
for _, pkg := range b.Pkgs {
path, err := os.Getwd() path, err := os.Getwd()
if err != nil { if err != nil {
log.Fatalf("Cannot get current working directory, the error is: %v", err) log.Fatalf("Cannot get current working directory, the error is: %v", err)
@ -109,7 +108,7 @@ func getTmpwd(tmpBuildDir string, pkgs map[string]*cover.Package, isMod bool) st
index := -1 index := -1
var parentPath string var parentPath string
if isMod == false { if b.IsMod == false {
index = strings.Index(path, pkg.Root) index = strings.Index(path, pkg.Root)
parentPath = pkg.Root parentPath = pkg.Root
} else { } else {
@ -120,24 +119,24 @@ func getTmpwd(tmpBuildDir string, pkgs map[string]*cover.Package, isMod bool) st
if index == -1 { if index == -1 {
log.Fatalf("goc install not executed in project directory.") log.Fatalf("goc install not executed in project directory.")
} }
tmpwd := filepath.Join(tmpBuildDir, path[len(parentPath):]) b.TmpWorkingDir = filepath.Join(b.TmpDir, path[len(parentPath):])
// log.Printf("New building directory in: %v", tmpwd) // log.Printf("New building directory in: %v", tmpwd)
return tmpwd return
} }
log.Fatalln("Should never be reached....") log.Fatalln("Should never be reached....")
return "" return
} }
func FindWhereToInstall(pkgs map[string]*cover.Package) string { func (b *Build) findWhereToInstall() string {
if GOBIN := os.Getenv("GOBIN"); GOBIN != "" { if GOBIN := os.Getenv("GOBIN"); GOBIN != "" {
return GOBIN return GOBIN
} }
// old GOPATH dir // old GOPATH dir
GOPATH := os.Getenv("GOPATH") GOPATH := os.Getenv("GOPATH")
if true == checkIfLegacyProject(pkgs) { if false == b.IsMod {
for _, v := range pkgs { for _, v := range b.Pkgs {
return filepath.Join(v.Root, "bin") return filepath.Join(v.Root, "bin")
} }
} }

View File

@ -1,133 +0,0 @@
/*
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 (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"github.com/qiniu/goc/pkg/cover"
)
const TEST_GO_LIST_LEGACY = `{
"Dir": "/Users/lyyyuna/gitup/linking/src/qiniu.com/linking/api/linking.v1",
"ImportPath": "qiniu.com/linking/api/linking.v1",
"Name": "linking",
"Target": "/Users/lyyyuna/gitup/linking/pkg/darwin_amd64/qiniu.com/linking/api/linking.v1.a",
"Root": "/Users/lyyyuna/gitup/linking",
"Match": [
"./..."
],
"Stale": true,
"StaleReason": "stale dependency: vendor/github.com/modern-go/concurrent",
"GoFiles": [
"client.go"
],
"Imports": [
"vendor/github.com/json-iterator/go",
"github.com/qiniu/rpc.v2",
"vendor/github.com/qiniu/xlog.v1",
"vendor/qiniu.com/auth/qiniumac.v1"
],
"ImportMap": {
"github.com/json-iterator/go": "vendor/github.com/json-iterator/go",
"github.com/qiniu/xlog.v1": "vendor/github.com/qiniu/xlog.v1",
"qiniu.com/auth/qiniumac.v1": "vendor/qiniu.com/auth/qiniumac.v1"
},
"Deps": [
"bufio"
]
}`
const TEST_GO_LIST_MOD = `{
"Dir": "/Users/lyyyuna/gitup/tonghu-chat",
"ImportPath": "github.com/lyyyuna/tonghu-chat",
"Name": "main",
"Target": "/Users/lyyyuna/go/bin/tonghu-chat",
"Root": "/Users/lyyyuna/gitup/tonghu-chat",
"Module": {
"Path": "github.com/lyyyuna/tonghu-chat",
"Main": true,
"Dir": "/Users/lyyyuna/gitup/tonghu-chat",
"GoMod": "/Users/lyyyuna/gitup/tonghu-chat/go.mod",
"GoVersion": "1.14"
},
"Match": [
"./..."
],
"Stale": true,
"StaleReason": "not installed but available in build cache",
"GoFiles": [
"main.go"
],
"Imports": [
"github.com/gin-gonic/gin",
"github.com/gorilla/websocket"
],
"Deps": [
"bufio"
]
}`
func constructPkg(raw string) *cover.Package {
var pkg cover.Package
if err := json.Unmarshal([]byte(raw), &pkg); err != nil {
panic(err)
}
return &pkg
}
func TestLegacyProjectJudgement(t *testing.T) {
pkgs := make(map[string]*cover.Package)
pkg := constructPkg(TEST_GO_LIST_LEGACY)
pkgs[pkg.ImportPath] = pkg
if expect, got := true, checkIfLegacyProject(pkgs); expect != got {
t.Fatalf("Expected %v, but got %v.", expect, got)
}
}
func TestModProjectJudgement(t *testing.T) {
pkgs := make(map[string]*cover.Package)
pkg := constructPkg(TEST_GO_LIST_MOD)
pkgs[pkg.ImportPath] = pkg
if expect, got := false, checkIfLegacyProject(pkgs); expect != got {
t.Fatalf("Expected %v, but got %v.", expect, got)
}
}
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")
os.Chdir(workingDir)
fmt.Println(gopath)
os.Setenv("GOPATH", gopath)
os.Setenv("GO111MODULE", "off")
newgopath, newwd, tmpdir, _ := MvProjectsToTmp(".", nil)
if -1 == strings.Index(newwd, tmpdir) {
t.Fatalf("Directory parse error. newwd: %v, tmpdir: %v", newwd, tmpdir)
}
if -1 == strings.Index(newgopath, ":") || -1 == strings.Index(newgopath, tmpdir) {
t.Fatalf("The New GOPATH is wrong. newgopath: %v, tmpdir: %v", newgopath, tmpdir)
}
}

View File

@ -113,17 +113,18 @@ type PackageError struct {
} }
// ListPackages list all packages under specific via go list command // ListPackages list all packages under specific via go list command
func ListPackages(dir string, args []string, newgopath string) map[string]*Package { // The argument newgopath is if you need to go list in a different GOPATH
cmd := exec.Command("go", args...) func ListPackages(dir string, args string, newgopath string) map[string]*Package {
cmd := exec.Command("/bin/bash", "-c", "go list " + args)
log.Printf("go list cmd is: %v", cmd.Args) log.Printf("go list cmd is: %v", cmd.Args)
cmd.Dir = dir cmd.Dir = dir
if newgopath != "" { if newgopath != "" {
cmd.Env = append(os.Environ(), fmt.Sprintf("GOPATH=%v", newgopath)) cmd.Env = append(os.Environ(), fmt.Sprintf("GOPATH=%v", newgopath))
} }
out, _ := cmd.Output() out, err := cmd.CombinedOutput()
// if err != nil { if err != nil {
// log.Fatalf("excute `go list -json ./...` command failed, err: %v, out: %v", err, string(out)) log.Fatalf("excute `go list -json ./...` command failed, err: %v, out: %v", err, string(out))
// } }
dec := json.NewDecoder(bytes.NewReader(out)) dec := json.NewDecoder(bytes.NewReader(out))
pkgs := make(map[string]*Package, 0) pkgs := make(map[string]*Package, 0)

View File

@ -63,7 +63,7 @@ var _ = Describe("E2E", func() {
By("goc install") By("goc install")
testProjDir = filepath.Join(TESTS_ROOT, "samples/simple_project") testProjDir = filepath.Join(TESTS_ROOT, "samples/simple_project")
cmd = exec.Command("goc", "install", "./...") cmd = exec.Command("goc", "install", "--debuggoc")
cmd.Dir = testProjDir cmd.Dir = testProjDir
out, err = cmd.CombinedOutput() out, err = cmd.CombinedOutput()
@ -124,7 +124,7 @@ var _ = Describe("E2E", func() {
By("goc install") By("goc install")
testProjDir = filepath.Join(TESTS_ROOT, "samples/simple_gopath_project") testProjDir = filepath.Join(TESTS_ROOT, "samples/simple_gopath_project")
cmd = exec.Command("goc", "install", "./...") cmd = exec.Command("goc", "install", "--debuggoc")
cmd.Dir = filepath.Join(testProjDir, "src/qiniu.com/simple_gopath_project") cmd.Dir = filepath.Join(testProjDir, "src/qiniu.com/simple_gopath_project")
// use GOPATH mode to compile project // use GOPATH mode to compile project
cmd.Env = append(os.Environ(), fmt.Sprintf("GOPATH=%v", testProjDir), "GO111MODULE=off") cmd.Env = append(os.Environ(), fmt.Sprintf("GOPATH=%v", testProjDir), "GO111MODULE=off")