Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go cover不能生成可编译的打桩代码 #352

Open
LeonSu070 opened this issue Oct 4, 2023 · 4 comments
Open

go cover不能生成可编译的打桩代码 #352

LeonSu070 opened this issue Oct 4, 2023 · 4 comments
Labels
enhancement New feature or request

Comments

@LeonSu070
Copy link

LeonSu070 commented Oct 4, 2023

执行goc cover生成打桩代码

执行命令:

goc cover

生成了cover.go和http_cover_apis_auto_generated.go

.
├── cover.go
├── go.mod
├── http_cover_apis_auto_generated.go
└── main.go

运行打好桩的代码报错

go run .
cover.go:1:9: expected 'IDENT', found '.'
http_cover_apis_auto_generated.go:24:2: .: cannot import current directory

生成的代码:
cover.go

package .



var GoCover_0_623462353261316139306530 = struct {
	Count     [29]uint32
	Pos       [3 * 29]uint32
	NumStmt   [29]uint16
} {
	Pos: [3 * 29]uint32{
		30, 48, 0x21000d, // [0]
		52, 65, 0xc0002, // [1]
		80, 82, 0x4f0002, // [2]
		86, 87, 0x220002, // [3]
		48, 50, 0x30021, // [4]
		65, 74, 0x2e000c, // [5]
		77, 77, 0xe0003, // [6]
		74, 76, 0x4002e, // [7]
		82, 84, 0x3004f, // [8]
		90, 91, 0x47001b, // [9]
		91, 92, 0x180047, // [10]
		96, 99, 0x230003, // [11]
		92, 95, 0x40018, // [12]
		103, 104, 0x47001d, // [13]
		104, 105, 0x260047, // [14]
		109, 109, 0x2f0003, // [15]
		105, 108, 0x40026, // [16]
		113, 114, 0x2e0042, // [17]
		114, 115, 0x48002e, // [18]
		115, 116, 0x110048, // [19]
		123, 123, 0x180004, // [20]
		116, 118, 0xc0011, // [21]
		121, 121, 0x510005, // [22]
		118, 120, 0x6000c, // [23]
		128, 129, 0x2e004b, // [24]
		129, 130, 0x48002e, // [25]
		130, 132, 0x170048, // [26]
		135, 137, 0x290004, // [27]
		132, 134, 0x50017, // [28]
	},
	NumStmt: [29]uint16{
		13, // 0
		5, // 1
		3, // 2
		2, // 3
		1, // 4
		7, // 5
		1, // 6
		1, // 7
		1, // 8
		1, // 9
		1, // 10
		4, // 11
		2, // 12
		1, // 13
		1, // 14
		1, // 15
		2, // 16
		1, // 17
		1, // 18
		1, // 19
		1, // 20
		2, // 21
		1, // 22
		1, // 23
		1, // 24
		1, // 25
		2, // 26
		3, // 27
		1, // 28
	},
}

http_cover_apis_auto_generated.go

// Code generated by goc system. DO NOT EDIT.

package main

import (
	"bufio"
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"
	_log "log"
	"net"
	"net/http"
	"os"
	"os/signal"
	"path/filepath"
	"strings"
	"sync/atomic"
	"syscall"
	"testing"

	_cover "."

)

func init() {
	go registerHandlersGoc()
}

func loadValuesGoc() (map[string][]uint32, map[string][]testing.CoverBlock) {
	var (
		coverCounters = make(map[string][]uint32)
		coverBlocks   = make(map[string][]testing.CoverBlock)
	)

...

main.go

//line /Users/leonsulicun/LeonDisk/chopeGit/test/simple-go-server/main.go:1
package main; import . "."

import (
	"context"
	"flag"
	"fmt"
	"log"
	"net/http"
	"os"
	"os/signal"
	"sync/atomic"
	"time"
)

type key int

const (
	requestIDKey key = 0
)

var (
	Version      string = ""
	GitTag       string = ""
	GitCommit    string = ""
	GitTreeState string = ""
	listenAddr   string
	healthy      int32
)

func main() {GoCover_0_623462353261316139306530.Count[0]++;
	flag.StringVar(&listenAddr, "listen-addr", ":5001", "server listen address")
	flag.Parse()
...

go.mod

module simple-server

go 1.20
@LeonSu070 LeonSu070 changed the title go cover --target=./target不能正常生成打桩代码 go cover不能正常生成打桩代码 Oct 4, 2023
@LeonSu070 LeonSu070 changed the title go cover不能正常生成打桩代码 go cover不能生成可编译的打桩代码 Oct 4, 2023
@CarlJi
Copy link
Collaborator

CarlJi commented Oct 6, 2023

hi @LeonSu070 , The goc cover function was initially created as an internal tool to aid in testing during the early stages of development. However, it is now considered deprecated and should not be utilized.

For experiencing the desired feature, I recommend using the goc build command. This will provide a suitable alternative to meet your testing requirements.

@LeonSu070
Copy link
Author

LeonSu070 commented Oct 9, 2023

Hi @CarlJi , Thanks for your response!

Actually, we are running our service on AWS Lambda. That means we use AWS sam build to build our service. That's why i need to run goc cover to generate the source code first, then run sam build to deploy our service to Lambda.

I changed the generated source code manually like the following steps, it can be built by sam build. If you can update goc cover to reduce the manual work that would be great.

  1. move cover.go to a folder cover, and rename the package name from "." to "cover"
  2. import "simple-server/cover" in main.go and http_cover_apis_auto_generated.go
  3. change the counter var from GoCover_0_xxxxxxxxxxxxxxxxxxxx to cover.GoCover_0_xxxxxxxxxxxxxxxxxxxx in main.go
//line /Users/leonsulicun/LeonDisk/chopeGit/test/simple-go-server/main.go:1
package main; 

import (
	"context"
	"flag"
	"fmt"
	"log"
	"net/http"
	"os"
	"os/signal"
	"sync/atomic"
	"time"
	"simple-server/cover"
)

type key int

const (
	requestIDKey key = 0
)

var (
	Version      string = ""
	GitTag       string = ""
	GitCommit    string = ""
	GitTreeState string = ""
	listenAddr   string
	healthy      int32
)

func main() {cover.GoCover_0_623462353261316139306530.Count[0]++;
	flag.StringVar(&listenAddr, "listen-addr", ":5001", "server listen address")
	flag.Parse()

	logger := log.New(os.Stdout, "http: ", log.LstdFlags)

	logger.Println("Simple go server")
	logger.Println("Version:", Version)
	logger.Println("GitTag:", GitTag)
	logger.Println("GitCommit:", GitCommit)
	logger.Println("GitTreeState:", GitTreeState)

	logger.Println("Server is starting...")

	router := http.NewServeMux()
	router.Handle("/", index())
	router.Handle("/healthz", healthz())

	nextRequestID := func() string {cover.GoCover_0_623462353261316139306530.Count[4]++;
		return fmt.Sprintf("%d", time.Now().UnixNano())
	}

	cover.GoCover_0_623462353261316139306530.Count[1]++;server := &http.Server{
		Addr:         listenAddr,
		Handler:      tracing(nextRequestID)(logging(logger)(router)),
		ErrorLog:     logger,
		ReadTimeout:  5 * time.Second,
		WriteTimeout: 10 * time.Second,
		IdleTimeout:  15 * time.Second,
	}
...

@CarlJi
Copy link
Collaborator

CarlJi commented Oct 10, 2023

@LeonSu070 Certainly, let's add it to the backlog. Thank you!

@CarlJi CarlJi added enhancement New feature or request and removed need-more-information labels Oct 10, 2023
@LeonSu070
Copy link
Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants