Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func runBuild(args []string, wd string) {
Mode: coverMode.String(),
AgentPort: agentPort.String(),
Center: center,
Singleton: singleton,
IsMod: gocBuild.IsMod,
ModRootPath: gocBuild.ModRootPath,
OneMainPackage: true, // it is a go build
Expand Down
2 changes: 2 additions & 0 deletions cmd/commonflags.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var (
debugGoc bool
debugInCISyncFile string
buildFlags string
singleton bool

goRunExecFlag string
goRunArguments string
Expand All @@ -51,6 +52,7 @@ func addCommonFlags(cmdset *pflag.FlagSet) {
addBasicFlags(cmdset)
cmdset.Var(&coverMode, "mode", "coverage mode: set, count, atomic")
cmdset.Var(&agentPort, "agentport", "a fixed port such as :8100 for registered service communicate with goc server. if not provided, using a random one")
cmdset.BoolVar(&singleton, "singleton", false, "singleton mode, not register to goc center")
cmdset.StringVar(&buildFlags, "buildflags", "", "specify the build flags")
// bind to viper
viper.BindPFlags(cmdset)
Expand Down
29 changes: 16 additions & 13 deletions cmd/cover.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,25 @@ goc cover --center=http://127.0.0.1:7777 --target=/path/to/target --mode=atomic
`,
Hidden: true,
Run: func(cmd *cobra.Command, args []string) {
var buildFlags string
buildFlags = viper.GetString("buildflags")

ci := &cover.CoverInfo{
Args: buildFlags,
GoPath: "",
Target: target,
Mode: coverMode.String(),
AgentPort: agentPort.String(),
Center: center,
OneMainPackage: false,
}
_ = cover.Execute(ci)
runCover(target)
},
}

func runCover(target string) {
buildFlags := viper.GetString("buildflags")
ci := &cover.CoverInfo{
Args: buildFlags,
GoPath: "",
Target: target,
Mode: coverMode.String(),
AgentPort: agentPort.String(),
Center: center,
Singleton: singleton,
OneMainPackage: false,
}
_ = cover.Execute(ci)
}

func init() {
coverCmd.Flags().StringVar(&target, "target", ".", "target folder to cover")
addCommonFlags(coverCmd.Flags())
Expand Down
54 changes: 54 additions & 0 deletions cmd/cover_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package cmd

import (
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
)

var mainContent = []byte(`package main

import (
"fmt"
)

func main() {
fmt.Println("hello world")
}`)

var goModContent = []byte(`module example.com/simple-project

go 1.11
`)

func TestCoverSuccess(t *testing.T) {
workingDir := filepath.Join(baseDir, "../tests/samples/tmp/project")
err := os.MkdirAll(workingDir, os.ModePerm)
assert.NoError(t, err)
defer os.RemoveAll(workingDir)

err = writeFile(workingDir+"/main.go", mainContent)
assert.NoError(t, err)
err = writeFile(workingDir+"/go.mod", goModContent)
assert.NoError(t, err)
os.Setenv("GO111MODULE", "on")

runCover(workingDir)

_, err = os.Lstat(workingDir + "/http_cover_apis_auto_generated.go")
assert.Equal(t, err, nil, "the generate file should be generated.")
}

func writeFile(name string, data []byte) error {
f, err := os.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
_, err = f.Write(data)
if err1 := f.Close(); err1 != nil && err == nil {
err = err1
}
return err
}
1 change: 1 addition & 0 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func runInstall(args []string, wd string) {
Mode: coverMode.String(),
AgentPort: agentPort.String(),
Center: center,
Singleton: singleton,
IsMod: gocBuild.IsMod,
ModRootPath: gocBuild.ModRootPath,
OneMainPackage: false,
Expand Down
1 change: 1 addition & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ goc run . [--buildflags] [--exec] [--arguments]
Target: gocBuild.TmpDir,
Mode: coverMode.String(),
Center: gocServer,
Singleton: singleton,
AgentPort: "",
IsMod: gocBuild.IsMod,
ModRootPath: gocBuild.ModRootPath,
Expand Down
4 changes: 4 additions & 0 deletions pkg/cover/cover.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type TestCover struct {
Mode string
AgentPort string
Center string // cover profile host center
Singleton bool
MainPkgCover *PackageCover
DepsCover []*PackageCover
CacheCover map[string]*PackageCover
Expand Down Expand Up @@ -139,6 +140,7 @@ type CoverInfo struct {
Mode string
AgentPort string
Center string
Singleton bool
}

//Execute inject cover variables for all the .go files in the target folder
Expand All @@ -150,6 +152,7 @@ func Execute(coverInfo *CoverInfo) error {
mode := coverInfo.Mode
agentPort := coverInfo.AgentPort
center := coverInfo.Center
singleton := coverInfo.Singleton
globalCoverVarImportPath := coverInfo.GlobalCoverVarImportPath

if coverInfo.IsMod {
Expand Down Expand Up @@ -187,6 +190,7 @@ func Execute(coverInfo *CoverInfo) error {
Mode: mode,
AgentPort: agentPort,
Center: center,
Singleton: singleton,
MainPkgCover: mainCover,
GlobalCoverVarImportPath: globalCoverVarImportPath,
}
Expand Down
7 changes: 6 additions & 1 deletion pkg/cover/instrument.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,15 @@ func clearFileCover(counter []uint32) {
}

func registerHandlers() {
{{if .Singleton}}
ln, _, err := listen()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The purpose of this statement seems to avoid the scenario that in Singleton mode, the host variable is defined, but not used, right?

if yes, i prefer to print extra information to enhance it, like:

        ln, host, err := listen()
	if err != nil {
		log.Fatalf("listen failed, err:%v", err)
	}
        profileAddr := "http://" + host
        log.Debugf("[goc] goc profile address: %s \n", profileAddr)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good practice, i'll make change

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems this comment was not resolved, what's happening?

Copy link
Contributor Author

@sword-jin sword-jin Jun 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

log output will impact e2e test that assert gocc output @CarlJi

{{else}}
ln, host, err := listen()
{{end}}
if err != nil {
log.Fatalf("listen failed, err:%v", err)
}

{{if not .Singleton}}
profileAddr := "http://" + host
if resp, err := registerSelf(profileAddr); err != nil {
log.Fatalf("register address %v failed, err: %v, response: %v", profileAddr, err, string(resp))
Expand All @@ -157,6 +161,7 @@ func registerHandlers() {
deregisterSelf(profileAddrs)
}
go watchSignal(fn)
{{end}}

mux := http.NewServeMux()
// Coverage reports the current code coverage as a fraction in the range [0, 1].
Expand Down
15 changes: 14 additions & 1 deletion tests/build.bats
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,17 @@ setup() {
[ "$status" -eq 0 ]

wait $profile_pid
}
}

@test "test basic goc build command with singleton" {
cd samples/run_for_several_seconds

wait_profile_backend "build7" &
profile_pid=$!

run gocc build --debug --singleton --debugcisyncfile ci-sync.bak;
info build7 output: $output
[ "$status" -eq 0 ]

wait $profile_pid
}