Skip to content

Commit

Permalink
Improve error output in case local version is not installed
Browse files Browse the repository at this point in the history
Fixes #38
  • Loading branch information
markelog committed Mar 15, 2017
1 parent 6b9bfd4 commit 116a490
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 12 deletions.
16 changes: 14 additions & 2 deletions bin/ec-proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,33 @@ func setCmd(cmd *exec.Cmd, name, version string) {
cmd.Stdin = os.Stdin
}

// Get relative path to the dot file
func getRelativePath(dotPath string) string {
cwd, err := os.Getwd()
print.Error(err)

dir, err := filepath.Rel(filepath.Dir(cwd), cwd)
print.Error(err)

return "./" + filepath.Join(dir, filepath.Base(dotPath))
}

func main() {
_, name := path.Split(os.Args[0])

language := plugins.SearchBin(name)
dotFiles := plugins.Dots(language)
base := variables.Home()

version, err := io.GetVersion(dotFiles)
version, dotPath, err := io.GetVersion(dotFiles)
print.Error(err)

pathPart := filepath.Join(base, language, version)
binPath := filepath.Join(pathPart, "bin", name)

if _, err := os.Stat(binPath); os.IsNotExist(err) {
err = errors.New("Version " + version + " has not been established")
err = errors.New("Version \"" + version + "\" was defined on \"" +
getRelativePath(dotPath) + "\" path but this version is not installed")
print.Error(err)
}

Expand Down
20 changes: 19 additions & 1 deletion bin/ec/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ var _ = Describe("main", func() {
})
})

Describe("local install", func() {
Describe("local", func() {
It("should install version but don't switch to it globally", func() {
current := Command("go", "run", path, "ls", "node")

Expand Down Expand Up @@ -108,6 +108,24 @@ var _ = Describe("main", func() {
Expect(strings.Contains(string(upperRes), "♥ 6.5.0")).To(Equal(true))
Expect(strings.Contains(string(upperRes), "6.4.0")).To(Equal(true))
})

It("should provide more or less error message if local install is not there", func() {
pwd, _ := os.Getwd()
versionFile := filepath.Join(pwd, ".node-version")

Command("go", "run", path, "rm", "node@6.3.0")

io.WriteFile(versionFile, "6.3.0")

result, _ := Command("node", "-v").CombinedOutput()

expected := string(result)
actual := "Version \"6.3.0\" was defined on \"./ec/.node-version\" path but this version is not installed"

Expect(expected).To(ContainSubstring(actual))

os.RemoveAll(versionFile)
})
})
})

Expand Down
2 changes: 1 addition & 1 deletion cmd/commands/ls/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func listLocalVersions(language string) {
versions, err := plugin.List()
print.Error(err)

current, err := io.GetVersion(plugin.Dots())
current, _, err := io.GetVersion(plugin.Dots())
print.Error(err)

// In case we could find `.<language>-version` file i.e. there is no local version
Expand Down
12 changes: 5 additions & 7 deletions io/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ func walkUp(path string, fn Walker) {
return
}

func GetVersion(args ...interface{}) (version string, err error) {
var path string

func GetVersion(args ...interface{}) (version, path string, err error) {
if len(args) > 1 {
path, err = FindDotFile(args[0], args[1])
} else {
Expand All @@ -47,7 +45,7 @@ func GetVersion(args ...interface{}) (version string, err error) {
}

if _, err := os.Stat(path); os.IsNotExist(err) {
return "current", nil
return "current", "", nil
}

file, err := os.Open(path)
Expand All @@ -59,14 +57,14 @@ func GetVersion(args ...interface{}) (version string, err error) {

scanner := bufio.NewScanner(file)
for scanner.Scan() {
return scanner.Text(), nil
return scanner.Text(), path, nil
}

if scannerErr := scanner.Err(); scannerErr != nil {
return "", scannerErr
return "", "", scannerErr
}

return "current", nil
return "current", "", nil
}

func FindDotFile(args ...interface{}) (versionPath string, err error) {
Expand Down
3 changes: 2 additions & 1 deletion io/io_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ var _ = Describe("io", func() {
It("Should get version for node from .nvmrc file", func() {
dots := plugins.Dots("node")
path, _ := filepath.Abs("../testdata/io/node-with-nvm/")
result, _ := GetVersion(dots, path)
result, dotPath, _ := GetVersion(dots, path)

Expect(dotPath).To(ContainSubstring("io/node-with-nvm/.nvmrc"))
Expect(result).To(Equal("6.8.0"))
})
})
Expand Down

0 comments on commit 116a490

Please sign in to comment.