Skip to content

Commit

Permalink
rewrite flags and fix project vendors fix#10
Browse files Browse the repository at this point in the history
  • Loading branch information
wgliang committed Apr 13, 2017
1 parent 8968fa7 commit 0765e41
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 17 deletions.
2 changes: 1 addition & 1 deletion engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (r *Reporter) Engine(projectPath string, exceptPackages string) {
for pkgName, pkgPath := range dirsUnitTest {
pkg.Add(1)
go func(pkgName, pkgPath string) {
unitTestRes, unitRaceRes := unittest.UnitTest(pkgPath)
unitTestRes, unitRaceRes := unittest.UnitTest("./" + pkgPath)
var packageTest PackageTest
if len(unitTestRes) >= 1 {
testres := unitTestRes[pkgName]
Expand Down
40 changes: 35 additions & 5 deletions linters/depend/depend.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,15 @@ var (

buildTags []string
buildContext = build.Default

vendors []string
)

func Depend(path, expect string) string {
ignorePackages = expect

vendors = getVendorlist(path)
// add root vendor
vendors = append(vendors, "vendor")
pkgs = make(map[string]*build.Package)
ids = make(map[string]int)

Expand Down Expand Up @@ -153,10 +157,19 @@ func processPackage(root string, pkgName string) error {
if ignored[pkgName] {
return nil
}

pkg, err := buildContext.Import(pkgName, root, 0)
if err != nil {
return fmt.Errorf("failed to import %s: %s", pkgName, err)
flag := false
for i := 0; i < len(vendors); i++ {
pkg, err = buildContext.Import(vendors[i]+string(filepath.Separator)+pkgName, root, 0)
if err == nil {
flag = true
break
}
}
if !flag {
return fmt.Errorf("failed to import %s: %s", pkgName, err)
}
}

if isIgnored(pkg) {
Expand Down Expand Up @@ -251,8 +264,8 @@ func getVendorlist(path string) []string {
if !f.IsDir() {
return nil
}
if strings.HasSuffix(path, "vendor") {
vendors = append(vendors, path)
if strings.HasSuffix(path, "vendor") && path != "" {
vendors = append(vendors, PackageAbsPath(path))
}
return nil
})
Expand All @@ -261,3 +274,20 @@ func getVendorlist(path string) []string {
}
return vendors
}

func PackageAbsPath(path string) (packagePath string) {
_, err := os.Stat(path)
if err != nil {
log.Fatal("package path is invalid")
}
absPath, err := filepath.Abs(path)
if err != nil {
log.Println(err)
}
packagePathIndex := strings.Index(absPath, "src")
if -1 != packagePathIndex {
packagePath = absPath[(packagePathIndex + 4):]
}

return packagePath
}
8 changes: 6 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"os"
"strconv"
"strings"
"time"
)

Expand All @@ -14,13 +15,13 @@ var (
// project path:Must Be Relative path
project = flag.String("p", "", "path of project.")
// save path of report
report = flag.String("d", "", "path of report.")
report = flag.String("r", "", "path of report.")
// except packages,multiple packages are separated by semicolons
except = flag.String("e", "", "except packages.")
// template
tplpath = flag.String("t", "", "project meta information.")
// report formate
formate = flag.String("r", "", "project report formate(json/html).")
formate = flag.String("f", "", "project report formate(json/html).")
)

func main() {
Expand All @@ -44,6 +45,9 @@ func main() {
tpl = string(fileData)
}
} else {
if !strings.HasSuffix(*report, ".html") {
log.Println("The template file is not a html template")
}
fileData, err := ioutil.ReadFile(*tplpath)
if err != nil {
log.Fatal(err)
Expand Down
18 changes: 9 additions & 9 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@ import (

func DirList(projectPath string, suffix, expect string) (dirs map[string]string, err error) {
dirs = make(map[string]string, 0)
if strings.HasSuffix(projectPath, "./") {
log.Fatal("please specify a relative path with the project name.")
}
if absPath(projectPath) == projectPath {
log.Fatal("please specify a relative path with the project name.")
}
_, err = os.Stat(projectPath)
if err != nil {
log.Fatal("dir path is invalid")
}
err = filepath.Walk(projectPath, func(projectPath string, f os.FileInfo, err error) error {
err = filepath.Walk(projectPath, func(subPath string, f os.FileInfo, err error) error {
if f == nil {
return err
}
if f.IsDir() {
return nil
}
if strings.HasSuffix(projectPath, suffix) {
dir := projectPath[0:strings.LastIndex(projectPath, string(filepath.Separator))]
if strings.HasSuffix(subPath, suffix) {
sepIdx := strings.LastIndex(subPath, string(filepath.Separator))
var dir string
if sepIdx == -1 {
dir = "."
} else {
dir = subPath[0:sepIdx]
}
if ExpectPkg(expect, dir) {
return nil
}
Expand Down

0 comments on commit 0765e41

Please sign in to comment.