Skip to content

Commit

Permalink
feat: improve gb.CurrentVersion()
Browse files Browse the repository at this point in the history
  • Loading branch information
juev committed Dec 6, 2023
1 parent 3ef853a commit f8d305e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
12 changes: 9 additions & 3 deletions gobrew.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"path/filepath"
"regexp"
"runtime"
"slices"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -431,9 +432,14 @@ func (gb *GoBrew) CurrentVersion() string {
return ""
}

version := strings.ReplaceAll(fp, strings.Join([]string{"go", "bin"}, string(os.PathSeparator)), "")
version = strings.ReplaceAll(version, gb.versionsDir, "")
version = strings.ReplaceAll(version, string(os.PathSeparator), "")
version := strings.TrimSuffix(fp, strings.Join([]string{"go", "bin"}, string(os.PathSeparator)))
paths := strings.Split(version, string(os.PathSeparator))
slices.Reverse(paths)
for _, version = range paths {
if version != "" {
break
}
}
return version
}

Expand Down
28 changes: 25 additions & 3 deletions gobrew_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestNewGobrewHomeDirUsesGoBrewRoot(t *testing.T) {
}

func TestJudgeVersion(t *testing.T) {
t.Parallel()
//t.Parallel()
tests := []struct {
version string
wantVersion string
Expand Down Expand Up @@ -107,7 +107,7 @@ func TestJudgeVersion(t *testing.T) {
for _, test := range tests {
test := test
t.Run(test.version, func(t *testing.T) {
t.Parallel()
//t.Parallel()
gb := NewGoBrew()
version := gb.judgeVersion(test.version)
assert.Equal(t, test.wantVersion, version)
Expand Down Expand Up @@ -180,7 +180,7 @@ func TestUpgrade(t *testing.T) {
}

func TestDoNotUpgradeLatestVersion(t *testing.T) {
t.Parallel()
//t.Parallel()
t.Skip("skipping test...needs to rewrite")
tempDir := t.TempDir()

Expand Down Expand Up @@ -239,3 +239,25 @@ func TestInteractive(t *testing.T) {
currentVersion = strings.Replace(currentVersion, "private", "", -1)
assert.Equal(t, currentVersion, latestVersion)
}

func TestPrune(t *testing.T) {
t.Parallel()
tempDir := t.TempDir()
gb := NewGoBrewDirectory(tempDir)
gb.Install("1.20")
gb.Install("1.19")
gb.Use("1.19")
gb.Prune()
assert.Equal(t, false, gb.existsVersion("1.20"))
assert.Equal(t, true, gb.existsVersion("1.19"))
}

func TestGoBrew_CurrentVersion(t *testing.T) {
t.Parallel()
tempDir := t.TempDir()
gb := NewGoBrewDirectory(tempDir)
assert.Equal(t, true, gb.CurrentVersion() == "")
gb.Install("1.19")
gb.Use("1.19")
assert.Equal(t, true, gb.CurrentVersion() == "1.19")
}

0 comments on commit f8d305e

Please sign in to comment.