Skip to content

Commit

Permalink
Merge pull request #160 from kevincobain2000/tests-06122023
Browse files Browse the repository at this point in the history
Tests 06122023
  • Loading branch information
kevincobain2000 committed Dec 8, 2023
2 parents e32c86f + faba08b commit 0e6fe5f
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 74 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ on:
- '**/*.go'
- '**/*.mod'
- '**/*.sum'
name: CI

name: "Build"
jobs:
build:
strategy:
Expand All @@ -20,5 +21,7 @@ jobs:

- name: Go version
run: go version
shell: bash
- name: Build
run: go build cmd/gobrew/main.go
shell: bash
11 changes: 9 additions & 2 deletions .github/workflows/coveritup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ on:
- '**/*.mod'
- '**/*.sum'
- '**/*.yaml'
name: Cover It Up

name: "Cover It Up"
jobs:
coveritup:
strategy:
Expand All @@ -30,12 +31,13 @@ jobs:
go install github.com/securego/gosec/v2/cmd/gosec@latest
go install github.com/axw/gocov/gocov@latest
go install github.com/AlekSi/gocov-xml@latest
shell: bash
- name: Test
run: |
BUILD_START=$SECONDS
go test -race -v ./... -count=1 -coverprofile=coverage.out
echo SCORE=$(($SECONDS-BUILD_START)) >> "$GITHUB_ENV"
shell: bash
- uses: kevincobain2000/action-coveritup@v1
with:
type: unit-test-run-time
Expand All @@ -49,6 +51,7 @@ jobs:
BUILD_START=$SECONDS
go build -ldflags '-s -w' -o main cmd/gobrew/main.go
echo SCORE=$(($SECONDS-BUILD_START)) >> "$GITHUB_ENV"
shell: bash

- uses: kevincobain2000/action-coveritup@v1
with:
Expand All @@ -58,6 +61,7 @@ jobs:
run: |
curl -sLk https://raw.githubusercontent.com/kevincobain2000/cover-totalizer/master/install.sh | sh
echo SCORE=`./cover-totalizer coverage.xml` >> "$GITHUB_ENV"
shell: bash

- uses: kevincobain2000/action-coveritup@v1
with:
Expand All @@ -66,6 +70,7 @@ jobs:
- name: Go Binary Size
run: |
echo SCORE=`du -sk main | awk '{print $1}'` >> "$GITHUB_ENV"
shell: bash

- uses: kevincobain2000/action-coveritup@v1
with:
Expand All @@ -74,6 +79,7 @@ jobs:
- name: Number of dependencies
run: |
echo SCORE=`go list -m all|wc -l|awk '{$1=$1};1'` >> "$GITHUB_ENV"
shell: bash

- uses: kevincobain2000/action-coveritup@v1
with:
Expand All @@ -82,6 +88,7 @@ jobs:
- name: Number of GO Sec issues
run: |
echo SCORE=`gosec -no-fail --quiet ./...|grep Issues | tail -1 |awk '{print $3}'` >> "$GITHUB_ENV"
shell: bash

- uses: kevincobain2000/action-coveritup@v1
with:
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
name: goreleaser

on:
push:
tags:
- '*'

name: "Create release"
jobs:
goreleaser:
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/sync-release-tags.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ on:
# hourly
# This crontab is related to https://github.com/kevincobain2000/gobrew/issues/45
- cron: '0 * * * *'
name: "Sync Go releases"

name: "Sync Go releases"
jobs:
sync-tags:
name: sync
Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ on:
- '**/*.mod'
- '**/*.sum'
- '**/*.yaml'
name: CI

