fix golint error

This commit is contained in:
jichangjun 2020-07-26 17:03:47 +08:00
parent 550cc83b85
commit 249a931f6c
11 changed files with 32 additions and 14 deletions

View File

@ -58,7 +58,7 @@ func TestGeneratedBinary(t *testing.T) {
assert.Equal(t, cnt > 0, true, "main.registerSelf function should be in the binary") assert.Equal(t, cnt > 0, true, "main.registerSelf function should be in the binary")
cnt = strings.Count(string(out), "GoCover") cnt = strings.Count(string(out), "GoCover")
assert.Equal(t, cnt > 0, true, "GoCover varibale should be in the binary") assert.Equal(t, cnt > 0, true, "GoCover variable should be in the binary")
} }
func TestBuildBinaryName(t *testing.T) { func TestBuildBinaryName(t *testing.T) {
@ -86,7 +86,7 @@ func TestBuildBinaryName(t *testing.T) {
assert.Equal(t, cnt > 0, true, "main.registerSelf function should be in the binary") assert.Equal(t, cnt > 0, true, "main.registerSelf function should be in the binary")
cnt = strings.Count(string(out), "GoCover") cnt = strings.Count(string(out), "GoCover")
assert.Equal(t, cnt > 0, true, "GoCover varibale should be in the binary") assert.Equal(t, cnt > 0, true, "GoCover variable should be in the binary")
} }
// test if goc can get variables in internal package // test if goc can get variables in internal package
@ -115,5 +115,5 @@ func TestBuildBinaryForInternalPackage(t *testing.T) {
assert.Equal(t, cnt > 0, true, "GoCacheCover variable for internal package should be in the binary") assert.Equal(t, cnt > 0, true, "GoCacheCover variable for internal package should be in the binary")
cnt = strings.Count(string(out), "internal.GoCover") cnt = strings.Count(string(out), "internal.GoCover")
assert.Equal(t, cnt > 0, true, "internal.GoCover varibale should be in the binary") assert.Equal(t, cnt > 0, true, "internal.GoCover variable should be in the binary")
} }

View File

