Browse Source

Add some test cases

pull/103/head
Unknown 11 years ago
parent
commit
149080db4b
  1. 31
      cmd/cmd_test.go
  2. 6
      cmd/get.go
  3. 31
      doc/utils.go
  4. 71
      doc/utils_test.go
  5. 8
      doc/vcs.go
  6. 2
      gopm.go

31
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")
})
}

6
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) {

31
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

71
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)
})
}

8
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")) {

2
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,

Loading…
Cancel
Save