Skip to content

Commit

Permalink
fix map operation error and find the Race condition #1
Browse files Browse the repository at this point in the history
  • Loading branch information
wgliang committed May 20, 2017
1 parent a5af075 commit 20f8b5d
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 30 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

A Golang tool that does static analysis, unit testing, code review and generate code quality report. This is a tool that concurrently runs a whole bunch of those linters and normalises their output to a report:

# Branch features

<!-- MarkdownTOC -->

- [Supported linters](#supported-linters)
Expand Down
4 changes: 4 additions & 0 deletions engine/config.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package engine

import "sync"

// Error contains the line number and the reason for
// an error output from a command
type Error struct {
Expand Down Expand Up @@ -34,4 +36,6 @@ type Reporter struct {
Metrics map[string]Metric `json:"metrics"`
Issues int `json:"issues"`
TimeStamp string `json:"time_stamp"`

syncRW *sync.RWMutex
}
34 changes: 24 additions & 10 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func (w *WaitGroupWrapper) Wrap(cb func()) {
func NewReporter(templateHtml string) *Reporter {
return &Reporter{
Metrics: make(map[string]Metric, 0),
syncRW: new(sync.RWMutex),
}
}

Expand All @@ -50,7 +51,9 @@ func (r *Reporter) Engine(projectPath string, exceptPackages string) {
if err != nil {
glog.Errorln(err)
}
r.syncRW.Lock()
r.Project = PackageAbsPath(projectPath)
r.syncRW.Unlock()

// linterFunction:unitTestF,Run all valid TEST in your golang package.And will measure
// from both coverage and time-consuming
Expand Down Expand Up @@ -126,9 +129,15 @@ func (r *Reporter) Engine(projectPath string, exceptPackages string) {
packagesTestDetail.mux.Lock()
metricUnitTest.Summaries = packagesTestDetail.Values
packagesTestDetail.mux.Unlock()
metricUnitTest.Percentage = sumCover / float64(countCover)
if countCover == 0 {
metricUnitTest.Percentage = 0
} else {
metricUnitTest.Percentage = sumCover / float64(countCover)
}

r.syncRW.Lock()
r.Metrics["UnitTestTips"] = metricUnitTest
r.syncRW.Unlock()
glog.Infoln("unit test over!")
}
// All directory that has .go files will be add into.
Expand Down Expand Up @@ -181,9 +190,10 @@ func (r *Reporter) Engine(projectPath string, exceptPackages string) {

metricCyclo.Summaries = summaries
metricCyclo.Percentage = countPercentage(compBigThan15 + int(sumAverageCyclo/float64(len(dirsAll))) - 1)

r.syncRW.Lock()
r.Issues = r.Issues + len(summaries)
r.Metrics["CycloTips"] = metricCyclo
r.syncRW.Unlock()
glog.Infoln("comput cyclo done!")
}
// linterfunction:simpleCodeF,all golang code hints that can be optimized
Expand All @@ -195,7 +205,6 @@ func (r *Reporter) Engine(projectPath string, exceptPackages string) {
Name: "Simple",
Description: "All golang code hints that can be optimized and give suggestions for changes.",
Weight: 0.1,
Summaries: make(map[string]Summary, 0),
}
summaries := make(map[string]Summary, 0)

Expand Down Expand Up @@ -226,9 +235,10 @@ func (r *Reporter) Engine(projectPath string, exceptPackages string) {
}
metricSimple.Summaries = summaries
metricSimple.Percentage = countPercentage(len(summaries))

r.syncRW.Lock()
r.Issues = r.Issues + len(summaries)
r.Metrics["SimpleTips"] = metricSimple
r.syncRW.Unlock()
glog.Infoln("simple code done!")
}

Expand Down Expand Up @@ -270,9 +280,10 @@ func (r *Reporter) Engine(projectPath string, exceptPackages string) {
}
metricCopyCode.Summaries = summaries
metricCopyCode.Percentage = countPercentage(len(summaries))

r.syncRW.Lock()
r.Issues = r.Issues + len(summaries)
r.Metrics["CopyCodeTips"] = metricCopyCode
r.syncRW.Unlock()
glog.Infoln("checked copy code!")
}
// linterFunction:deadCodeF,all useless code, or never obsolete obsolete code.
Expand All @@ -283,7 +294,6 @@ func (r *Reporter) Engine(projectPath string, exceptPackages string) {
Name: "DeadCode",
Description: "All useless code, or never obsolete obsolete code.",
Weight: 0.1,
Summaries: make(map[string]Summary, 0),
}
summaries := make(map[string]Summary, 0)

Expand Down Expand Up @@ -313,9 +323,10 @@ func (r *Reporter) Engine(projectPath string, exceptPackages string) {
}
metricDeadCode.Summaries = summaries
metricDeadCode.Percentage = countPercentage(len(summaries))

r.syncRW.Lock()
r.Issues = r.Issues + len(summaries)
r.Metrics["DeadCodeTips"] = metricDeadCode
r.syncRW.Unlock()
glog.Infoln("check dead code done.")
}
// linterFunction:spellCheckF,check the project variables, functions,
Expand All @@ -327,7 +338,6 @@ func (r *Reporter) Engine(projectPath string, exceptPackages string) {
Name: "SpellCheck",
Description: "Check the project variables, functions, etc. naming spelling is wrong.",
Weight: 0.1,
Summaries: make(map[string]Summary, 0),
}
summaries := make(map[string]Summary, 0)

Expand Down Expand Up @@ -358,9 +368,10 @@ func (r *Reporter) Engine(projectPath string, exceptPackages string) {
}
metricSpellTips.Summaries = summaries
metricSpellTips.Percentage = countPercentage(len(summaries))

r.syncRW.Lock()
r.Issues = r.Issues + len(summaries)
r.Metrics["SpellCheckTips"] = metricSpellTips
r.syncRW.Unlock()
glog.Infoln("checked spell error")
}
// linterFunction:dependGraphF,The project contains all the package lists.
Expand All @@ -379,7 +390,9 @@ func (r *Reporter) Engine(projectPath string, exceptPackages string) {
}
metricImportPackageTips.Summaries = summaries
metricImportPackageTips.Percentage = countPercentage(len(summaries))
r.syncRW.Lock()
r.Metrics["ImportPackagesTips"] = metricImportPackageTips
r.syncRW.Unlock()
glog.Infoln("import packages done.")
}

