diff options
| author | Shulhan <ms@kilabit.info> | 2018-06-05 23:23:46 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2018-06-05 23:23:46 +0700 |
| commit | 96cf87c6134f0dd4b9e002b734f98dd2074b77c7 (patch) | |
| tree | 7280782eb6100ec458bec632237f00102ce5abbe | |
| parent | dec955d8f85e6ca5515bee53fd25dc8f24da2027 (diff) | |
| download | beku-96cf87c6134f0dd4b9e002b734f98dd2074b77c7.tar.xz | |
ScanBuild: simplify checking vendor file
| -rw-r--r-- | common.go | 17 | ||||
| -rw-r--r-- | common_test.go | 36 | ||||
| -rw-r--r-- | package.go | 28 |
3 files changed, 59 insertions, 22 deletions
@@ -88,6 +88,23 @@ func IsDirEmpty(dir string) (ok bool) { } // +// IsFileExist will return true if relative path is exist on parent directory; +// otherwise it will return false. +// +func IsFileExist(parent, relpath string) bool { + path := filepath.Join(parent, relpath) + + fi, err := os.Stat(path) + if err != nil { + return false + } + if fi.IsDir() { + return false + } + return true +} + +// // IsIgnoredDir will return true if directory start with "_" or ".", or // equal with "vendor" or "testdata"; otherwise it will return false. // diff --git a/common_test.go b/common_test.go index d7bb557..09021f1 100644 --- a/common_test.go +++ b/common_test.go @@ -89,6 +89,42 @@ func TestIsDirEmpty(t *testing.T) { } } +func TestIsFileExist(t *testing.T) { + wd, err := os.Getwd() + if err != nil { + t.Fatal(err) + } + + cases := []struct { + desc, parent, relpath string + exp bool + }{{ + desc: "With directory", + relpath: "testdata", + }, { + desc: "With non existen path", + parent: "/random", + relpath: "file", + }, { + desc: "With file exist without parent", + relpath: "LICENSE", + exp: true, + }, { + desc: "With file exist", + parent: wd, + relpath: "LICENSE", + exp: true, + }} + + for _, c := range cases { + t.Log(c.desc) + + got := IsFileExist(c.parent, c.relpath) + + test.Assert(t, "", c.exp, got, true) + } +} + func TestIsIgnoredDir(t *testing.T) { cases := []struct { name string @@ -310,35 +310,19 @@ func (pkg *Package) ScanDeps(env *Env) (err error) { // // * Godeps: gdm // * Gopkg.toml: dep -// * Makefile: make -// -// Otherwise, it would use "go install" // func (pkg *Package) ScanBuild() (cmd buildMode) { - f, err := os.Open(pkg.FullPath) - if err != nil { + ok := IsFileExist(pkg.FullPath, buildFileDep) + if ok { + cmd |= buildModeDep return } - - fis, err := f.Readdir(0) - if err != nil { + ok = IsFileExist(pkg.FullPath, buildFileGdm) + if ok { + cmd |= buildModeGdm return } - for x := range fis { - if fis[x].IsDir() { - continue - } - if fis[x].Name() == buildFileDep { - cmd |= buildModeDep - continue - } - if fis[x].Name() == buildFileGdm { - cmd |= buildModeGdm - continue - } - } - return } |
