spf13--cobra/cobra/cmd/helpers_test.go
irfan sharif 40d25d2c31 Compatibility for multiple GOPATH workspaces
This commit adds the necessary changes to support users with multiple
`GOPATH` workspaces. Previously the `getSrcPath()` function in
`cobra/cmd/helpers.go` would not take into account the fact the the
system `GOPATH` returns a colon-separated list if system was configured
with multiple GOPATH workspaces. If the GOPATH was set to be
`/foo/a:/bar/b`, it would simply append `src/` to the end of the string
literal giving `/foo/a:/bar/b/src` which resulted in undesired behavior.

Following the specs the changeset here adds support for multiple GOPATH
workspaces and if a given package directory is not to be found in any of
the workspaces the first one is chosen, i.e. for `cobra init` it will
first check all workspaces for the specified project and settling on the
first (`/foo/a`) if nothing found.
This preserves backwards compatibility as single workspace environments
are provably unaffected by these changes.
2016-10-01 20:51:36 -04:00

40 lines
1.1 KiB
Go

package cmd
import (
"fmt"
"os"
"path/filepath"
"testing"
)
var _ = fmt.Println
var _ = os.Stderr
func checkGuess(t *testing.T, wd, input, expected string) {
testWd = wd
inputPath = input
guessProjectPath()
if projectPath != expected {
t.Errorf("Unexpected Project Path. \n Got: %q\nExpected: %q\n", projectPath, expected)
}
reset()
}
func reset() {
testWd = ""
inputPath = ""
projectPath = ""
}
func TestProjectPath(t *testing.T) {
checkGuess(t, "", filepath.Join("github.com", "spf13", "hugo"), filepath.Join(getSrcPaths()[0], "github.com", "spf13", "hugo"))
checkGuess(t, "", filepath.Join("spf13", "hugo"), filepath.Join(getSrcPaths()[0], "github.com", "spf13", "hugo"))
checkGuess(t, "", filepath.Join("/", "bar", "foo"), filepath.Join("/", "bar", "foo"))
checkGuess(t, "/bar/foo", "baz", filepath.Join("/", "bar", "foo", "baz"))
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"))
}