Skip to content

Commit fd82658

Browse files
committed
test(git): refactor testcase with goconvey
before: TestInstallSubCmd use with testing library after: git.install and git.util some testcase use goconvey goal: goconvey provide BDD style testcase which allows you write testcase more clearly
1 parent 5d373b3 commit fd82658

File tree

2 files changed

+35
-65
lines changed

2 files changed

+35
-65
lines changed

git/install_test.go

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,27 @@
11
/*
22
* @Author: robert zhang <robertzhangwenjie@gmail.com>
33
* @Date: 2022-08-06 16:52:22
4-
* @LastEditTime: 2022-09-03 17:24:07
4+
* @LastEditTime: 2022-09-04 11:37:33
55
* @LastEditors: robert zhang
66
* @Description:
77
*/
88
package git
99

1010
import (
11-
"os"
12-
"path/filepath"
1311
"testing"
12+
13+
. "github.com/glycerine/goconvey/convey"
1414
)
1515

1616
func TestInstallSubCmd(t *testing.T) {
17-
type args struct {
18-
filePath string
19-
subCmdName string
20-
}
21-
22-
testcases := []struct {
23-
name string
24-
args args
25-
wantErr bool
26-
}{
27-
{
28-
name: "noExistFile",
29-
args: args{
30-
filePath: "/123/456",
31-
subCmdName: "123",
32-
},
33-
wantErr: true,
34-
},
35-
{
36-
name: "normalFile",
37-
args: args{
38-
filePath: filepath.Join("testdata", "testcmd"),
39-
subCmdName: "testcmd",
40-
},
41-
wantErr: false,
42-
},
43-
}
44-
45-
for _, tc := range testcases {
46-
t.Run(tc.name, func(t *testing.T) {
47-
dstFilePath, err := InstallSubCmd(tc.args.filePath, tc.args.subCmdName)
48-
if err == nil {
49-
os.Remove(dstFilePath)
50-
}
51-
if (err != nil) != tc.wantErr {
52-
t.Errorf("InstallSubCmd() error = %v,wantErr %v ", err, tc.wantErr)
53-
}
54-
17+
Convey("Given a file which doesn't exist", t, func() {
18+
filepath := "/123/456"
19+
subCmdName := "123"
20+
Convey("When install it as git subcommand", func() {
21+
_, err := InstallSubCmd(filepath, subCmdName)
22+
Convey("Then should return err", func() {
23+
So(err, ShouldNotBeNil)
24+
})
5525
})
56-
}
26+
})
5727
}

git/util_test.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
/*
22
* @Author: robert zhang <robertzhangwenjie@gmail.com>
33
* @Date: 2022-08-07 18:14:41
4-
* @LastEditTime: 2022-08-10 15:09:22
4+
* @LastEditTime: 2022-09-04 11:20:49
55
* @LastEditors: robert zhang
66
* @Description:
77
*/
88
package git
99

1010
import (
11+
"os"
1112
"os/exec"
13+
"path/filepath"
1214
"strings"
1315
"testing"
16+
17+
. "github.com/glycerine/goconvey/convey"
1418
)
1519

1620
func TestExecPath(t *testing.T) {
@@ -27,29 +31,25 @@ func TestExecPath(t *testing.T) {
2731
}
2832

2933
func TestIsGitRepository(t *testing.T) {
30-
testcases := []struct {
31-
name string
32-
path string
33-
want bool
34-
}{
35-
{
36-
name: "invalid path",
37-
path: "ttttt/",
38-
want: false,
39-
},
40-
{
41-
name: "current path",
42-
path: ".",
43-
want: true,
44-
},
45-
}
34+
Convey("Given a path", t, func() {
35+
var path string
36+
Convey("When it doestn't exist", func() {
37+
path = "testdata/xxx"
38+
Convey("Then should return false", func() {
39+
So(IsGitRepository(path), ShouldBeFalse)
40+
})
41+
})
4642

47-
for _, tc := range testcases {
48-
t.Run(tc.name, func(t *testing.T) {
49-
got := IsGitRepository(tc.path)
50-
if got != tc.want {
51-
t.Errorf("expected %v, got %v", tc.want, got)
43+
Convey("When it's a git repo", func() {
44+
path = filepath.Join("testdata", "gitrepo")
45+
if err := exec.Command("git", "init", path).Run(); err != nil {
46+
t.Errorf("git init failed: %v", err)
5247
}
48+
defer os.RemoveAll(path)
49+
50+
Convey("Then should return true", func() {
51+
So(IsGitRepository(path), ShouldBeTrue)
52+
})
5353
})
54-
}
54+
})
5555
}

0 commit comments

Comments
 (0)