name: "Go test"
jobs:
test:
strategy:
matrix:
go-version: [dev-latest]
os: [ubuntu-latest, macos-latest]
os: [ubuntu-latest, windows-latest, macos-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
Expand Down
35 changes: 9 additions & 26 deletions gobrew.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,15 +425,16 @@ func (gb *GoBrew) existsVersion(version string) bool {

// CurrentVersion get current version from symb link
func (gb *GoBrew) CurrentVersion() string {

fp, err := filepath.EvalSymlinks(gb.currentBinDir)
if err != nil {
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, filepath.Join("go", "bin"))
version = filepath.Base(version)
if version == "." {
return ""
}
return version
}

Expand Down Expand Up @@ -638,11 +639,6 @@ func (gb *GoBrew) Upgrade(currentVersion string) {
return
}

fileExt := ""
if runtime.GOOS == "windows" {
fileExt = ".exe"
}

mkdirTemp, _ := os.MkdirTemp("", "gobrew")
tmpFile := filepath.Join(mkdirTemp, "gobrew"+fileExt)
url := goBrewDownloadUrl + "gobrew-" + gb.getArch() + fileExt
Expand All @@ -658,12 +654,7 @@ func (gb *GoBrew) Upgrade(currentVersion string) {
}(source)

goBrewFile := filepath.Join(gb.installDir, "bin", "gobrew"+fileExt)
if runtime.GOOS == "windows" {
goBrewOldFile := goBrewFile + ".old"
utils.CheckError(os.Rename(goBrewFile, goBrewOldFile), "==> [Error] Cannot rename binary file")
} else {
utils.CheckError(os.Remove(goBrewFile), "==> [Error] Cannot remove binary file")
}
removeFile(goBrewFile)
destination, err := os.Create(goBrewFile)
utils.CheckError(err, "==> [Error] Cannot open file")
defer func(destination *os.File) {
Expand All @@ -687,14 +678,9 @@ func (gb *GoBrew) mkDirs(version string) {
func (gb *GoBrew) getVersionDir(version string) string {
return filepath.Join(gb.versionsDir, version)
}
func (gb *GoBrew) downloadAndExtract(version string) {
tarName := "go" + version + "." + gb.getArch()

if runtime.GOOS == "windows" {
tarName = tarName + ".zip"
} else {
tarName = tarName + ".tar.gz"
}
func (gb *GoBrew) downloadAndExtract(version string) {
tarName := "go" + version + "." + gb.getArch() + tarNameExt

registryPath := defaultRegistryPath
if p := os.Getenv("GOBREW_REGISTRY"); p != "" {
Expand Down Expand Up @@ -798,10 +784,7 @@ func (gb *GoBrew) getGolangVersions() (result []string) {
func doRequest(url string) (data []byte) {
client := &http.Client{}
request, err := http.NewRequest("GET", url, nil)
if err != nil {
color.Errorln("==> [Error] Cannot create request:", err.Error())
return
}
utils.CheckError(err, "==> [Error] Cannot create request")

request.Header.Set("User-Agent", "gobrew")

Expand Down
85 changes: 45 additions & 40 deletions gobrew_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@ func TestNewGobrewHomeDirUsesUserHomeDir(t *testing.T) {
gobrew := NewGoBrew()

assert.Equal(t, homeDir, gobrew.homeDir)
t.Log("test finished")
}

func TestNewGobrewHomeDirDefaultsToHome(t *testing.T) {
var envName string

if runtime.GOOS == "windows" {
switch runtime.GOOS {
case "windows":
envName = "USERPROFILE"
} else if runtime.GOOS == "plan9" {
case "plan9":
envName = "home"
} else {
default:
envName = "HOME"
}

Expand All @@ -43,6 +45,7 @@ func TestNewGobrewHomeDirDefaultsToHome(t *testing.T) {
gobrew := NewGoBrew()

assert.Equal(t, os.Getenv("HOME"), gobrew.homeDir)
t.Log("test finished")
}

func TestNewGobrewHomeDirUsesGoBrewRoot(t *testing.T) {
Expand All @@ -56,6 +59,7 @@ func TestNewGobrewHomeDirUsesGoBrewRoot(t *testing.T) {
gobrew := NewGoBrew()

assert.Equal(t, "some_fancy_value", gobrew.homeDir)
t.Log("test finished")
}

func TestJudgeVersion(t *testing.T) {
Expand Down Expand Up @@ -88,8 +92,8 @@ func TestJudgeVersion(t *testing.T) {
version: "1.18@dev-latest",
wantVersion: "1.18.10",
},
// // following 2 tests fail upon new version release
// // commenting out for now as the tool is stable
// following 2 tests fail upon new version release
// commenting out for now as the tool is stable
// {
// version: "latest",
// wantVersion: "1.19.1",
Expand All @@ -100,20 +104,23 @@ func TestJudgeVersion(t *testing.T) {
// },
}
for _, test := range tests {
test := test
t.Run(test.version, func(t *testing.T) {
gb := NewGoBrew()
version := gb.judgeVersion(test.version)
assert.Equal(t, test.wantVersion, version)

})
}
t.Log("test finished")
}

func TestListVersions(t *testing.T) {
tempDir := t.TempDir()
gb := NewGoBrewDirectory(tempDir)

gb.ListVersions()
t.Log("test finished")
}

func TestExistVersion(t *testing.T) {
Expand All @@ -123,37 +130,25 @@ func TestExistVersion(t *testing.T) {
exists := gb.existsVersion("1.19")

assert.Equal(t, false, exists)
t.Log("test finished")
}

func TestInstallAndExistVersion(t *testing.T) {
tempDir := filepath.Join(os.TempDir(), "gobrew-test-install-uninstall")
err := os.MkdirAll(tempDir, os.ModePerm)
if err != nil {
t.Skip("could not create directory for gobrew update:", err)
return
}

tempDir := t.TempDir()
gb := NewGoBrewDirectory(tempDir)
gb.Install("1.19")
exists := gb.existsVersion("1.19")
assert.Equal(t, true, exists)
t.Log("test finished")
}

func TestUnInstallThenNotExistVersion(t *testing.T) {
tempDir := filepath.Join(os.TempDir(), "gobrew-test-install-uninstall")
err := os.MkdirAll(tempDir, os.ModePerm)
if err != nil {
t.Skip("could not create directory for gobrew update:", err)
return
}
defer func() {
_ = os.RemoveAll(tempDir)
}()

tempDir := t.TempDir()
gb := NewGoBrewDirectory(tempDir)
gb.Uninstall("1.19")
exists := gb.existsVersion("1.19")
assert.Equal(t, false, exists)
t.Log("test finished")
}

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

baseName := "gobrew"
if runtime.GOOS == "windows" {
baseName = baseName + ".exe"
}
baseName := "gobrew" + fileExt
binaryFile := filepath.Join(binaryDir, baseName)

if oldFile, err := os.Create(binaryFile); err == nil {
Expand All @@ -180,6 +172,7 @@ func TestUpgrade(t *testing.T) {
if _, err := os.Stat(binaryFile); err != nil {
t.Errorf("updated executable does not exist")
}
t.Log("test finished")
}

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

baseName := "gobrew"
if runtime.GOOS == "windows" {
baseName = baseName + ".exe"
}
baseName := "gobrew" + fileExt
binaryFile := filepath.Join(binaryDir, baseName)

currentVersion := gb.getGobrewVersion()
Expand All @@ -208,19 +198,11 @@ func TestDoNotUpgradeLatestVersion(t *testing.T) {
if _, err := os.Stat(binaryFile); err == nil {
t.Errorf("unexpected upgrade of latest version")
}
t.Log("test finished")
}

func TestInteractive(t *testing.T) {
tempDir := filepath.Join(os.TempDir(), "gobrew-test-interactive")
err := os.MkdirAll(tempDir, os.ModePerm)
if err != nil {
t.Skip("could not create directory for gobrew update:", err)
return
}

defer func() {
_ = os.RemoveAll(tempDir)
}()
tempDir := t.TempDir()

gb := NewGoBrewDirectory(tempDir)
currentVersion := gb.CurrentVersion()
Expand Down Expand Up @@ -248,4 +230,27 @@ func TestInteractive(t *testing.T) {
currentVersion = gb.CurrentVersion()
currentVersion = strings.Replace(currentVersion, "private", "", -1)
assert.Equal(t, currentVersion, latestVersion)
t.Log("test finished")
}

func TestPrune(t *testing.T) {
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"))
t.Log("test finished")
}

func TestGoBrew_CurrentVersion(t *testing.T) {
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")
t.Log("test finished")
}
Loading

0 comments on commit 0e6fe5f

Please sign in to comment.