diff --git a/cmd/cmd_test.go b/cmd/cmd_test.go new file mode 100644 index 000000000..70a146b4f --- /dev/null +++ b/cmd/cmd_test.go @@ -0,0 +1,31 @@ +// Copyright 2013-2014 gopm authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"): you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. + +package cmd + +import ( + "testing" + + . "github.com/smartystreets/goconvey/convey" +) + +func Test_parseTarget(t *testing.T) { + Convey("Target is empty", t, func() { + So(parseTarget(""), ShouldEqual, ".") + }) + + Convey("Target is not empty", t, func() { + So(parseTarget("github.com/gpmgo/gopm"), ShouldEqual, "github.com/gpmgo/gopm") + }) +} diff --git a/cmd/get.go b/cmd/get.go index a6f616b86..252d4c3a0 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -106,8 +106,11 @@ func runGet(ctx *cli.Context) { switch len(ctx.Args()) { case 0: getByGopmfile(ctx) - default: + case 1: getByPath(ctx) + default: + log.Error("get", "too many arguments") + log.Help("Try 'gopm help get' to get more information") } } @@ -124,6 +127,7 @@ func getByGopmfile(ctx *cli.Context) { nodes := make([]*doc.Node, 0, len(imports)) for _, p := range imports { + // TODO: DOING TEST CASES!!! p = doc.GetProjectPath(p) // Skip subpackage(s) of current project. if isSubpackage(p, targetPath) { diff --git a/doc/utils.go b/doc/utils.go index 558ab2ac9..98243d8d3 100644 --- a/doc/utils.go +++ b/doc/utils.go @@ -29,21 +29,24 @@ import ( const VENDOR = ".vendor" // GetDirsInfo returns os.FileInfo of all sub-directories in root path. -func GetDirsInfo(rootPath string) []os.FileInfo { +func GetDirsInfo(rootPath string) ([]os.FileInfo, error) { + if !com.IsDir(rootPath) { + log.Warn("Directory %s does not exist", rootPath) + return []os.FileInfo{}, nil + } + rootDir, err := os.Open(rootPath) if err != nil { - log.Error("", "Fail to open directory") - log.Fatal("", err.Error()) + return nil, err } defer rootDir.Close() dirs, err := rootDir.Readdir(0) if err != nil { - log.Error("", "Fail to read directory") - log.Fatal("", err.Error()) + return nil, err } - return dirs + return dirs, nil } // GetImports returns package denpendencies. @@ -56,7 +59,11 @@ func GetImports(absPath, importPath string, example bool) []string { } } - fis := GetDirsInfo(absPath) + fis, err := GetDirsInfo(absPath) + if err != nil { + log.Error("", "Fail to get directory's information") + log.Fatal("", err.Error()) + } absPath += "/" imports := make([]string, 0, len(pkg.Imports)) @@ -66,7 +73,7 @@ func GetImports(absPath, importPath string, example bool) []string { } } - // TODO: Load too much + // TODO: Load too much, need to make sure which is imported which are not. dirs := make([]string, 0, len(imports)) for _, fi := range fis { if fi.IsDir() && !strings.Contains(fi.Name(), VENDOR) { @@ -80,12 +87,14 @@ func GetImports(absPath, importPath string, example bool) []string { return imports } +// isVcsPath returns true if the directory was created by VCS. func isVcsPath(dirPath string) bool { return strings.Contains(dirPath, "/.git") || strings.Contains(dirPath, "/.hg") || strings.Contains(dirPath, "/.svn") } +// GetAllImports returns all imports in given directory and all sub-directories. func GetAllImports(dirs []string, importPath string, example bool) (imports []string) { for _, d := range dirs { if !isVcsPath(d) && @@ -116,7 +125,11 @@ func CheckIsExistWithVCS(path string) bool { } // Check if only has VCS folder. - dirs := GetDirsInfo(path) + dirs, err := GetDirsInfo(path) + if err != nil { + log.Error("", "Fail to get directory's information") + log.Fatal("", err.Error()) + } if len(dirs) > 1 { return true diff --git a/doc/utils_test.go b/doc/utils_test.go new file mode 100644 index 000000000..568cc6d0f --- /dev/null +++ b/doc/utils_test.go @@ -0,0 +1,71 @@ +// Copyright 2013-2014 gopm authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"): you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. + +package doc + +import ( + "testing" + + . "github.com/smartystreets/goconvey/convey" +) + +var VcsTestPairs = map[string]bool{ + "/.hg": true, + "/.git": true, + "/.svn": true, + "/.vendor": false, +} + +func Test_isVcsPath(t *testing.T) { + Convey("Test if the path is belonging to VCS", t, func() { + for name, expect := range VcsTestPairs { + So(isVcsPath(name), ShouldEqual, expect) + } + }) +} + +func TestGetDirsInfo(t *testing.T) { + Convey("Get directory's information that exist", t, func() { + dis, err := GetDirsInfo(".") + So(err, ShouldBeNil) + So(len(dis), ShouldEqual, 13) + }) + + Convey("Get directory's information does not exist", t, func() { + dis, err := GetDirsInfo("./404") + So(err, ShouldBeNil) + So(len(dis), ShouldEqual, 0) + }) +} + +var GoStdTestPairs = map[string]bool{ + "net/http": true, + "fmt": true, + "github.com/gpmgo/gopm": false, + "github.com/Unknwon/com": false, +} + +func TestIsGoRepoPath(t *testing.T) { + Convey("Test if the path is belonging to Go STD", t, func() { + for name, expect := range GoStdTestPairs { + So(IsGoRepoPath(name), ShouldEqual, expect) + } + }) +} + +func TestGetImports(t *testing.T) { + Convey("Get package that are imported", t, func() { + So(len(GetImports(".", "github.com/gpmgo/gopm/docs", false)), ShouldEqual, 4) + }) +} diff --git a/doc/vcs.go b/doc/vcs.go index c3afc090c..224ae7507 100644 --- a/doc/vcs.go +++ b/doc/vcs.go @@ -1,4 +1,4 @@ -// Copyright 2013 gopm authors. +// Copyright 2013-2014 gopm authors. // // Licensed under the Apache License, Version 2.0 (the "License"): you may // not use this file except in compliance with the License. You may obtain @@ -245,7 +245,11 @@ metaScan: } func getImports(rootPath string, match map[string]string, cmdFlags map[string]bool, nod *Node) (imports []string) { - dirs := GetDirsInfo(rootPath) + dirs, err := GetDirsInfo(rootPath) + if err != nil { + log.Error("", "Fail to get directory's information") + log.Fatal("", err.Error()) + } for _, d := range dirs { if d.IsDir() && !(!cmdFlags["-e"] && strings.Contains(d.Name(), "example")) { diff --git a/gopm.go b/gopm.go index 80700358e..3d3d98cdf 100644 --- a/gopm.go +++ b/gopm.go @@ -29,7 +29,7 @@ import ( // Test that go1.1 tag above is included in builds. main.go refers to this definition. const go11tag = true -const APP_VER = "0.6.3.0313" +const APP_VER = "0.6.3.0315" // //cmd.CmdSearch, // cmdClean,