@ -70,7 +70,7 @@ func addRunFlags(cmdset *pflag.FlagSet) {
viper.BindPFlags(cmdset) viper.BindPFlags(cmdset)
} }
// add Cover Mode check // CoverMode represents the covermode when doing cover for source code
type CoverMode struct { type CoverMode struct {
mode string mode string
} }
@ -79,6 +79,7 @@ func (m *CoverMode) String() string {
return m.mode return m.mode
} }
// Set sets the value to the CoverMode struct, use 'count' as default if v is empty
func (m *CoverMode) Set(v string) error { func (m *CoverMode) Set(v string) error {
if v == "" { if v == "" {
m.mode = "count" m.mode = "count"
@ -91,11 +92,12 @@ func (m *CoverMode) Set(v string) error {
return nil return nil
} }
// Type returns the type of CoverMode
func (m *CoverMode) Type() string { func (m *CoverMode) Type() string {
return "string" return "string"
} }
// add agentPort check // AgentPort is the struct to do agentPort check
type AgentPort struct { type AgentPort struct {
port string port string
} }
@ -104,6 +106,7 @@ func (agent *AgentPort) String() string {
return agent.port return agent.port
} }
// Set sets the value to the AgentPort struct
func (agent *AgentPort) Set(v string) error { func (agent *AgentPort) Set(v string) error {
if v == "" { if v == "" {
agent.port = "" agent.port = ""
@ -117,6 +120,7 @@ func (agent *AgentPort) Set(v string) error {
return nil return nil
} }
// Type returns the type of AgentPort
func (agent *AgentPort) Type() string { func (agent *AgentPort) Type() string {
return "string" return "string"
} }

View File

@ -52,7 +52,7 @@ func TestInstalledBinaryForMod(t *testing.T) {
assert.Equal(t, cnt > 0, true, "main.registerSelf function should be in the binary") assert.Equal(t, cnt > 0, true, "main.registerSelf function should be in the binary")
cnt = strings.Count(string(out), "GoCover") cnt = strings.Count(string(out), "GoCover")
assert.Equal(t, cnt > 0, true, "GoCover varibale should be in the binary") assert.Equal(t, cnt > 0, true, "GoCover variable should be in the binary")
} }
func TestInstalledBinaryForLegacy(t *testing.T) { func TestInstalledBinaryForLegacy(t *testing.T) {
@ -80,5 +80,5 @@ func TestInstalledBinaryForLegacy(t *testing.T) {
assert.Equal(t, cnt > 0, true, "main.registerSelf function should be in the binary") assert.Equal(t, cnt > 0, true, "main.registerSelf function should be in the binary")
cnt = strings.Count(string(out), "GoCover") cnt = strings.Count(string(out), "GoCover")
assert.Equal(t, cnt > 0, true, "GoCover varibale should be in the binary") assert.Equal(t, cnt > 0, true, "GoCover variable should be in the binary")
} }

View File

@ -79,6 +79,7 @@ func NewBuild(buildflags string, args []string, workingDir string, outputDir str
return b, nil return b, nil
} }
// Build calls 'go build' tool to do building
func (b *Build) Build() error { func (b *Build) Build() error {
log.Infoln("Go building in temp...") log.Infoln("Go building in temp...")
// new -o will overwrite previous ones // new -o will overwrite previous ones

View File

@ -45,6 +45,7 @@ func NewInstall(buildflags string, args []string, workingDir string) (*Build, er
return b, nil return b, nil
} }
// Install use the 'go install' tool to install packages
func (b *Build) Install() error { func (b *Build) Install() error {
log.Println("Go building in temp...") log.Println("Go building in temp...")
cmd := exec.Command("/bin/bash", "-c", "go install "+b.BuildFlags+" "+b.Packages) cmd := exec.Command("/bin/bash", "-c", "go install "+b.BuildFlags+" "+b.Packages)

View File

@ -29,6 +29,7 @@ import (
"github.com/spf13/viper" "github.com/spf13/viper"
) )
// MvProjectsToTmp moves the projects into a temporary directory
func (b *Build) MvProjectsToTmp() error { func (b *Build) MvProjectsToTmp() error {
listArgs := []string{"-json"} listArgs := []string{"-json"}
if len(b.BuildFlags) != 0 { if len(b.BuildFlags) != 0 {
@ -67,7 +68,7 @@ func (b *Build) MvProjectsToTmp() error {
} }
func (b *Build) mvProjectsToTmp() error { func (b *Build) mvProjectsToTmp() error {
b.TmpDir = filepath.Join(os.TempDir(), TmpFolderName(b.WorkingDir)) b.TmpDir = filepath.Join(os.TempDir(), tmpFolderName(b.WorkingDir))
// Delete previous tmp folder and its content // Delete previous tmp folder and its content
os.RemoveAll(b.TmpDir) os.RemoveAll(b.TmpDir)
@ -110,7 +111,9 @@ func (b *Build) mvProjectsToTmp() error {
return nil return nil
} }
func TmpFolderName(path string) string { // tmpFolderName uses the first six characters of the input path's SHA256 checksum
// as the suffix.
func tmpFolderName(path string) string {
sum := sha256.Sum256([]byte(path)) sum := sha256.Sum256([]byte(path))
h := fmt.Sprintf("%x", sum[:6]) h := fmt.Sprintf("%x", sum[:6])

View File

@ -364,6 +364,9 @@ func TestCoverResultForInternalPackage(t *testing.T) {
} }
out, err := ioutil.ReadFile(filepath.Join(testDir, "http_cover_apis_auto_generated.go")) out, err := ioutil.ReadFile(filepath.Join(testDir, "http_cover_apis_auto_generated.go"))
if err != nil {
assert.FailNow(t, "failed to read http_cover_apis_auto_generated.go file")
}
cnt := strings.Count(string(out), "GoCacheCover") cnt := strings.Count(string(out), "GoCacheCover")
assert.Equal(t, cnt > 0, true, "GoCacheCover variable should be in http_cover_apis_auto_generated.go") assert.Equal(t, cnt > 0, true, "GoCacheCover variable should be in http_cover_apis_auto_generated.go")

View File

@ -116,7 +116,7 @@ func registerService(c *gin.Context) {
realIP := c.ClientIP() realIP := c.ClientIP()
if host != realIP { if host != realIP {
log.Printf("the registed host %s of service %s is different with the real one %s, here we choose the real one", service.Name, host, realIP) log.Printf("the registered host %s of service %s is different with the real one %s, here we choose the real one", service.Name, host, realIP)
service.Address = fmt.Sprintf("http://%s:%s", realIP, port) service.Address = fmt.Sprintf("http://%s:%s", realIP, port)
} }

View File

@ -34,8 +34,11 @@ import (
"github.com/qiniu/goc/pkg/cover" "github.com/qiniu/goc/pkg/cover"
) )
// CommentsPrefix is the prefix when commenting on Github Pull Requests
// It is also the flag when checking whether the target comment exists or not to avoid duplicate
const CommentsPrefix = "The following is the coverage report on the affected files." const CommentsPrefix = "The following is the coverage report on the affected files."
// PrComment is the entry which is able to comment on Github Pull Requests
type PrComment struct { type PrComment struct {
RobotUserName string RobotUserName string
RepoOwner string RepoOwner string
@ -81,7 +84,7 @@ func NewPrClient(githubTokenPath, repoOwner, repoName, prNumStr, botUserName, co
} }
} }
//post github comment of diff coverage // CreateGithubComment post github comment of diff coverage
func (c *PrComment) CreateGithubComment(commentPrefix string, diffCovList cover.DeltaCovList) (err error) { func (c *PrComment) CreateGithubComment(commentPrefix string, diffCovList cover.DeltaCovList) (err error) {
if len(diffCovList) == 0 { if len(diffCovList) == 0 {
logrus.Printf("Detect 0 files coverage diff, will not comment to github.") logrus.Printf("Detect 0 files coverage diff, will not comment to github.")
@ -97,6 +100,7 @@ func (c *PrComment) CreateGithubComment(commentPrefix string, diffCovList cover.
return return
} }
// PostComment post comment on github. It erased the old one if existed to avoid duplicate
func (c *PrComment) PostComment(content, commentPrefix string) error { func (c *PrComment) PostComment(content, commentPrefix string) error {
//step1: erase history similar comment to avoid too many comment for same job //step1: erase history similar comment to avoid too many comment for same job
err := c.EraseHistoryComment(commentPrefix) err := c.EraseHistoryComment(commentPrefix)
@ -116,7 +120,7 @@ func (c *PrComment) PostComment(content, commentPrefix string) error {
return nil return nil
} }
// erase history similar comment before post again // EraseHistoryComment erase history similar comment before post again
func (c *PrComment) EraseHistoryComment(commentPrefix string) error { func (c *PrComment) EraseHistoryComment(commentPrefix string) error {
comments, _, err := c.GithubClient.Issues.ListComments(c.Ctx, c.RepoOwner, c.RepoName, c.PrNumber, nil) comments, _, err := c.GithubClient.Issues.ListComments(c.Ctx, c.RepoOwner, c.RepoName, c.PrNumber, nil)
if err != nil { if err != nil {
@ -161,7 +165,7 @@ func (c *PrComment) GetPrChangedFiles() (files []string, err error) {
return return
} }
//generate github comment content based on diff coverage and commentFlag // GenCommentContent generate github comment content based on diff coverage and commentFlag
func GenCommentContent(commentPrefix string, delta cover.DeltaCovList) string { func GenCommentContent(commentPrefix string, delta cover.DeltaCovList) string {
var buf bytes.Buffer var buf bytes.Buffer
table := tablewriter.NewWriter(&buf) table := tablewriter.NewWriter(&buf)

View File

@ -26,6 +26,7 @@ import (
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
// MockQiniuServer simulate qiniu cloud for testing
func MockQiniuServer(config *Config) (client *Client, router *httprouter.Router, serverURL string, teardown func()) { func MockQiniuServer(config *Config) (client *Client, router *httprouter.Router, serverURL string, teardown func()) {
// router is the HTTP request multiplexer used with the test server. // router is the HTTP request multiplexer used with the test server.
router = httprouter.New() router = httprouter.New()
@ -54,7 +55,7 @@ func MockRouterAPI(router *httprouter.Router, profile string, count int) {
logrus.Infof("request url is: %s", r.URL.String()) logrus.Infof("request url is: %s", r.URL.String())
if timeout > 0 { if timeout > 0 {
timeout -= 1 timeout--
http.Error(w, "not found", http.StatusNotFound) http.Error(w, "not found", http.StatusNotFound)
return return
} }

View File

@ -38,6 +38,7 @@ type ObjectHandle struct {
client *client.Client client *client.Client
} }
// Attrs get the object's metainfo
func (o *ObjectHandle) Attrs(ctx context.Context) (storage.FileInfo, error) { func (o *ObjectHandle) Attrs(ctx context.Context) (storage.FileInfo, error) {
//TODO(CarlJi): need retry when errors //TODO(CarlJi): need retry when errors
return o.bm.Stat(o.cfg.Bucket, o.key) return o.bm.Stat(o.cfg.Bucket, o.key)