@@ -2,12 +2,14 @@ package command
22
33import (
44 "context"
5+ "github.com/antchfx/jsonquery"
56 "github.com/antchfx/xpath"
67 "github.com/urfave/cli/v3"
78 "github.com/vearne/autotest/internal/resource"
89 "github.com/vearne/autotest/internal/rule"
910 slog "github.com/vearne/simplelog"
1011 "github.com/vearne/zaplog"
12+ "strings"
1113)
1214
1315func RunTestCases (ctx context.Context , cmd * cli.Command ) error {
@@ -57,6 +59,7 @@ func ValidateConfig(ctx context.Context, cmd *cli.Command) error {
5759 }
5860
5961 slog .Info ("=== validate config file ===" )
62+ slog .Info ("1. check xpath" )
6063 for filePath , testcases := range resource .HttpTestCases {
6164 slog .Info ("filePath:%v, len(testcases):%v" , filePath , len (testcases ))
6265 for _ , tc := range testcases {
@@ -70,7 +73,7 @@ func ValidateConfig(ctx context.Context, cmd *cli.Command) error {
7073 return err
7174 }
7275 case "HttpBodyAtLeastOneRule" :
73- rule := r .(* rule.HttpBodyEqualRule )
76+ rule := r .(* rule.HttpBodyAtLeastOneRule )
7477 _ , err := xpath .Compile (rule .Xpath )
7578 if err != nil {
7679 slog .Error ("rule error, testCaseId:%v, xpath:%v" , tc .ID , rule .Xpath )
@@ -82,5 +85,48 @@ func ValidateConfig(ctx context.Context, cmd *cli.Command) error {
8285 }
8386 }
8487 }
88+
89+ slog .Info ("2. check if ID is duplicate" )
90+ for filePath , testcases := range resource .HttpTestCases {
91+ slog .Info ("filePath:%v, len(testcases):%v" , filePath , len (testcases ))
92+ exist := make (map [uint64 ]struct {})
93+ for _ , tc := range testcases {
94+ _ , ok := exist [tc .ID ]
95+ if ok {
96+ slog .Error ("filePath:%v, ID [%v] is duplicate" , filePath , tc .ID )
97+ break
98+ }
99+ exist [tc .ID ] = struct {}{}
100+ }
101+ }
102+ return nil
103+ }
104+
105+ func ExtractXpath (ctx context.Context , cmd * cli.Command ) error {
106+ // 检查testcase的xpath语法是否正确
107+
108+ xpathStr := cmd .String ("xpath" )
109+ slog .Info ("xpathStr:%v" , xpathStr )
110+
111+ jsonStr := cmd .String ("json" )
112+ slog .Info ("jsonStr:%v" , jsonStr )
113+
114+ _ , err := xpath .Compile (xpathStr )
115+ if err != nil {
116+ slog .Error ("xpath syntax error" )
117+ return nil
118+ }
119+
120+ doc , err := jsonquery .Parse (strings .NewReader (jsonStr ))
121+ if err != nil {
122+ slog .Error ("jsonStr format error" )
123+ return nil
124+ }
125+ nodes := jsonquery .Find (doc , xpathStr )
126+ for idx , node := range nodes {
127+ if node != nil {
128+ slog .Info ("[%v] = %v" , idx , node .Value ())
129+ }
130+ }
85131 return nil
86132}
0 commit comments