From 116a4901ed356e4eea4a5f68b7ae9302f9b06341 Mon Sep 17 00:00:00 2001 From: Oleg Gaidarenko Date: Wed, 15 Mar 2017 12:14:32 +0100 Subject: [PATCH] Improve error output in case local version is not installed Fixes #38 --- bin/ec-proxy/main.go | 16 ++++++++++++++-- bin/ec/main_test.go | 20 +++++++++++++++++++- cmd/commands/ls/ls.go | 2 +- io/io.go | 12 +++++------- io/io_test.go | 3 ++- 5 files changed, 41 insertions(+), 12 deletions(-) diff --git a/bin/ec-proxy/main.go b/bin/ec-proxy/main.go index f497f5f..d631273 100644 --- a/bin/ec-proxy/main.go +++ b/bin/ec-proxy/main.go @@ -33,6 +33,17 @@ 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]) @@ -40,14 +51,15 @@ func main() { 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) } diff --git a/bin/ec/main_test.go b/bin/ec/main_test.go index 9ec6500..73fc94e 100644 --- a/bin/ec/main_test.go +++ b/bin/ec/main_test.go @@ -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") @@ -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) + }) }) }) diff --git a/cmd/commands/ls/ls.go b/cmd/commands/ls/ls.go index d7f41ff..58bd818 100644 --- a/cmd/commands/ls/ls.go +++ b/cmd/commands/ls/ls.go @@ -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 `.-version` file i.e. there is no local version diff --git a/io/io.go b/io/io.go index 2b07ff9..56290df 100644 --- a/io/io.go +++ b/io/io.go @@ -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 { @@ -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) @@ -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) { diff --git a/io/io_test.go b/io/io_test.go index 2d32547..498ca1f 100644 --- a/io/io_test.go +++ b/io/io_test.go @@ -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")) }) })