diff --git a/cmd/build_test.go b/cmd/build_test.go index 6e86287..fc3841c 100644 --- a/cmd/build_test.go +++ b/cmd/build_test.go @@ -58,7 +58,7 @@ func TestGeneratedBinary(t *testing.T) { 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") + assert.Equal(t, cnt > 0, true, "GoCover variable should be in the binary") } 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") 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 @@ -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") 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") } diff --git a/cmd/commonflags.go b/cmd/commonflags.go index fcd63d4..9440276 100644 --- a/cmd/commonflags.go +++ b/cmd/commonflags.go @@ -70,7 +70,7 @@ func addRunFlags(cmdset *pflag.FlagSet) { viper.BindPFlags(cmdset) } -// add Cover Mode check +// CoverMode represents the covermode when doing cover for source code type CoverMode struct { mode string } @@ -79,6 +79,7 @@ func (m *CoverMode) String() string { 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 { if v == "" { m.mode = "count" @@ -91,11 +92,12 @@ func (m *CoverMode) Set(v string) error { return nil } +// Type returns the type of CoverMode func (m *CoverMode) Type() string { return "string" } -// add agentPort check +// AgentPort is the struct to do agentPort check type AgentPort struct { port string } @@ -104,6 +106,7 @@ func (agent *AgentPort) String() string { return agent.port } +// Set sets the value to the AgentPort struct func (agent *AgentPort) Set(v string) error { if v == "" { agent.port = "" @@ -117,6 +120,7 @@ func (agent *AgentPort) Set(v string) error { return nil } +// Type returns the type of AgentPort func (agent *AgentPort) Type() string { return "string" } diff --git a/cmd/install_test.go b/cmd/install_test.go index d18a674..5b94c97 100644 --- a/cmd/install_test.go +++ b/cmd/install_test.go @@ -52,7 +52,7 @@ func TestInstalledBinaryForMod(t *testing.T) { 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") + assert.Equal(t, cnt > 0, true, "GoCover variable should be in the binary") } 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") 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") } diff --git a/pkg/build/build.go b/pkg/build/build.go index 01ad30b..28ddb28 100644 --- a/pkg/build/build.go +++ b/pkg/build/build.go @@ -79,6 +79,7 @@ func NewBuild(buildflags string, args []string, workingDir string, outputDir str return b, nil } +// Build calls 'go build' tool to do building func (b *Build) Build() error { log.Infoln("Go building in temp...") // new -o will overwrite previous ones diff --git a/pkg/build/install.go b/pkg/build/install.go index 8546e46..b5cb823 100644 --- a/pkg/build/install.go +++ b/pkg/build/install.go @@ -45,6 +45,7 @@ func NewInstall(buildflags string, args []string, workingDir string) (*Build, er return b, nil } +// Install use the 'go install' tool to install packages func (b *Build) Install() error { log.Println("Go building in temp...") cmd := exec.Command("/bin/bash", "-c", "go install "+b.BuildFlags+" "+b.Packages) diff --git a/pkg/build/tmpfolder.go b/pkg/build/tmpfolder.go index 5998ebd..bc40438 100644 --- a/pkg/build/tmpfolder.go +++ b/pkg/build/tmpfolder.go @@ -29,6 +29,7 @@ import ( "github.com/spf13/viper" ) +// MvProjectsToTmp moves the projects into a temporary directory func (b *Build) MvProjectsToTmp() error { listArgs := []string{"-json"} if len(b.BuildFlags) != 0 { @@ -67,7 +68,7 @@ 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 os.RemoveAll(b.TmpDir) @@ -110,7 +111,9 @@ func (b *Build) mvProjectsToTmp() error { 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)) h := fmt.Sprintf("%x", sum[:6]) diff --git a/pkg/cover/cover_test.go b/pkg/cover/cover_test.go index f1a0de4..9c415ef 100644 --- a/pkg/cover/cover_test.go +++ b/pkg/cover/cover_test.go @@ -364,6 +364,9 @@ func TestCoverResultForInternalPackage(t *testing.T) { } 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") assert.Equal(t, cnt > 0, true, "GoCacheCover variable should be in http_cover_apis_auto_generated.go") diff --git a/pkg/cover/server.go b/pkg/cover/server.go index 05d0efe..0d09f52 100644 --- a/pkg/cover/server.go +++ b/pkg/cover/server.go @@ -116,7 +116,7 @@ func registerService(c *gin.Context) { realIP := c.ClientIP() 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) } diff --git a/pkg/github/github.go b/pkg/github/github.go index bbefbf0..5ed7609 100644 --- a/pkg/github/github.go +++ b/pkg/github/github.go @@ -34,8 +34,11 @@ import ( "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." +// PrComment is the entry which is able to comment on Github Pull Requests type PrComment struct { RobotUserName 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) { if len(diffCovList) == 0 { 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 } +// PostComment post comment on github. It erased the old one if existed to avoid duplicate func (c *PrComment) PostComment(content, commentPrefix string) error { //step1: erase history similar comment to avoid too many comment for same job err := c.EraseHistoryComment(commentPrefix) @@ -116,7 +120,7 @@ func (c *PrComment) PostComment(content, commentPrefix string) error { return nil } -// erase history similar comment before post again +// EraseHistoryComment erase history similar comment before post again func (c *PrComment) EraseHistoryComment(commentPrefix string) error { comments, _, err := c.GithubClient.Issues.ListComments(c.Ctx, c.RepoOwner, c.RepoName, c.PrNumber, nil) if err != nil { @@ -161,7 +165,7 @@ func (c *PrComment) GetPrChangedFiles() (files []string, err error) { 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 { var buf bytes.Buffer table := tablewriter.NewWriter(&buf) diff --git a/pkg/qiniu/mock.go b/pkg/qiniu/mock.go index 59e56ad..d60c7bc 100644 --- a/pkg/qiniu/mock.go +++ b/pkg/qiniu/mock.go @@ -26,6 +26,7 @@ import ( "github.com/sirupsen/logrus" ) +// MockQiniuServer simulate qiniu cloud for testing 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 = httprouter.New() @@ -54,7 +55,7 @@ func MockRouterAPI(router *httprouter.Router, profile string, count int) { logrus.Infof("request url is: %s", r.URL.String()) if timeout > 0 { - timeout -= 1 + timeout-- http.Error(w, "not found", http.StatusNotFound) return } diff --git a/pkg/qiniu/object.go b/pkg/qiniu/object.go index e374c60..3f10b37 100644 --- a/pkg/qiniu/object.go +++ b/pkg/qiniu/object.go @@ -38,6 +38,7 @@ type ObjectHandle struct { client *client.Client } +// Attrs get the object's metainfo func (o *ObjectHandle) Attrs(ctx context.Context) (storage.FileInfo, error) { //TODO(CarlJi): need retry when errors return o.bm.Stat(o.cfg.Bucket, o.key)