From cdc9c8d1302cffbbe4865e57594de2cb689c587e Mon Sep 17 00:00:00 2001 From: Rick <1450685+LinuxSuRen@users.noreply.github.com> Date: Sun, 21 Aug 2022 20:09:48 +0800 Subject: [PATCH] Add unit tests (#284) * Add unit tests * Add codecov badage in the readme file --- README.md | 1 + pkg/common/util.go | 4 +- pkg/common/util_test.go | 139 +++++++++++++++++++++++++++++++++- pkg/installer/check_test.go | 67 ++++++++++++++++ pkg/installer/process_test.go | 35 +++++++++ 5 files changed, 243 insertions(+), 3 deletions(-) create mode 100644 pkg/installer/process_test.go diff --git a/README.md b/README.md index dc3355b..44e4eb6 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/pkg/common/util.go b/pkg/common/util.go index 4954efa..47a513d 100644 --- a/pkg/common/util.go +++ b/pkg/common/util.go @@ -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 diff --git a/pkg/common/util_test.go b/pkg/common/util_test.go index 778e957..2fdf983 100644 --- a/pkg/common/util_test.go +++ b/pkg/common/util_test.go @@ -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) { @@ -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) + } + }) + } +} diff --git a/pkg/installer/check_test.go b/pkg/installer/check_test.go index 8d6d307..a852432 100644 --- a/pkg/installer/check_test.go +++ b/pkg/installer/check_test.go @@ -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) + }) + } +} diff --git a/pkg/installer/process_test.go b/pkg/installer/process_test.go new file mode 100644 index 0000000..e36eb73 --- /dev/null +++ b/pkg/installer/process_test.go @@ -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)) +}