// return a list of available virtual go environments for the user func (e *EnvironmentsList) getEnvironments() ([]string, []string, error) { envs_path := filepath.Join("~", ".VenGO", "*") files, err := filepath.Glob(cache.ExpandUser(envs_path)) if err != nil { fmt.Println("while getting list of environments:", err) return nil, nil, err } available, invalid := []string{}, []string{} for _, file := range files { var vengoenv string filename := path.Base(file) stat, err := os.Stat(file) if err != nil { fmt.Println("while getting list of environments:", err) return nil, nil, err } if stat.IsDir() && filename != "bin" && filename != "scripts" { _, err := os.Open(filepath.Join(file, "bin", "activate")) if err != nil { if os.IsNotExist(err) || os.IsPermission(err) { invalid = append(invalid, filename) } continue } if r, err := os.Readlink(filepath.Join(file, "lib")); err != nil { if os.IsNotExist(err) || os.IsPermission(err) { invalid = append(invalid, filename) } continue } else { if e.DisplayAs == Text { vengoenv = fmt.Sprintf("%-22s%-8s", filename, path.Base(r)) } else { vengoenv = filename } if _, err := os.Stat(r); err != nil { if os.IsNotExist(err) || os.IsPermission(err) { invalid = append(invalid, vengoenv) } continue } } available = append(available, vengoenv) } } return available, invalid, nil }
// activate environment in the current process func (e *Environment) activate() { os.Setenv("VENGO_ENV", cache.ExpandUser(e.VenGO_PATH)) os.Setenv("GOTOOLDIR", cache.ExpandUser(e.Gotooldir)) os.Setenv("GORROT", cache.ExpandUser(e.Goroot)) os.Setenv("GOPATH", cache.ExpandUser(e.Gopath)) }
var re *regexp.Regexp BeforeEach(func() { if runtime.GOOS != "darwin" && runtime.GOOS != "windows" { re = regexp.MustCompile("/home/([a-z0-9]+)/VenGO") } if runtime.GOOS == "darwin" { re = regexp.MustCompile("/Users/([a-z0-9]+)/VenGO") } if runtime.GOOS == "windows" { re = regexp.MustCompile("\\Users\\([a-z0-9]+)\\VenGO") } }) It("Should Match", func() { Expect(re.MatchString(cache.ExpandUser("~/VenGO"))).To(BeTrue()) }) }) Describe("CacheDirectory is a valid directory on each platform", func() { var re *regexp.Regexp BeforeEach(func() { if runtime.GOOS != "darwin" && runtime.GOOS != "windows" { re = regexp.MustCompile("/home/([a-z0-9]+)/.cache/VenGO") } if runtime.GOOS == "darwin" { re = regexp.MustCompile( "/Users/([a-zA-Z0-9]+)/Library/Caches/VenGO") }
Expect(l.DisplayAs).To(Equal(commands.Text)) displayJson := func(e *commands.EnvironmentsList) { e.DisplayAs = commands.Json } l = commands.NewEnvironmentsList(displayJson) Expect(l).ToNot(BeNil()) Expect(l.DisplayAs).To(Equal(commands.Json)) }) }) Describe("EnvironmentsList", func() { Describe("Run", func() { var renamed bool = false envs_path := cache.ExpandUser(filepath.Join("~", ".VenGO")) BeforeEach(func() { if _, err := os.Stat(envs_path); err == nil { rename := filepath.Join(envs_path, "..", "Real.VenGO") Expect(os.Rename(envs_path, rename)).To(Succeed()) Expect(os.MkdirAll(envs_path, 0755)).To(Succeed()) renamed = true } }) AfterEach(func() { if renamed { rename := filepath.Join(envs_path, "..", "Real.VenGO") Expect(os.RemoveAll(envs_path)).To(Succeed()) Expect(os.Rename(rename, envs_path)).To(Succeed()) }