Expand All @@ -391,7 +404,6 @@ func (r *Reporter) Engine(projectPath string, exceptPackages string) {
Name: "DependGraph",
Description: "The dependency graph for all packages in the project helps you optimize the project architecture.",
Weight: 0,
Summaries: make(map[string]Summary, 0),
}
summaries := make(map[string]Summary, 0)

Expand All @@ -402,8 +414,10 @@ func (r *Reporter) Engine(projectPath string, exceptPackages string) {
}
metricDependGraphTips.Summaries = summaries
metricDependGraphTips.Percentage = countPercentage(len(summaries))
r.syncRW.Lock()
r.Issues = r.Issues + len(summaries)
r.Metrics["DependGraphTips"] = metricDependGraphTips
r.syncRW.Unlock()
glog.Infoln("created depend graph")
}
r.TimeStamp = time.Now().Format("2006-01-02 15:04:05")
Expand Down
14 changes: 8 additions & 6 deletions linters/copycheck/copycheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"bufio"
"flag"
"io/ioutil"
"log"
"os"
"path/filepath"
"sort"
"strings"

"github.com/golang/glog"
"github.com/wgliang/goreporter/linters/copycheck/job"
"github.com/wgliang/goreporter/linters/copycheck/output"
"github.com/wgliang/goreporter/linters/copycheck/syntax"
Expand Down Expand Up @@ -38,14 +38,15 @@ const (
vendorDirInPath = string(filepath.Separator) + "vendor" + string(filepath.Separator)
)

