Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests 10122023 #167

Merged
merged 22 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
matrix:
go-version: [dev-latest]
os: [ubuntu-latest, windows-latest, macos-latest]
os: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
Expand All @@ -21,3 +21,4 @@ jobs:
version: ${{ matrix.go-version }}
- name: Test
run: go test -v ./...
shell: bash
21 changes: 17 additions & 4 deletions cmd/gobrew/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,27 @@ func init() {
}

func main() {
homeDir, ok := os.LookupEnv("GOBREW_ROOT")
if !ok || homeDir == "" {
rootDir := os.Getenv("GOBREW_ROOT")
if rootDir == "" {
var err error
homeDir, err = os.UserHomeDir()
rootDir, err = os.UserHomeDir()
utils.CheckError(err, "failed get home directory and GOBREW_ROOT not defined")
}

gb := gobrew.NewGoBrew(homeDir)
registryPath := gobrew.DefaultRegistryPath
if p := os.Getenv("GOBREW_REGISTRY"); p != "" {
registryPath = p
}

config := gobrew.Config{
RootDir: rootDir,
RegistryPathUrl: registryPath,
GobrewDownloadUrl: gobrew.DownloadUrl,
GobrewTags: gobrew.TagsApi,
GobrewVersionsUrl: gobrew.VersionsUrl,
}

gb := gobrew.NewGoBrew(config)
switch actionArg {
case "interactive", "info":
gb.Interactive(true)
Expand Down
20 changes: 10 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@ require (
github.com/Masterminds/semver v1.5.0
github.com/c4milo/unpackit v1.0.0
github.com/gookit/color v1.5.4
github.com/schollz/progressbar/v3 v3.13.1
github.com/schollz/progressbar/v3 v3.14.1
github.com/stretchr/testify v1.8.4
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dsnet/compress v0.0.1 // indirect
github.com/klauspost/compress v1.4.1 // indirect
github.com/klauspost/cpuid v1.2.0 // indirect
github.com/klauspost/pgzip v1.2.5 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/klauspost/compress v1.17.4 // indirect
github.com/klauspost/cpuid v1.3.1 // indirect
github.com/klauspost/pgzip v1.2.6 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/ulikunitz/xz v0.5.10 // indirect
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/term v0.6.0 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/ulikunitz/xz v0.5.11 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/term v0.15.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
23 changes: 23 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,22 @@ github.com/hooklift/assert v0.1.0/go.mod h1:pfexfvIHnKCdjh6CkkIZv5ic6dQ6aU2jhKgh
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213/go.mod h1:vNUNkEQ1e29fT/6vq2aBdFsgNPmy8qMdSay1npru+Sw=
github.com/klauspost/compress v1.4.1 h1:8VMb5+0wMgdBykOV96DwNwKFQ+WTI4pzYURP99CcB9E=
github.com/klauspost/compress v1.4.1/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
github.com/klauspost/compress v1.17.4 h1:Ej5ixsIri7BrIjBkRZLTo6ghwrEtHFk7ijlczPW4fZ4=
github.com/klauspost/compress v1.17.4/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM=
github.com/klauspost/cpuid v1.2.0 h1:NMpwD2G9JSFOE1/TJjGSo5zG7Yb2bTe7eq1jH+irmeE=
github.com/klauspost/cpuid v1.2.0/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
github.com/klauspost/cpuid v1.3.1 h1:5JNjFYYQrZeKRJ0734q51WCEEn2huer72Dc7K+R/b6s=
github.com/klauspost/cpuid v1.3.1/go.mod h1:bYW4mA6ZgKPob1/Dlai2LviZJO7KGI3uoWLd42rAQw4=
github.com/klauspost/pgzip v1.2.5 h1:qnWYvvKqedOF2ulHpMG72XQol4ILEJ8k2wwRl/Km8oE=
github.com/klauspost/pgzip v1.2.5/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs=
github.com/klauspost/pgzip v1.2.6 h1:8RXeL5crjEUFnR2/Sn6GJNWtSQ3Dk8pq4CL3jvdDyjU=
github.com/klauspost/pgzip v1.2.6/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ=
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand All @@ -32,23 +41,37 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/schollz/progressbar/v3 v3.13.1 h1:o8rySDYiQ59Mwzy2FELeHY5ZARXZTVJC7iHD6PEFUiE=
github.com/schollz/progressbar/v3 v3.13.1/go.mod h1:xvrbki8kfT1fzWzBT/UZd9L6GA+jdL7HAgq2RFnO6fQ=
github.com/schollz/progressbar/v3 v3.14.1 h1:VD+MJPCr4s3wdhTc7OEJ/Z3dAeBzJ7yKH/P4lC5yRTI=
github.com/schollz/progressbar/v3 v3.14.1/go.mod h1:Zc9xXneTzWXF81TGoqL71u0sBPjULtEHYtj/WVgVy8E=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/ulikunitz/xz v0.5.6/go.mod h1:2bypXElzHzzJZwzH67Y6wb67pO62Rzfn7BSiF4ABRW8=
github.com/ulikunitz/xz v0.5.10 h1:t92gobL9l3HE202wg3rlk19F6X+JOxl9BBrCCMYEYd8=
github.com/ulikunitz/xz v0.5.10/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
github.com/ulikunitz/xz v0.5.11 h1:kpFauv27b6ynzBNT/Xy+1k+fK4WswhN/6PN5WhFAGw8=
github.com/ulikunitz/xz v0.5.11/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8=
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.6.0 h1:clScbb1cHjoCkyRbWwBEUZ5H/tIFu5TAXIqaZD0Gcjw=
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
golang.org/x/term v0.14.0/go.mod h1:TySc+nGkYR6qt8km8wUhuFRTVSMIX3XPR58y2lC8vww=
golang.org/x/term v0.15.0 h1:y/Oo/a/q3IXu26lQgl04j/gjuBDOBlx7X6Om1j2CPW4=
golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
28 changes: 19 additions & 9 deletions gobrew.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"io/fs"
"net/url"
"os"
"path/filepath"
"regexp"
Expand All @@ -18,9 +19,10 @@ import (

const (
goBrewDir string = ".gobrew"
defaultRegistryPath string = "https://go.dev/dl/"
goBrewDownloadUrl string = "https://github.com/kevincobain2000/gobrew/releases/latest/download/"
goBrewTagsApi string = "https://raw.githubusercontent.com/kevincobain2000/gobrew/json/golang-tags.json"
DefaultRegistryPath string = "https://go.dev/dl/"
DownloadUrl string = "https://github.com/kevincobain2000/gobrew/releases/latest/download/"
TagsApi = "https://raw.githubusercontent.com/kevincobain2000/gobrew/json/golang-tags.json"
VersionsUrl string = "https://api.github.com/repos/kevincobain2000/gobrew/releases/latest"
)

// check GoBrew implement is Command interface
Expand All @@ -42,20 +44,28 @@ type Command interface {

// GoBrew struct
type GoBrew struct {
homeDir string
installDir string
versionsDir string
currentDir string
currentBinDir string
currentGoDir string
downloadsDir string
Config
}

type Config struct {
RootDir string
RegistryPathUrl string
GobrewDownloadUrl string
GobrewTags string
GobrewVersionsUrl string
}

// NewGoBrew instance
func NewGoBrew(homeDir string) GoBrew {
installDir := filepath.Join(homeDir, goBrewDir)
func NewGoBrew(config Config) GoBrew {
installDir := filepath.Join(config.RootDir, goBrewDir)
gb := GoBrew{
homeDir: homeDir,
Config: config,
installDir: installDir,
versionsDir: filepath.Join(installDir, "versions"),
currentDir: filepath.Join(installDir, "current"),
Expand Down Expand Up @@ -330,9 +340,9 @@ func (gb *GoBrew) Upgrade(currentVersion string) {

mkdirTemp, _ := os.MkdirTemp("", "gobrew")
tmpFile := filepath.Join(mkdirTemp, "gobrew"+fileExt)
url := goBrewDownloadUrl + "gobrew-" + gb.getArch() + fileExt
downloadUrl, _ := url.JoinPath(gb.GobrewDownloadUrl, "gobrew-"+gb.getArch()+fileExt)
utils.CheckError(
utils.DownloadWithProgress(url, "gobrew"+fileExt, mkdirTemp),
utils.DownloadWithProgress(downloadUrl, "gobrew"+fileExt, mkdirTemp),
"[Error] Download GoBrew failed")

source, err := os.Open(tmpFile)
Expand Down
63 changes: 46 additions & 17 deletions gobrew_test.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,60 @@
package gobrew

import (
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

func setupGobrew(t *testing.T, ts *httptest.Server) GoBrew {
tags, _ := url.JoinPath(ts.URL, "golang-tags.json")
versionUrl, _ := url.JoinPath(ts.URL, "latest")
config := Config{
RootDir: t.TempDir(),
RegistryPathUrl: ts.URL,
GobrewDownloadUrl: ts.URL,
GobrewTags: tags,
GobrewVersionsUrl: versionUrl,
}
gb := NewGoBrew(config)
return gb
}

func TestInstallAndExistVersion(t *testing.T) {
t.Parallel()
gb := NewGoBrew(t.TempDir())
gb.Install("1.19")
exists := gb.existsVersion("1.19")
ts := httptest.NewServer(http.FileServer(http.Dir("testdata")))
defer ts.Close()
gb := setupGobrew(t, ts)
gb.Install("1.9")
exists := gb.existsVersion("1.9")
assert.Equal(t, true, exists)
t.Log("test finished")
}

func TestUnInstallThenNotExistVersion(t *testing.T) {
t.Parallel()
gb := NewGoBrew(t.TempDir())
gb.Uninstall("1.19")
exists := gb.existsVersion("1.19")
ts := httptest.NewServer(http.FileServer(http.Dir("testdata")))
defer ts.Close()
gb := setupGobrew(t, ts)
gb.Install("1.9")
exists := gb.existsVersion("1.9")
assert.Equal(t, true, exists)
gb.Uninstall("1.9")
exists = gb.existsVersion("1.9")
assert.Equal(t, false, exists)
t.Log("test finished")
}

func TestUpgrade(t *testing.T) {
t.Parallel()
gb := NewGoBrew(t.TempDir())
ts := httptest.NewServer(http.FileServer(http.Dir("testdata")))
defer ts.Close()
gb := setupGobrew(t, ts)

binaryDir := filepath.Join(gb.installDir, "bin")
_ = os.MkdirAll(binaryDir, os.ModePerm)
Expand All @@ -52,7 +77,9 @@ func TestUpgrade(t *testing.T) {

func TestDoNotUpgradeLatestVersion(t *testing.T) {
t.Skip("skipping test...needs to rewrite")
gb := NewGoBrew(t.TempDir())
ts := httptest.NewServer(http.FileServer(http.Dir("testdata")))
defer ts.Close()
gb := setupGobrew(t, ts)

binaryDir := filepath.Join(gb.installDir, "bin")
_ = os.MkdirAll(binaryDir, os.ModePerm)
Expand All @@ -76,37 +103,37 @@ func TestDoNotUpgradeLatestVersion(t *testing.T) {

func TestInteractive(t *testing.T) {
t.Parallel()
gb := NewGoBrew(t.TempDir())
ts := httptest.NewServer(http.FileServer(http.Dir("testdata")))
defer ts.Close()
gb := setupGobrew(t, ts)

currentVersion := gb.CurrentVersion()
latestVersion := gb.getLatestVersion()
// modVersion := gb.getModVersion()
assert.Equal(t, "None", currentVersion)
assert.NotEqual(t, currentVersion, latestVersion)

gb.Interactive(false)

currentVersion = gb.CurrentVersion()
// remove string private from currentVersion (for macOS) due to /private/var symlink issue
currentVersion = strings.Replace(currentVersion, "private", "", -1)
assert.Equal(t, currentVersion, latestVersion)

gb.Install("1.16.5") // we know, it is not latest
gb.Use("1.16.5")
currentVersion = gb.CurrentVersion()
currentVersion = strings.Replace(currentVersion, "private", "", -1)
assert.Equal(t, "1.16.5", currentVersion)
assert.NotEqual(t, currentVersion, latestVersion)

gb.Interactive(false)
currentVersion = gb.CurrentVersion()
currentVersion = strings.Replace(currentVersion, "private", "", -1)
assert.Equal(t, currentVersion, latestVersion)
t.Log("test finished")
}

func TestPrune(t *testing.T) {
t.Parallel()
gb := NewGoBrew(t.TempDir())
ts := httptest.NewServer(http.FileServer(http.Dir("testdata")))
defer ts.Close()
gb := setupGobrew(t, ts)
gb.Install("1.20")
gb.Install("1.19")
gb.Use("1.19")
Expand All @@ -118,7 +145,9 @@ func TestPrune(t *testing.T) {

func TestGoBrew_CurrentVersion(t *testing.T) {
t.Parallel()
gb := NewGoBrew(t.TempDir())
ts := httptest.NewServer(http.FileServer(http.Dir("testdata")))
defer ts.Close()
gb := setupGobrew(t, ts)
assert.Equal(t, true, gb.CurrentVersion() == "None")
gb.Install("1.19")
gb.Use("1.19")
Expand Down
Loading