Skip to content

Commit cdc9c8d

Browse files
authored
Add unit tests (#284)
* Add unit tests * Add codecov badage in the readme file
1 parent 0f23bd3 commit cdc9c8d

File tree

5 files changed

+243
-3
lines changed

5 files changed

+243
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
[![](http://img.shields.io/badge/godoc-reference-5272B4.svg?style=flat-square)](https://godoc.org/github.com/linuxsuren/http-downloader)
44
[![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)
55
[![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)
6+
[![codecov](https://codecov.io/gh/LinuxSuRen/http-downloader/branch/master/graph/badge.svg?token=Ntc8z2iEQ2)](https://codecov.io/gh/LinuxSuRen/http-downloader)
67
[![Contributors](https://img.shields.io/github/contributors/linuxsuren/http-downloader.svg)](https://github.com/linuxsuren/github-go/graphs/contributors)
78
[![GitHub release](https://img.shields.io/github/release/linuxsuren/http-downloader.svg?label=release)](https://github.com/linuxsuren/github-go/releases/latest)
89
![GitHub All Releases](https://img.shields.io/github/downloads/linuxsuren/http-downloader/total)

pkg/common/util.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ func CheckDirPermission(dir string, perm os.FileMode) error {
7272

7373
// Exist returns true if a file or directory exists.
7474
func Exist(name string) bool {
75-
_, err := os.Stat(name)
76-
return err == nil
75+
ok, _ := PathExists(name)
76+
return ok
7777
}
7878

7979
// ParseVersionNum split version from release or tag

pkg/common/util_test.go

Lines changed: 138 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package common
33
import (
44
"fmt"
55
"os"
6+
"path"
67
"testing"
8+
"time"
79

8-
"github.com/magiconair/properties/assert"
10+
"github.com/stretchr/testify/assert"
911
)
1012

1113
func TestGetOrDefault(t *testing.T) {
@@ -129,3 +131,138 @@ func TestGetEnvironment(t *testing.T) {
129131
})
130132
}
131133
}
134+
135+
func TestExist(t *testing.T) {
136+
tests := []struct {
137+
name string
138+
createFilepath func(*testing.T) string
139+
wantResult bool
140+
}{{
141+
name: "a existing regular file",
142+
createFilepath: func(t *testing.T) string {
143+
var f *os.File
144+
var err error
145+
f, err = os.CreateTemp(os.TempDir(), "fake")
146+
assert.Nil(t, err)
147+
assert.NotNil(t, f)
148+
return f.Name()
149+
},
150+
wantResult: true,
151+
}, {
152+
name: "a existing directory",
153+
createFilepath: func(t *testing.T) string {
154+
dir, err := os.MkdirTemp(os.TempDir(), "fake")
155+
assert.Nil(t, err)
156+
assert.NotEmpty(t, dir)
157+
return dir
158+
},
159+
wantResult: true,
160+
}, {
161+
name: "non-exsit regular file",
162+
createFilepath: func(t *testing.T) string {
163+
return path.Join(os.TempDir(), fmt.Sprintf("%d", time.Now().Nanosecond()))
164+
},
165+
wantResult: false,
166+
}}
167+
for i, tt := range tests {
168+
t.Run(tt.name, func(t *testing.T) {
169+
filepath := tt.createFilepath(t)
170+
if filepath != "" {
171+
defer func() {
172+
_ = os.RemoveAll(filepath)
173+
}()
174+
}
175+
176+
ok := Exist(filepath)
177+
if tt.wantResult {
178+
assert.True(t, ok, "should exist in case [%d]-[%s]", i, tt.name)
179+
} else {
180+
assert.False(t, ok, "should not exist in case [%d]-[%s]", i, tt.name)
181+
}
182+
})
183+
}
184+
}
185+
186+
func TestParseVersionNum(t *testing.T) {
187+
tests := []struct {
188+
name string
189+
version string
190+
expect string
191+
}{{
192+
name: "version start with v",
193+
version: "v1.2.3",
194+
expect: "1.2.3",
195+
}, {
196+
name: "version has not prefix v",
197+
version: "1.2.3",
198+
expect: "1.2.3",
199+
}, {
200+
name: "have more prefix",
201+
version: "alpha-v1.2.3",
202+
expect: "1.2.3",
203+
}}
204+
for i, tt := range tests {
205+
t.Run(tt.name, func(t *testing.T) {
206+
version := ParseVersionNum(tt.version)
207+
assert.Equal(t, tt.expect, version, "expect [%s], got [%s] in case [%d]", tt.expect, version, i)
208+
})
209+
}
210+
}
211+
212+
func TestIsDirWriteable(t *testing.T) {
213+
tests := []struct {
214+
name string
215+
dir string
216+
wantErr bool
217+
}{{
218+
name: "should writeable",
219+
dir: os.TempDir(),
220+
wantErr: false,
221+
}, {
222+
name: "should not writable",
223+
dir: path.Join(os.TempDir(), "fake", "dir"),
224+
wantErr: true,
225+
}}
226+
for i, tt := range tests {
227+
t.Run(tt.name, func(t *testing.T) {
228+
err := IsDirWriteable(tt.dir)
229+
if tt.wantErr {
230+
assert.NotNil(t, err, "expect error, but not in case [%d]", i)
231+
} else {
232+
assert.Nil(t, err, "expect not error, but have in case [%d]", i)
233+
}
234+
})
235+
}
236+
}
237+
238+
func TestCheckDirPermission(t *testing.T) {
239+
tests := []struct {
240+
name string
241+
dir string
242+
perm os.FileMode
243+
wantErr bool
244+
}{{
245+
name: "dir is empty",
246+
dir: "",
247+
wantErr: true,
248+
}, {
249+
name: "non-exsiting dir",
250+
dir: path.Join(os.TempDir(), "fake"),
251+
wantErr: true,
252+
}, {
253+
name: "have permission",
254+
perm: os.FileMode(0777),
255+
dir: os.TempDir(),
256+
wantErr: false,
257+
}}
258+
for i, tt := range tests {
259+
t.Run(tt.name, func(t *testing.T) {
260+
err := CheckDirPermission(tt.dir, tt.perm)
261+
if tt.wantErr {
262+
assert.NotNil(t, err, "expect error, but not in case [%d]", i)
263+
} else {
264+
assert.Nil(t, err, "expect not error, but have in case [%d]", i)
265+
}
266+
})
267+
}
268+
}

pkg/installer/check_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,3 +440,70 @@ func TestCheckDepAndInstall(t *testing.T) {
440440
})
441441
assert.Nil(t, err)
442442
}
443+
444+
func TestIsSupport(t *testing.T) {
445+
type args struct {
446+
cfg HDConfig
447+
}
448+
tests := []struct {
449+
name string
450+
args args
451+
want bool
452+
}{{
453+
name: "support case",
454+
args: args{
455+
cfg: HDConfig{
456+
SupportOS: []string{runtime.GOOS},
457+
SupportArch: []string{runtime.GOARCH},
458+
},
459+
},
460+
want: true,
461+
}, {
462+
name: "not os and arch setting",
463+
want: true,
464+
}}
465+
for _, tt := range tests {
466+
t.Run(tt.name, func(t *testing.T) {
467+
assert.Equalf(t, tt.want, IsSupport(tt.args.cfg), "IsSupport(%v)", tt.args.cfg)
468+
})
469+
}
470+
}
471+
472+
func Test_getVersionOrDefault(t *testing.T) {
473+
type args struct {
474+
version string
475+
defaultVer string
476+
}
477+
tests := []struct {
478+
name string
479+
args args
480+
wantTarget string
481+
prepare func(string)
482+
}{{
483+
name: "version is not a HTTP",
484+
args: args{
485+
version: "v1.2.3",
486+
defaultVer: "1.2.3",
487+
},
488+
wantTarget: "1.2.3",
489+
}, {
490+
name: "version is a HTTP address",
491+
args: args{
492+
version: "https://foo.com/",
493+
defaultVer: "",
494+
},
495+
wantTarget: "v1.2.3",
496+
prepare: func(address string) {
497+
gock.New(address).Get("/").Reply(http.StatusOK).BodyString("v1.2.3")
498+
},
499+
}}
500+
for _, tt := range tests {
501+
t.Run(tt.name, func(t *testing.T) {
502+
defer gock.Off()
503+
if tt.prepare != nil {
504+
tt.prepare(tt.args.version)
505+
}
506+
assert.Equalf(t, tt.wantTarget, getVersionOrDefault(tt.args.version, tt.args.defaultVer), "getVersionOrDefault(%v, %v)", tt.args.version, tt.args.defaultVer)
507+
})
508+
}
509+
}

pkg/installer/process_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package installer
2+
3+
import (
4+
"io/ioutil"
5+
"os"
6+
"path"
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestInstallerExtractFiles(t *testing.T) {
13+
installer := &Installer{}
14+
15+
assert.NotNil(t, installer.extractFiles("fake.fake", ""))
16+
assert.NotNil(t, installer.extractFiles("a.tar.gz", ""))
17+
}
18+
19+
func TestOverwriteBinary(t *testing.T) {
20+
installer := &Installer{}
21+
22+
sourceFile := path.Join(os.TempDir(), "fake-1")
23+
targetFile := path.Join(os.TempDir(), "fake-2")
24+
25+
ioutil.WriteFile(sourceFile, []byte("fake"), 0600)
26+
27+
defer func() {
28+
_ = os.RemoveAll(sourceFile)
29+
}()
30+
defer func() {
31+
_ = os.RemoveAll(targetFile)
32+
}()
33+
34+
assert.Nil(t, installer.OverWriteBinary(sourceFile, targetFile))
35+
}

0 commit comments

Comments
 (0)