func CopyCheck(projectPath string, expect string) [][]string {
func CopyCheck(projectPath string, expect string) (result [][]string) {
flag.Parse()
if html && plumbing {
log.Fatal("you can have either plumbing or HTML output")
glog.Errorln("you can have either plumbing or HTML output")
return result
}
paths := []string{projectPath}
if verbose {
log.Println("Building suffix tree")
glog.Errorln("Building suffix tree")
}
schan := job.Parse(filesFeed(paths, expect))
t, data, done := job.BuildTree(schan)
Expand All @@ -55,7 +56,7 @@ func CopyCheck(projectPath string, expect string) [][]string {
t.Update(&syntax.Node{Type: -1})

if verbose {
log.Println("Searching for clones")
glog.Errorln("Searching for clones")
}
mchan := t.FindDuplOver(threshold)
duplChan := make(chan syntax.Match)
Expand Down Expand Up @@ -94,7 +95,8 @@ func crawlPaths(paths []string, expect string) chan string {
for _, path := range paths {
info, err := os.Lstat(path)
if err != nil {
log.Fatal(err)
glog.Errorln(err)
break
}
if !info.IsDir() {
fchan <- path
Expand Down
29 changes: 17 additions & 12 deletions linters/depend/depend.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import (
"fmt"
"go/build"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"

"github.com/golang/glog"
)

var (
Expand Down Expand Up @@ -51,7 +52,8 @@ func Depend(path, expect string) string {
args := []string{path}

if len(args) != 1 {
log.Fatal("need one package name to process")
glog.Errorln("need one package name to process")
return ""
}

if ignorePrefixes != "" {
Expand All @@ -72,10 +74,12 @@ func Depend(path, expect string) string {

cwd, err := os.Getwd()
if err != nil {
log.Fatalf("failed to get cwd: %s", err)
glog.Errorf("failed to get cwd: %s", err)
return ""
}
if err := processPackage(cwd, args[0]); err != nil {
log.Fatal(err)
glog.Errorln(err)
return ""
}

graph := "digraph godep {"
Expand Down Expand Up @@ -122,7 +126,7 @@ func Depend(path, expect string) string {

err = ioutil.WriteFile("graph.gv", []byte(graph), 0666)
if err != nil {
log.Println(err)
glog.Errorln(err)
}

// convert file formate
Expand All @@ -132,22 +136,22 @@ func Depend(path, expect string) string {
cmdsvg.Stderr = os.Stderr
err = cmdsvg.Run()
if err != nil {
log.Println(err)
glog.Errorln(err)
}

svg, err := ioutil.ReadFile("pkgdep.svg")
if err != nil {
log.Println(err)
glog.Errorln(err)
}

err = os.Remove("pkgdep.svg")
if err != nil {
log.Println(err)
glog.Errorln(err)
}

err = os.Remove("graph.gv")
if err != nil {
log.Println(err)
glog.Errorln(err)
}

return string(svg)
Expand Down Expand Up @@ -270,19 +274,20 @@ func getVendorlist(path string) []string {
return nil
})
if err != nil {
log.Printf("filepath.Walk() returned %v\n", err)
glog.Errorf("filepath.Walk() returned %v\n", err)
}
return vendors
}

func PackageAbsPath(path string) (packagePath string) {
_, err := os.Stat(path)
if err != nil {
log.Fatal("package path is invalid")
glog.Errorln("package path is invalid")
return ""
}
absPath, err := filepath.Abs(path)
if err != nil {
log.Println(err)
glog.Errorln(err)
}
packagePathIndex := strings.Index(absPath, "src")
if -1 != packagePathIndex {
Expand Down
6 changes: 4 additions & 2 deletions tools/report2html.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,12 @@ func Json2Html(jsonData []byte) (HtmlData, error) {
htmlData.Issues = issues
htmlData.Date = structData.TimeStamp

if len(importPackages) > 0 {
if len(importPackages) > 0 && len(noTestPackages) == 0 {
htmlData.AveragePackageCover = float64(100)
} else if len(importPackages) > 0 {
htmlData.AveragePackageCover = float64(100 * (len(importPackages) - len(noTestPackages)) / len(importPackages))
} else {
htmlData.AveragePackageCover = float64(100)
htmlData.AveragePackageCover = float64(0)
}
return htmlData, nil
}
Expand Down

0 comments on commit 20f8b5d

Please sign in to comment.