spf13--cobra/cobra/cmd/helpers_test.go

44 lines
1.4 KiB
Go
Raw Normal View History

2015-10-28 16:51:48 +00:00
package cmd
import (
"fmt"
"os"
2015-11-24 03:46:26 +00:00
"path/filepath"
2015-10-28 16:51:48 +00:00
"testing"
)
var _ = fmt.Println
var _ = os.Stderr
func checkGuess(t *testing.T, wd, input, expected string) {
testWd = wd
inputPath = input
guessProjectPath()
if projectPath != expected {
2016-08-09 18:21:57 +00:00
t.Errorf("Unexpected Project Path. \nGot: %q\nExpected: %q\nArg: %v\nDir: %v", projectPath, expected, input, wd)
2015-10-28 16:51:48 +00:00
}
reset()
}
func reset() {
testWd = ""
inputPath = ""
projectPath = ""
}
func TestProjectPath(t *testing.T) {
2015-11-24 04:19:22 +00:00
checkGuess(t, "", filepath.Join("github.com", "spf13", "hugo"), filepath.Join(getSrcPath(), "github.com", "spf13", "hugo"))
2016-08-09 18:21:57 +00:00
checkGuess(t, "", filepath.Join("spf13", "hugo"), filepath.Join(getSrcPath(), "spf13", "hugo"))
checkGuess(t, "", filepath.Join("/", "bar", "foo"), filepath.Join("/", "bar", "foo"))
2016-08-09 18:21:57 +00:00
checkGuess(t, "/bar/foo", "baz", filepath.Join(getSrcPath(), "github.com", "baz"))
checkGuess(t, "/bar/foo", "gopkg.in/baz", filepath.Join(getSrcPath(), "gopkg.in", "baz"))
checkGuess(t, "/bar/foo", "./baz", filepath.Join("/", "bar", "foo", "baz"))
checkGuess(t, "/bar/foo", "./gopkg.in/baz", filepath.Join("/", "bar", "foo", "gopkg.in", "baz"))
2015-11-24 03:46:26 +00:00
checkGuess(t, "/bar/foo/cmd", "", filepath.Join("/", "bar", "foo"))
checkGuess(t, "/bar/foo/command", "", filepath.Join("/", "bar", "foo"))
checkGuess(t, "/bar/foo/commands", "", filepath.Join("/", "bar", "foo"))
checkGuess(t, "github.com/spf13/hugo/../hugo", "", filepath.Join("github.com", "spf13", "hugo"))
2015-10-28 16:51:48 +00:00
}