Skip to content

Commit

Permalink
Add unit tests (#284)
Browse files Browse the repository at this point in the history
* Add unit tests

* Add codecov badage in the readme file
  • Loading branch information
LinuxSuRen authored Aug 21, 2022
1 parent 0f23bd3 commit cdc9c8d
Show file tree
Hide file tree
Showing 5 changed files with 243 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[![](http://img.shields.io/badge/godoc-reference-5272B4.svg?style=flat-square)](https://godoc.org/github.com/linuxsuren/http-downloader)
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/7cc20ea84e0543068c320e471bde560e)](https://www.codacy.com/gh/LinuxSuRen/http-downloader/dashboard?utm_source=github.com&utm_medium=referral&utm_content=LinuxSuRen/http-downloader&utm_campaign=Badge_Grade)
[![Codacy Badge](https://app.codacy.com/project/badge/Coverage/7cc20ea84e0543068c320e471bde560e)](https://www.codacy.com/gh/LinuxSuRen/http-downloader/dashboard?utm_source=github.com&utm_medium=referral&utm_content=LinuxSuRen/http-downloader&utm_campaign=Badge_Coverage)
[![codecov](https://codecov.io/gh/LinuxSuRen/http-downloader/branch/master/graph/badge.svg?token=Ntc8z2iEQ2)](https://codecov.io/gh/LinuxSuRen/http-downloader)
[![Contributors](https://img.shields.io/github/contributors/linuxsuren/http-downloader.svg)](https://github.com/linuxsuren/github-go/graphs/contributors)
[![GitHub release](https://img.shields.io/github/release/linuxsuren/http-downloader.svg?label=release)](https://github.com/linuxsuren/github-go/releases/latest)
![GitHub All Releases](https://img.shields.io/github/downloads/linuxsuren/http-downloader/total)
Expand Down
4 changes: 2 additions & 2 deletions pkg/common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ func CheckDirPermission(dir string, perm os.FileMode) error {

// Exist returns true if a file or directory exists.
func Exist(name string) bool {
_, err := os.Stat(name)
return err == nil
ok, _ := PathExists(name)
return ok
}

// ParseVersionNum split version from release or tag
Expand Down
139 changes: 138 additions & 1 deletion pkg/common/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package common
import (
"fmt"
"os"
"path"
"testing"
"time"

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

func TestGetOrDefault(t *testing.T) {
Expand Down Expand Up @@ -129,3 +131,138 @@ func TestGetEnvironment(t *testing.T) {
})
}
}

func TestExist(t *testing.T) {
tests := []struct {
name string
createFilepath func(*testing.T) string
wantResult bool
}{{
name: "a existing regular file",
createFilepath: func(t *testing.T) string {
var f *os.File
var err error
f, err = os.CreateTemp(os.TempDir(), "fake")
assert.Nil(t, err)
assert.NotNil(t, f)
return f.Name()
},
wantResult: true,
}, {
name: "a existing directory",
createFilepath: func(t *testing.T) string {
dir, err := os.MkdirTemp(os.TempDir(), "fake")
assert.Nil(t, err)
assert.NotEmpty(t, dir)
return dir
},
wantResult: true,
}, {
name: "non-exsit regular file",
createFilepath: func(t *testing.T) string {
return path.Join(os.TempDir(), fmt.Sprintf("%d", time.Now().Nanosecond()))
},
wantResult: false,
}}
for i, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
filepath := tt.createFilepath(t)
if filepath != "" {
defer func() {
_ = os.RemoveAll(filepath)
}()
}

ok := Exist(filepath)
if tt.wantResult {
assert.True(t, ok, "should exist in case [%d]-[%s]", i, tt.name)
} else {
assert.False(t, ok, "should not exist in case [%d]-[%s]", i, tt.name)
}
})
}
}

func TestParseVersionNum(t *testing.T) {
tests := []struct {
name string
version string
expect string
}{{
name: "version start with v",
version: "v1.2.3",
expect: "1.2.3",
}, {
name: "version has not prefix v",
version: "1.2.3",
expect: "1.2.3",
}, {
name: "have more prefix",
version: "alpha-v1.2.3",
expect: "1.2.3",
}}
for i, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
version := ParseVersionNum(tt.version)
assert.Equal(t, tt.expect, version, "expect [%s], got [%s] in case [%d]", tt.expect, version, i)
})
}
}

func TestIsDirWriteable(t *testing.T) {
tests := []struct {
name string
dir string
wantErr bool
}{{
name: "should writeable",
dir: os.TempDir(),
wantErr: false,
}, {
name: "should not writable",
dir: path.Join(os.TempDir(), "fake", "dir"),
wantErr: true,
}}
for i, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := IsDirWriteable(tt.dir)
if tt.wantErr {
assert.NotNil(t, err, "expect error, but not in case [%d]", i)
} else {
assert.Nil(t, err, "expect not error, but have in case [%d]", i)
}
})
}
}

func TestCheckDirPermission(t *testing.T) {
tests := []struct {
name string
dir string
perm os.FileMode
wantErr bool
}{{
name: "dir is empty",
dir: "",
wantErr: true,
}, {
name: "non-exsiting dir",
dir: path.Join(os.TempDir(), "fake"),
wantErr: true,
}, {
name: "have permission",
perm: os.FileMode(0777),
dir: os.TempDir(),
wantErr: false,
}}
for i, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := CheckDirPermission(tt.dir, tt.perm)
if tt.wantErr {
assert.NotNil(t, err, "expect error, but not in case [%d]", i)
} else {
assert.Nil(t, err, "expect not error, but have in case [%d]", i)
}
})
}
}
67 changes: 67 additions & 0 deletions pkg/installer/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,3 +440,70 @@ func TestCheckDepAndInstall(t *testing.T) {
})
assert.Nil(t, err)
}

