Skip to content

Commit

Permalink
test: 使用 testify 替代旧的测试断言
Browse files Browse the repository at this point in the history
  • Loading branch information
arrebole committed Apr 14, 2023
1 parent 550069d commit dcfdf20
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 148 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@ jobs:
run: go mod download
- name: Test
run: |
go build -v ./cmd/upx.go
go test -v ./...
55 changes: 30 additions & 25 deletions basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,68 +5,71 @@ import (
"os"
"strings"
"testing"

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

func TestLoginAndLogout(t *testing.T) {

b, err := Upx("login", BUCKET_1, USERNAME, PASSWORD)
Nil(t, err)
Equal(t, string(b), fmt.Sprintf("Welcome to %s, %s!\n", BUCKET_1, USERNAME))
assert.NoError(t, err)
assert.Equal(t, string(b), fmt.Sprintf("Welcome to %s, %s!\n", BUCKET_1, USERNAME))

b, err = Upx("login", BUCKET_2, USERNAME, PASSWORD)
Nil(t, err)
Equal(t, string(b), fmt.Sprintf("Welcome to %s, %s!\n", BUCKET_2, USERNAME))
assert.NoError(t, err)
assert.Equal(t, string(b), fmt.Sprintf("Welcome to %s, %s!\n", BUCKET_2, USERNAME))

b, err = Upx("logout")
Nil(t, err)
Equal(t, string(b), fmt.Sprintf("Goodbye %s/%s ~~\n", USERNAME, BUCKET_2))
assert.NoError(t, err)
assert.Equal(t, string(b), fmt.Sprintf("Goodbye %s/%s ~~\n", USERNAME, BUCKET_2))

b, err = Upx("logout")
Nil(t, err)
Equal(t, string(b), fmt.Sprintf("Goodbye %s/%s ~~\n", USERNAME, BUCKET_1))
assert.NoError(t, err)
assert.Equal(t, string(b), fmt.Sprintf("Goodbye %s/%s ~~\n", USERNAME, BUCKET_1))
}

func TestGetInfo(t *testing.T) {
SetUp()
defer TearDown()
pwd, _ := Upx("pwd")
b, err := Upx("info")
Nil(t, err)
assert.NoError(t, err)
s := []string{
"ServiceName: " + BUCKET_1,
"Operator: " + USERNAME,
"CurrentDir: " + strings.TrimRight(string(pwd), "\n"),
"Usage: ",
}
Equal(t, strings.HasPrefix(string(b), strings.Join(s, "\n")), true)
assert.Equal(t, strings.HasPrefix(string(b), strings.Join(s, "\n")), true)
}

func TestSessionsAndSwitch(t *testing.T) {
SetUp()
defer TearDown()
b, err := Upx("sessions")
Nil(t, err)
Equal(t, string(b), fmt.Sprintf("> %s\n", BUCKET_1))
assert.NoError(t, err)
assert.Equal(t, string(b), fmt.Sprintf("> %s\n", BUCKET_1))

Upx("login", BUCKET_2, USERNAME, PASSWORD)
b, err = Upx("sessions")
Nil(t, err)
Equal(t, string(b), fmt.Sprintf(" %s\n> %s\n", BUCKET_1, BUCKET_2))
assert.NoError(t, err)
assert.Equal(t, string(b), fmt.Sprintf(" %s\n> %s\n", BUCKET_1, BUCKET_2))

Upx("switch", BUCKET_1)
b, err = Upx("sessions")
Nil(t, err)
Equal(t, string(b), fmt.Sprintf("> %s\n %s\n", BUCKET_1, BUCKET_2))
assert.NoError(t, err)
assert.Equal(t, string(b), fmt.Sprintf("> %s\n %s\n", BUCKET_1, BUCKET_2))

pwd, _ := Upx("pwd")
b, err = Upx("info")
Nil(t, err)
assert.NoError(t, err)
s := []string{
"ServiceName: " + BUCKET_1,
"Operator: " + USERNAME,
"CurrentDir: " + strings.TrimRight(string(pwd), "\n"),
"Usage: ",
}
Equal(t, strings.HasPrefix(string(b), strings.Join(s, "\n")), true)
assert.Equal(t, strings.HasPrefix(string(b), strings.Join(s, "\n")), true)
}

// TODO
Expand All @@ -77,12 +80,14 @@ func TestPurge(t *testing.T) {
SetUp()
defer TearDown()
b, err := Upx("purge", fmt.Sprintf("http://%s.b0.upaiyun.com/test.jpg", BUCKET_1))
Nil(t, err)
Equal(t, len(b), 0)
assert.NoError(t, err)
assert.Equal(t, len(b), 0)

b, err = Upx("purge", "http://www.baidu.com")
NotNil(t, err)
Equal(t, err.Error(), fmt.Sprintf("Purge failed urls:\nhttp://www.baidu.com\ntoo many fails\n"))
_, err = Upx("purge", "http://www.baidu.com")
if !assert.Error(t, err) {
assert.Fail(t, "purge not has return error")
}
assert.Equal(t, err.Error(), "Purge failed urls:\nhttp://www.baidu.com\ntoo many fails\n")

fd, _ := os.Create("list")
fd.WriteString(fmt.Sprintf("http://%s.b0.upaiyun.com/test.jpg\n", BUCKET_1))
Expand All @@ -91,6 +96,6 @@ func TestPurge(t *testing.T) {
fd.Close()

b, err = Upx("purge", "--list", "list")
Nil(t, err)
Equal(t, len(b), 0)
assert.NoError(t, err)
assert.Equal(t, len(b), 0)
}
60 changes: 31 additions & 29 deletions fscmds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"path"
"strings"
"testing"

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

/*
Expand All @@ -24,40 +26,40 @@ func TestMkdirAndCdAndPwd(t *testing.T) {

case1 := path.Join(base, "case1")
b, err := Upx("mkdir", case1)
Nil(t, err)
Equal(t, string(b), "")
assert.NoError(t, err)
assert.Equal(t, string(b), "")

Upx("cd", base)
b, _ = Upx("pwd")
Equal(t, string(b), base+"\n")
assert.Equal(t, string(b), base+"\n")

case2 := path.Join(base, "case2")
b, err = Upx("mkdir", "case2")
Nil(t, err)
Equal(t, string(b), "")
assert.NoError(t, err)
assert.Equal(t, string(b), "")

Upx("cd", "case2")
b, _ = Upx("pwd")
Equal(t, string(b), case2+"\n")
assert.Equal(t, string(b), case2+"\n")

case3 := path.Join(base, "case3")
b, err = Upx("mkdir", "../case3")
Nil(t, err)
Equal(t, string(b), "")
assert.NoError(t, err)
assert.Equal(t, string(b), "")

Upx("cd", "../case3")
b, _ = Upx("pwd")
Equal(t, string(b), case3+"\n")
assert.Equal(t, string(b), case3+"\n")

// check
b, err = Upx("ls", base)
Nil(t, err)
assert.NoError(t, err)
output := string(b)
lines := strings.Split(output, "\n")
Equal(t, len(lines), 4)
Equal(t, strings.Contains(output, " case1\n"), true)
Equal(t, strings.Contains(output, " case2\n"), true)
Equal(t, strings.Contains(output, " case3\n"), true)
assert.Equal(t, len(lines), 4)
assert.Equal(t, strings.Contains(output, " case1\n"), true)
assert.Equal(t, strings.Contains(output, " case2\n"), true)
assert.Equal(t, strings.Contains(output, " case3\n"), true)
}

/*
Expand Down Expand Up @@ -98,35 +100,35 @@ func TestLs(t *testing.T) {
}()

b, err := Upx("ls")
Nil(t, err)
Equal(t, len(strings.Split(string(b), "\n")), len(dirs)+len(files)+1)
assert.NoError(t, err)
assert.Equal(t, len(strings.Split(string(b), "\n")), len(dirs)+len(files)+1)

normal, err := Upx("ls", base)
Nil(t, err)
Equal(t, len(strings.Split(string(normal), "\n")), len(dirs)+len(files)+1)
assert.NoError(t, err)
assert.Equal(t, len(strings.Split(string(normal), "\n")), len(dirs)+len(files)+1)

c := (len(dirs) + len(files)) - 1
limited, err := Upx("ls", "-c", fmt.Sprint(c))
Nil(t, err)
Equal(t, len(strings.Split(string(limited), "\n")), c+1)
assert.NoError(t, err)
assert.Equal(t, len(strings.Split(string(limited), "\n")), c+1)

folders, err := Upx("ls", "-d")
Nil(t, err)
Equal(t, len(strings.Split(string(folders), "\n")), len(dirs)+1)
assert.NoError(t, err)
assert.Equal(t, len(strings.Split(string(folders), "\n")), len(dirs)+1)

c = len(dirs) - 1
lfolders, err := Upx("ls", "-d", "-c", fmt.Sprint(c))
Nil(t, err)
Equal(t, len(strings.Split(string(lfolders), "\n")), c+1)
assert.NoError(t, err)
assert.Equal(t, len(strings.Split(string(lfolders), "\n")), c+1)
for _, line := range strings.Split(string(lfolders), "\n")[0:c] {
Equal(t, strings.HasPrefix(line, "drwxrwxrwx "), true)
assert.Equal(t, strings.HasPrefix(line, "drwxrwxrwx "), true)
}

lfiles, err := Upx("ls", "FILE*")
Nil(t, err)
Equal(t, len(strings.Split(string(lfiles), "\n")), 6)
assert.NoError(t, err)
assert.Equal(t, len(strings.Split(string(lfiles), "\n")), 6)

reversed, err := Upx("ls", "-r", base)
Nil(t, err)
NotEqual(t, string(reversed), string(normal))
assert.NoError(t, err)
assert.NotEqual(t, string(reversed), string(normal))
}
16 changes: 9 additions & 7 deletions get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"sort"
"strings"
"testing"

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

func GetStartBetweenEndFiles(t *testing.T, src, dst, correct, start, end string) {
Expand All @@ -26,7 +28,7 @@ func GetStartBetweenEndFiles(t *testing.T, src, dst, correct, start, end string)
} else {
_, err = Upx("get", src, dst, "--start="+start, "--end="+end)
}
Nil(t, err)
assert.NoError(t, err)
}

/*
Expand All @@ -49,18 +51,18 @@ func TestGetStartBetweenEndFiles(t *testing.T) {
root := strings.Join(strings.Split(ROOT, " "), "-")
base := root + "/get/"
pwd, err := ioutil.TempDir("", "test")
Nil(t, err)
assert.NoError(t, err)
localBase := filepath.Join(pwd, "get")

func() {
SetUp()
err := os.MkdirAll(localBase, 0755)
Nil(t, err)
assert.NoError(t, err)
}()
defer TearDown()

err = os.Chdir(localBase)
Nil(t, err)
assert.NoError(t, err)
Upx("mkdir", base)
Upx("cd", base)

Expand Down Expand Up @@ -123,11 +125,11 @@ func TestGetStartBetweenEndFiles(t *testing.T) {

sort.Strings(tc.real)
sort.Strings(tc.want)
Equal(t, len(tc.real), len(tc.want))
assert.Equal(t, len(tc.real), len(tc.want))

for i := 0; i < len(tc.real); i++ {
log.Println("compare:", tc.real[i], " ", tc.want[i])
Equal(t, tc.real[i], tc.want[i])
assert.Equal(t, tc.real[i], tc.want[i])
}
}
}
Expand All @@ -151,7 +153,7 @@ func localFile(local, up string) []string {
// 递归获取云存储目录文件
func upFile(t *testing.T, up, start, end string) []string {
b, err := Upx("ls", up)
Nil(t, err)
assert.NoError(t, err)

var ups []string
output := strings.TrimRight(string(b), "\n")
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.17
require (
github.com/fatih/color v1.15.0
github.com/gosuri/uiprogress v0.0.1
github.com/stretchr/testify v1.8.1
github.com/syndtr/goleveldb v1.0.0
github.com/upyun/go-sdk/v3 v3.0.4
github.com/urfave/cli v1.22.12
Expand All @@ -13,11 +14,14 @@ require (

require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db // indirect
github.com/gosuri/uilive v0.0.4 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/sys v0.7.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
33 changes: 33 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,51 @@ github.com/upyun/go-sdk/v3 v3.0.4 h1:2DCJa/Yi7/3ZybT9UCPATSzvU3wpPPxhXinNlb1Hi8Q
github.com/upyun/go-sdk/v3 v3.0.4/go.mod h1:P/SnuuwhrIgAVRd/ZpzDWqCsBAf/oHg7UggbAxyZa0E=
github.com/urfave/cli v1.22.12 h1:igJgVw1JdKH+trcLWLeLwZjU9fEfPesQ+9/e4MQ44S8=
github.com/urfave/cli v1.22.12/go.mod h1:sSBEIC79qR6OvcmsD4U3KABeOTxDqQtdDnaFuUN30b8=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
golang.org/x/term v0.7.0 h1:BEvjmm5fURWqcfbSKTdpkDXYBrUS1c0m8agp14W48vQ=
golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68=
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
Expand Down
Loading

0 comments on commit dcfdf20

Please sign in to comment.