Skip to content

Commit

Permalink
test/refact: 重构测试用例 (#83)
Browse files Browse the repository at this point in the history
* chore: 程序入口移动到 cmd 目录

* test: 使用 testify 替代旧的测试断言
  • Loading branch information
arrebole authored Apr 14, 2023
1 parent 0d8de0c commit 0a35090
Show file tree
Hide file tree
Showing 24 changed files with 204 additions and 198 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ jobs:

- name: Build
run: |
go build -v .
go build -v ./cmd/upx.go
./upx -v
2 changes: 1 addition & 1 deletion .github/workflows/goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
uses: actions/setup-go@v2
- name: Build Upx
run: |
go build
go build ./cmd/upx.go
./upx login "${{ env.UPYUN_RELEASE_BUCKET }}" "${{ env.UPYUN_RELEASE_USERNAME }}" "${{ env.UPYUN_RELEASE_PASSWORD }}"
-
name: Run GoReleaser
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,5 @@ jobs:
run: go mod download
- name: Test
run: |
go build .
go build -v ./cmd/upx.go
go test -v ./...
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ PROJ_DIR= $(ROOT)/src/upyun.com
PWD= $(shell pwd)

app:
go build -o $(APP) .
go build -o $(APP) ./cmd/upx.go

test:
go test -v .
Expand Down
59 changes: 32 additions & 27 deletions basic_test.go
Original file line number Diff line number Diff line change
@@ -1,88 +1,93 @@
package main
package upx

import (
"fmt"
"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
// TODO
func TestAuth(t *testing.T) {
}

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)
}
14 changes: 14 additions & 0 deletions cmd/upx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import (
"os"

"github.com/upyun/upx"
)

func main() {
upx.InitProgress()
upx.Progress.Start()
defer upx.Progress.Stop()
upx.CreateUpxApp().Run(os.Args)
}
2 changes: 1 addition & 1 deletion commands.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package upx

import (
"encoding/json"
Expand Down
2 changes: 1 addition & 1 deletion config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package upx

import (
"encoding/base64"
Expand Down
2 changes: 1 addition & 1 deletion db.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package upx

import (
"encoding/json"
Expand Down
86 changes: 44 additions & 42 deletions fscmds_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package main
package upx

import (
"fmt"
"path"
"strings"
"testing"

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

/*
mkdir /path/to/mkdir/case1
cd /path/to/mkdir
mkdir case2
cd case2
mkdir ../case3
cd ../case3
ls /path/to/mkdir
mkdir /path/to/mkdir/case1
cd /path/to/mkdir
mkdir case2
cd case2
mkdir ../case3
cd ../case3
ls /path/to/mkdir
*/
func TestMkdirAndCdAndPwd(t *testing.T) {
SetUp()
Expand All @@ -24,48 +26,48 @@ 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)
}

/*
ls /path/to/file
ls -r /path/to/dir
ls -c 10 /path/to/dir
ls -d /path/to/dir
ls -r -d -c 10 /path/to/dir
ls /path/to/file
ls -r /path/to/dir
ls -c 10 /path/to/dir
ls -d /path/to/dir
ls -r -d -c 10 /path/to/dir
*/
func TestLs(t *testing.T) {
base := ROOT + "/ls"
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))
}
Loading

0 comments on commit 0a35090

Please sign in to comment.