func TestIsSupport(t *testing.T) {
type args struct {
cfg HDConfig
}
tests := []struct {
name string
args args
want bool
}{{
name: "support case",
args: args{
cfg: HDConfig{
SupportOS: []string{runtime.GOOS},
SupportArch: []string{runtime.GOARCH},
},
},
want: true,
}, {
name: "not os and arch setting",
want: true,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, IsSupport(tt.args.cfg), "IsSupport(%v)", tt.args.cfg)
})
}
}

func Test_getVersionOrDefault(t *testing.T) {
type args struct {
version string
defaultVer string
}
tests := []struct {
name string
args args
wantTarget string
prepare func(string)
}{{
name: "version is not a HTTP",
args: args{
version: "v1.2.3",
defaultVer: "1.2.3",
},
wantTarget: "1.2.3",
}, {
name: "version is a HTTP address",
args: args{
version: "https://foo.com/",
defaultVer: "",
},
wantTarget: "v1.2.3",
prepare: func(address string) {
gock.New(address).Get("/").Reply(http.StatusOK).BodyString("v1.2.3")
},
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer gock.Off()
if tt.prepare != nil {
tt.prepare(tt.args.version)
}
assert.Equalf(t, tt.wantTarget, getVersionOrDefault(tt.args.version, tt.args.defaultVer), "getVersionOrDefault(%v, %v)", tt.args.version, tt.args.defaultVer)
})
}
}
35 changes: 35 additions & 0 deletions pkg/installer/process_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package installer

import (
"io/ioutil"
"os"
"path"
"testing"

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

func TestInstallerExtractFiles(t *testing.T) {
installer := &Installer{}

assert.NotNil(t, installer.extractFiles("fake.fake", ""))
assert.NotNil(t, installer.extractFiles("a.tar.gz", ""))
}

func TestOverwriteBinary(t *testing.T) {
installer := &Installer{}

sourceFile := path.Join(os.TempDir(), "fake-1")
targetFile := path.Join(os.TempDir(), "fake-2")

ioutil.WriteFile(sourceFile, []byte("fake"), 0600)

defer func() {
_ = os.RemoveAll(sourceFile)
}()
defer func() {
_ = os.RemoveAll(targetFile)
}()

assert.Nil(t, installer.OverWriteBinary(sourceFile, targetFile))
}

0 comments on commit cdc9c8d

Please sign in to comment.