Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 53 additions & 6 deletions pkg/server/remote_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func (s *server) Run(ctx context.Context, task *TestTask) (reply *HelloReply, er
for key, val := range oldEnv {
os.Setenv(key, val)
}
fmt.Println(reply, err)
}()

switch task.Kind {
Expand Down Expand Up @@ -82,6 +83,7 @@ func (s *server) Run(ctx context.Context, task *TestTask) (reply *HelloReply, er

if targetTestcase != nil {
parentCases := findParentTestCases(targetTestcase, suite)
fmt.Printf("find %d parent cases\n", len(parentCases))
suite.Items = append(parentCases, *targetTestcase)
} else {
err = fmt.Errorf("cannot found testcase %s", task.CaseName)
Expand All @@ -101,6 +103,8 @@ func (s *server) Run(ctx context.Context, task *TestTask) (reply *HelloReply, er
suite.API = result
suite.API = strings.TrimSuffix(suite.API, "/")
} else {
reply.Error = err.Error()
err = nil
return
}

Expand Down Expand Up @@ -138,28 +142,71 @@ func findParentTestCases(testcase *testing.TestCase, suite *testing.TestSuite) (
reg, matchErr := regexp.Compile(`.*\{\{.*\.\w*.*}\}.*`)
targetReg, targetErr := regexp.Compile(`\.\w*`)

expectNames := new(UniqueSlice[string])
if matchErr == nil && targetErr == nil {
expectName := ""
var expectName string
for _, val := range testcase.Request.Header {
if matched := reg.MatchString(val); matched {
expectName = targetReg.FindString(val)
expectName = strings.TrimPrefix(expectName, ".")
break
expectNames.Push(expectName)
}
}

if expectName == "" {
if mached := reg.MatchString(testcase.Request.API); mached {
expectName = targetReg.FindString(testcase.Request.API)
if mached := reg.MatchString(testcase.Request.API); mached {
// remove {{ and }}
if left, leftErr := regexp.Compile(`.*\{\{`); leftErr == nil {
api := left.ReplaceAllString(testcase.Request.API, "")

expectName = targetReg.FindString(api)
expectName = strings.TrimPrefix(expectName, ".")
expectNames.Push(expectName)
}
}

fmt.Println("expect test case names", expectNames.GetAll())
for _, item := range suite.Items {
if item.Name == expectName {
if expectNames.Exist(item.Name) {
testcases = append(testcases, item)
}
}
}
return
}

// UniqueSlice represents an unique slice
type UniqueSlice[T comparable] struct {
data []T
}

// Push pushes an item if it's not exist
func (s *UniqueSlice[T]) Push(item T) *UniqueSlice[T] {
if s.data == nil {
s.data = []T{item}
} else {
for _, it := range s.data {
if it == item {
return s
}
}
s.data = append(s.data, item)
}
return s
}

// Exist checks if the item exist, return true it exists
func (s *UniqueSlice[T]) Exist(item T) bool {
if s.data != nil {
for _, it := range s.data {
if it == item {
return true
}
}
}
return false
}

// GetAll returns all the items
func (s *UniqueSlice[T]) GetAll() []T {
return s.data
}
43 changes: 43 additions & 0 deletions pkg/server/remote_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,43 @@ func TestFindParentTestCases(t *testing.T) {
expect: []atesting.TestCase{{
Name: "login",
}},
}, {
name: "nest dep",
testcase: &atesting.TestCase{
Name: "user",
Request: atesting.Request{
API: "/users/{{(index .users 0).name}}",
Header: map[string]string{
"Authorization": "Bearer {{.login.data.access_token}}",
},
},
},
suite: &atesting.TestSuite{
Items: []atesting.TestCase{{
Name: "login",
}, {
Name: "users",
Request: atesting.Request{
API: "/users",
},
}, {
Name: "user",
Request: atesting.Request{
API: "/users/{{(index .users 0).name}}",
Header: map[string]string{
"Authorization": "Bearer {{.login.data.access_token}}",
},
},
}},
},
expect: []atesting.TestCase{{
Name: "login",
}, {
Name: "users",
Request: atesting.Request{
API: "/users",
},
}},
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -116,6 +153,12 @@ func TestFindParentTestCases(t *testing.T) {
}
}

func TestUniqueSlice(t *testing.T) {
uniqueSlice := new(UniqueSlice[string])
uniqueSlice.Push("a").Push("a").Push("b")
assert.Equal(t, []string{"a", "b"}, uniqueSlice.GetAll())
}

//go:embed testdata/simple.yaml
var simpleSuite string

Expand Down