mirror of
https://github.com/gojp/goreportcard.git
synced 2026-01-28 22:39:05 +08:00
add method for checking gofmt using go/format package (not currently switched on)
This commit is contained in:
@@ -19,6 +19,7 @@ func (g GoFmt) Weight() float64 {
|
||||
// Percentage returns the percentage of .go files that pass gofmt
|
||||
func (g GoFmt) Percentage() (float64, []FileSummary, error) {
|
||||
return GoTool(g.Dir, g.Filenames, []string{"gometalinter", "--deadline=180s", "--disable-all", "--enable=gofmt"})
|
||||
// return GoFmtNative(g.Dir, g.Filenames)
|
||||
}
|
||||
|
||||
// Description returns the description of gofmt
|
||||
|
||||
@@ -2,7 +2,10 @@ package check
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"go/format"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
@@ -167,6 +170,26 @@ func (fs *FileSummary) AddError(out string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func fileURL(dir, filename string) string {
|
||||
var fileURL string
|
||||
base := strings.TrimPrefix(dir, "repos/src/")
|
||||
switch {
|
||||
case strings.HasPrefix(base, "golang.org/x/"):
|
||||
var pkg string
|
||||
if len(strings.Split(base, "/")) >= 3 {
|
||||
pkg = strings.Split(base, "/")[2]
|
||||
}
|
||||
return fmt.Sprintf("https://github.com/golang/%s/blob/master%s", pkg, strings.TrimPrefix(filename, "/"+base))
|
||||
case strings.HasPrefix(base, "github.com/"):
|
||||
if len(strings.Split(base, "/")) == 4 {
|
||||
base = strings.Join(strings.Split(base, "/")[0:3], "/")
|
||||
}
|
||||
return fmt.Sprintf("https://%s/blob/master%s", base, strings.TrimPrefix(filename, "/"+base))
|
||||
}
|
||||
|
||||
return fileURL
|
||||
}
|
||||
|
||||
// GoTool runs a given go command (for example gofmt, go tool vet)
|
||||
// on a directory
|
||||
func GoTool(dir string, filenames, command []string) (float64, []FileSummary, error) {
|
||||
@@ -206,21 +229,7 @@ outer:
|
||||
continue outer
|
||||
}
|
||||
|
||||
var fileURL string
|
||||
base := strings.TrimPrefix(dir, "repos/src/")
|
||||
switch {
|
||||
case strings.HasPrefix(base, "golang.org/x/"):
|
||||
var pkg string
|
||||
if len(strings.Split(base, "/")) >= 3 {
|
||||
pkg = strings.Split(base, "/")[2]
|
||||
}
|
||||
fileURL = fmt.Sprintf("https://github.com/golang/%s/blob/master%s", pkg, strings.TrimPrefix(filename, "/"+base))
|
||||
case strings.HasPrefix(base, "github.com/"):
|
||||
if len(strings.Split(base, "/")) == 4 {
|
||||
base = strings.Join(strings.Split(base, "/")[0:3], "/")
|
||||
}
|
||||
fileURL = fmt.Sprintf("https://%s/blob/master%s", base, strings.TrimPrefix(filename, "/"+base))
|
||||
}
|
||||
fu := fileURL(dir, filename)
|
||||
fs := fsMap[filename]
|
||||
if fs.Filename == "" {
|
||||
fs.Filename = filename
|
||||
@@ -231,7 +240,7 @@ outer:
|
||||
}
|
||||
|
||||
}
|
||||
fs.FileURL = fileURL
|
||||
fs.FileURL = fu
|
||||
}
|
||||
err = fs.AddError(out.Text())
|
||||
if err != nil {
|
||||
@@ -276,3 +285,45 @@ outer:
|
||||
|
||||
return float64(len(filenames)-len(failed)) / float64(len(filenames)), failed, nil
|
||||
}
|
||||
|
||||
// GoFmtNative runs gofmt via golang's stdlib format pkg
|
||||
func GoFmtNative(dir string, filenames []string) (float64, []FileSummary, error) {
|
||||
var failed = []FileSummary{}
|
||||
for _, f := range filenames {
|
||||
for _, skip := range skipSuffixes {
|
||||
if strings.HasSuffix(f, skip) {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
if autoGenerated(f) {
|
||||
continue
|
||||
}
|
||||
|
||||
b, err := ioutil.ReadFile(f)
|
||||
if err != nil {
|
||||
return 0, []FileSummary{}, err
|
||||
}
|
||||
g, err := format.Source(b)
|
||||
if err != nil {
|
||||
return 0, []FileSummary{}, err
|
||||
}
|
||||
if !bytes.Equal(b, g) {
|
||||
filename := strings.TrimPrefix(f, "repos/src")
|
||||
fs := FileSummary{}
|
||||
fs.Filename = filename
|
||||
if strings.HasPrefix(filename, "/github.com") {
|
||||
sp := strings.Split(filename, "/")
|
||||
if len(sp) > 3 {
|
||||
fs.Filename = strings.Join(sp[3:], "/")
|
||||
}
|
||||
}
|
||||
fu := fileURL(dir, strings.TrimPrefix(f, "repos/src"))
|
||||
fs.FileURL = fu
|
||||
fs.Errors = append(fs.Errors, Error{1, "file is not gofmted"})
|
||||
failed = append(failed, fs)
|
||||
}
|
||||
}
|
||||
|
||||
return float64(len(filenames)-len(failed)) / float64(len(filenames)), failed, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user