Skip to content

Commit 1d0bd60

Browse files
committed
feat: support to get go mod version
1 parent 19871a5 commit 1d0bd60

File tree

10 files changed

+163
-1
lines changed

10 files changed

+163
-1
lines changed

docs/site/content/zh/latest/tasks/verify.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,30 @@ title = "测试用例验证"
1616
verify:
1717
- len(data.data) == 6
1818
```
19+
20+
## 数组值检查
21+
22+
```yaml
23+
- name: popularHeaders
24+
request:
25+
api: /popularHeaders
26+
expect:
27+
verify:
28+
- any(data.data, {.key == "Content-Type"})
29+
```
30+
31+
[更多用法](https://expr-lang.org/docs/language-definition#any).
32+
33+
## 字符串判断
34+
35+
```yaml
36+
- name: metrics
37+
request:
38+
api: |
39+
{{.param.server}}/metrics
40+
expect:
41+
verify:
42+
- indexOf(data, "atest_execution_count") != -1
43+
```
44+
45+
[更多用法](https://expr-lang.org/docs/language-definition#indexOf).

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ require (
4141
gopkg.in/yaml.v3 v3.0.1
4242
)
4343

44+
require golang.org/x/mod v0.22.0 // indirect
45+
4446
require (
4547
github.com/Masterminds/goutils v1.1.1 // indirect
4648
github.com/Masterminds/semver/v3 v3.2.1 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4
224224
golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
225225
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
226226
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
227+
golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4=
228+
golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
227229
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
228230
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
229231
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=

go.work.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2154,6 +2154,7 @@ google.golang.org/api v0.149.0/go.mod h1:Mwn1B7JTXrzXtnvmzQE2BD6bYZQ8DShKZDZbeN9
21542154
google.golang.org/api v0.155.0 h1:vBmGhCYs0djJttDNynWo44zosHlPvHmA0XiN2zP2DtA=
21552155
google.golang.org/api v0.155.0/go.mod h1:GI5qK5f40kCpHfPn6+YzGAByIKWv8ujFnmoWm7Igduk=
21562156
google.golang.org/api v0.162.0/go.mod h1:6SulDkfoBIg4NFmCuZ39XeeAgSHCPecfSUuDyYlAHs0=
2157+
google.golang.org/appengine v1.6.8 h1:IhEN5q69dyKagZPYMSdIjS2HqprW324FRQZJcGqPAsM=
21572158
google.golang.org/appengine v1.6.8/go.mod h1:1jJ3jBArFh5pcgW8gCtRJnepW8FzD1V44FJffLiz/Ds=
21582159
google.golang.org/cloud v0.0.0-20151119220103-975617b05ea8/go.mod h1:0H1ncTHf11KCFhTc/+EFRbzSCOZx+VUbRMk55Yv5MYk=
21592160
google.golang.org/genproto v0.0.0-20190522204451-c2c4e71fbf69/go.mod h1:z3L6/3dTEVtUr6QSP8miRzeRqwQOioJ9I66odjN4I7s=

main.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
package main
22

33
import (
4+
_ "embed"
5+
"github.com/linuxsuren/api-testing/pkg/version"
46
"os"
57

6-
// _ "github.com/apache/skywalking-go"
78
"github.com/linuxsuren/api-testing/cmd"
89
"github.com/linuxsuren/api-testing/pkg/server"
910
exec "github.com/linuxsuren/go-fake-runtime"
1011
)
1112

1213
func main() {
14+
version.SetMod(goMod)
1315
c := cmd.NewRootCmd(exec.NewDefaultExecer(), server.NewDefaultHTTPServer())
1416
if err := c.Execute(); err != nil {
1517
os.Exit(1)
1618
}
1719
}
20+
21+
//go:embed go.mod
22+
var goMod string

pkg/render/template.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"encoding/hex"
2727
"encoding/json"
2828
"fmt"
29+
"github.com/linuxsuren/api-testing/pkg/version"
2930
"io"
3031
mathrand "math/rand"
3132
"strings"
@@ -193,6 +194,11 @@ func GetAdvancedFuncs() []AdvancedFunc {
193194
return advancedFuncs
194195
}
195196

197+
func GetEngineVersion() (ver string) {
198+
ver, _ = version.GetModVersion("github.com/Masterminds/sprig", "")
199+
return
200+
}
201+
196202
func generateJSONString(fields []string) (result string) {
197203
data := make(map[string]string)
198204
for _, item := range fields {

pkg/render/template_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,8 @@ func TestFuncUsages(t *testing.T) {
289289
assert.NotEmpty(t, usage)
290290
}
291291
}
292+
293+
func TestGetEngineVersion(t *testing.T) {
294+
ver := GetEngineVersion()
295+
assert.Empty(t, ver)
296+
}

pkg/version/mod.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
Copyright 2024 API Testing Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package version
17+
18+
import (
19+
"golang.org/x/mod/modfile"
20+
)
21+
22+
var goMod string
23+
24+
func SetMod(mod string) {
25+
goMod = mod
26+
}
27+
28+
func GetModVersions(mod string) (verMap map[string]string, err error) {
29+
if mod == "" {
30+
mod = goMod
31+
}
32+
33+
var f *modfile.File
34+
f, err = modfile.Parse("go.mod", []byte(mod), nil)
35+
if err != nil {
36+
return
37+
}
38+
39+
verMap = make(map[string]string, len(f.Require))
40+
for _, req := range f.Require {
41+
verMap[req.Mod.Path] = req.Mod.Version
42+
}
43+
return
44+
}
45+
46+
func GetModVersion(name, mod string) (ver string, err error) {
47+
var verMap map[string]string
48+
if verMap, err = GetModVersions(mod); err == nil {
49+
for k, v := range verMap {
50+
if k == name {
51+
ver = v
52+
break
53+
}
54+
}
55+
}
56+
return
57+
}

pkg/version/mod_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Copyright 2024 API Testing Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package version
17+
18+
import (
19+
_ "embed"
20+
"github.com/stretchr/testify/assert"
21+
"testing"
22+
)
23+
24+
func TestGetModVersion(t *testing.T) {
25+
t.Run("empty mod", func(t *testing.T) {
26+
SetMod("")
27+
_, err := GetModVersion("", "")
28+
assert.NoError(t, err)
29+
})
30+
31+
t.Run("a simple mod", func(t *testing.T) {
32+
ver, err := GetModVersion("github.com/a/b", simpleMod)
33+
assert.NoError(t, err)
34+
assert.Equal(t, "v0.0.1", ver)
35+
})
36+
37+
t.Run("not found in mod", func(t *testing.T) {
38+
ver, err := GetModVersion("github.com/a/b/c", simpleMod)
39+
assert.NoError(t, err)
40+
assert.Equal(t, "", ver)
41+
})
42+
43+
t.Run("invalid mod", func(t *testing.T) {
44+
_, err := GetModVersion("github.com/a/b", `invalid`)
45+
assert.Error(t, err)
46+
})
47+
}
48+
49+
//go:embed testdata/go.mod.txt
50+
var simpleMod string

pkg/version/testdata/go.mod.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module github.com/linuxsuren/api-testing
2+
3+
go 1.22.4
4+
5+
require (
6+
github.com/a/b v0.0.1
7+
)

0 commit comments

Comments
 (0)