-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathparams.go
More file actions
365 lines (315 loc) · 11.8 KB
/
params.go
File metadata and controls
365 lines (315 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package testing
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"sort"
"strings"
)
// staticFilesPath returns the path to static test fixtures for a language.
func staticFilesPath(repoRoot, language string) string {
return filepath.Join(repoRoot, "server", "ql", language, "examples")
}
// buildToolParams constructs tool parameters based on the tool name, test case,
// and fixture directory contents. This replicates the old JS runner's
// getToolSpecificParams() function.
func buildToolParams(repoRoot, toolName, testCase, testDir string) (map[string]any, error) {
beforeDir := filepath.Join(testDir, "before")
staticPath := staticFilesPath(repoRoot, "javascript")
// First check test-config.json
configPath := filepath.Join(testDir, "test-config.json")
if configData, err := os.ReadFile(configPath); err == nil {
var config TestConfig
if err := json.Unmarshal(configData, &config); err != nil {
return nil, fmt.Errorf("parse test-config.json: %w", err)
}
// SARIF tools need derived path injection from before/ even when
// test-config.json provides base params — continue to tool-specific logic.
if !isSARIFTool(toolName) {
return config.Arguments, nil
}
}
// Check monitoring-state.json for embedded parameters
monitoringPath := filepath.Join(beforeDir, "monitoring-state.json")
if data, err := os.ReadFile(monitoringPath); err == nil {
var state map[string]any
if err := json.Unmarshal(data, &state); err == nil {
if params, ok := state["parameters"].(map[string]any); ok && len(params) > 0 {
return params, nil
}
}
}
// Tool-specific parameter generation
params := make(map[string]any)
switch toolName {
case "codeql_lsp_diagnostics":
params["ql_code"] = `from UndefinedType x where x = "test" select x, "semantic error"`
case "codeql_bqrs_decode":
bqrsFile := filepath.Join(staticPath, "src", "ExampleQuery1", "ExampleQuery1.test.bqrs")
if !fileExists(bqrsFile) {
return nil, fmt.Errorf("static BQRS file not found: %s", bqrsFile)
}
params["files"] = []string{bqrsFile}
params["format"] = "json"
case "codeql_bqrs_info":
bqrsFile := filepath.Join(staticPath, "src", "ExampleQuery1", "ExampleQuery1.test.bqrs")
if !fileExists(bqrsFile) {
return nil, fmt.Errorf("static BQRS file not found: %s", bqrsFile)
}
params["file"] = bqrsFile
case "codeql_bqrs_interpret":
bqrsFiles := findFilesByExt(beforeDir, ".bqrs")
if len(bqrsFiles) == 0 {
return nil, fmt.Errorf("no .bqrs files in %s", beforeDir)
}
params["file"] = filepath.Join(beforeDir, bqrsFiles[0])
afterDir := filepath.Join(testDir, "after")
if strings.Contains(testCase, "graphtext") {
params["format"] = "graphtext"
params["output"] = filepath.Join(afterDir, "output.txt")
params["t"] = []string{"kind=graph", "id=test/query"}
} else if strings.Contains(testCase, "sarif") {
params["format"] = "sarif-latest"
params["output"] = filepath.Join(afterDir, "results.sarif")
params["t"] = []string{"kind=problem", "id=test/query"}
} else {
params["format"] = "graphtext"
params["output"] = filepath.Join(afterDir, "output.txt")
params["t"] = []string{"kind=graph", "id=test/query"}
}
case "codeql_test_extract":
testDirPath := filepath.Join(staticPath, "test", "ExampleQuery1")
if !fileExists(testDirPath) {
return nil, fmt.Errorf("static test directory not found: %s", testDirPath)
}
params["tests"] = []string{testDirPath}
case "codeql_test_run":
testDirPath := filepath.Join(staticPath, "test", "ExampleQuery1")
if !fileExists(testDirPath) {
return nil, fmt.Errorf("static test directory not found: %s", testDirPath)
}
params["tests"] = []string{testDirPath}
case "codeql_query_run":
queryFile := filepath.Join(staticPath, "src", "ExampleQuery1", "ExampleQuery1.ql")
databaseDir := filepath.Join(staticPath, "test", "ExampleQuery1", "ExampleQuery1.testproj")
if !fileExists(queryFile) {
return nil, fmt.Errorf("static query file not found: %s", queryFile)
}
params["query"] = queryFile
if fileExists(databaseDir) {
params["database"] = databaseDir
}
case "codeql_query_format":
qlFiles := findFilesByExt(beforeDir, ".ql")
if len(qlFiles) == 0 {
return nil, fmt.Errorf("no .ql files in %s", beforeDir)
}
params["files"] = []string{filepath.Join(beforeDir, qlFiles[0])}
params["check-only"] = true
case "codeql_query_compile":
queryFile := filepath.Join(staticPath, "src", "ExampleQuery1", "ExampleQuery1.ql")
if !fileExists(queryFile) {
return nil, fmt.Errorf("static query file not found: %s", queryFile)
}
params["query"] = queryFile
case "codeql_pack_ls":
params["dir"] = filepath.Join(staticPath, "src")
case "codeql_pack_install":
params["packDir"] = filepath.Join(staticPath, "src")
case "codeql_resolve_library-path", "codeql_resolve_library_path":
queryFile := filepath.Join(staticPath, "src", "ExampleQuery1", "ExampleQuery1.ql")
if !fileExists(queryFile) {
return nil, fmt.Errorf("static query file not found: %s", queryFile)
}
params["query"] = queryFile
case "codeql_resolve_metadata":
qlFiles := findFilesByExt(beforeDir, ".ql")
if len(qlFiles) == 0 {
return nil, fmt.Errorf("no .ql files in %s", beforeDir)
}
params["query"] = filepath.Join(beforeDir, qlFiles[0])
case "codeql_resolve_queries":
params["directory"] = beforeDir
case "codeql_resolve_tests":
params["tests"] = []string{beforeDir}
case "codeql_test_accept":
params["tests"] = []string{beforeDir}
case "codeql_resolve_files":
params["path"] = filepath.Join(staticPath, "src")
params["include-extension"] = ".ql"
case "codeql_resolve_languages":
// No params needed
case "codeql_database_create":
params["language"] = "javascript"
params["source-root"] = filepath.Join(staticPath, "test", "ExampleQuery1")
params["output"] = filepath.Join(repoRoot, ".tmp", "test-db-create")
case "codeql_database_analyze":
databaseDir := filepath.Join(staticPath, "test", "ExampleQuery1", "ExampleQuery1.testproj")
params["database"] = databaseDir
params["output"] = filepath.Join(repoRoot, ".tmp", "test-analyze-output.sarif")
params["format"] = "sarif-latest"
case "codeql_resolve_database":
databaseDir := filepath.Join(staticPath, "test", "ExampleQuery1", "ExampleQuery1.testproj")
params["database"] = databaseDir
case "codeql_generate_query-help":
queryFile := filepath.Join(staticPath, "src", "ExampleQuery1", "ExampleQuery1.ql")
params["query"] = queryFile
params["format"] = "markdown"
case "validate_codeql_query":
params["query"] = "from int i select i"
params["language"] = "java"
case "find_class_position":
qlFiles := findFilesByExt(beforeDir, ".ql")
if len(qlFiles) == 0 {
return nil, fmt.Errorf("no .ql files in %s", beforeDir)
}
params["file"] = filepath.Join(beforeDir, qlFiles[0])
params["name"] = "TestClass"
case "find_predicate_position":
qlFiles := findFilesByExt(beforeDir, ".ql")
if len(qlFiles) == 0 {
return nil, fmt.Errorf("no .ql files in %s", beforeDir)
}
params["file"] = filepath.Join(beforeDir, qlFiles[0])
params["name"] = "testPredicate"
case "find_codeql_query_files":
qlFiles := findFilesByExt(beforeDir, ".ql")
if len(qlFiles) > 0 {
params["queryPath"] = filepath.Join(beforeDir, qlFiles[0])
}
case "create_codeql_query":
params["language"] = "javascript"
params["queryName"] = "TestQuery"
params["outputDir"] = filepath.Join(repoRoot, ".tmp", "test-create-query")
case "search_ql_code":
params["pattern"] = "select"
params["path"] = filepath.Join(staticPath, "src")
case "quick_evaluate":
qlFiles := findFilesByExt(beforeDir, ".ql")
if len(qlFiles) > 0 {
params["file"] = filepath.Join(beforeDir, qlFiles[0])
}
case "read_database_source":
databaseDir := filepath.Join(staticPath, "test", "ExampleQuery1", "ExampleQuery1.testproj")
params["database"] = databaseDir
case "register_database":
databaseDir := filepath.Join(staticPath, "test", "ExampleQuery1", "ExampleQuery1.testproj")
params["databasePath"] = databaseDir
case "list_codeql_databases":
// No params needed (uses configured base dirs)
case "list_query_run_results":
// No params needed (uses configured dirs)
case "session_calculate_current_score":
params["sessionId"] = "test-session-score"
case "session_end":
params["sessionId"] = "test-session-end"
params["status"] = "completed"
case "sessions_compare":
params["sessionIds"] = []string{"session-1", "session-2"}
case "sarif_extract_rule", "sarif_list_rules", "sarif_rule_to_markdown":
sarifFiles := findFilesByExt(beforeDir, ".sarif")
if len(sarifFiles) > 0 {
params["sarifPath"] = filepath.Join(beforeDir, sarifFiles[0])
}
// Merge ruleId from test-config.json if present
if configData, err := os.ReadFile(configPath); err == nil {
var cfg TestConfig
if json.Unmarshal(configData, &cfg) == nil {
if ruleID, ok := cfg.Arguments["ruleId"]; ok {
params["ruleId"] = ruleID
}
}
}
case "sarif_compare_alerts":
sarifFiles := findFilesByExt(beforeDir, ".sarif")
if len(sarifFiles) > 0 {
sarifPath := filepath.Join(beforeDir, sarifFiles[0])
if configData, err := os.ReadFile(configPath); err == nil {
var cfg TestConfig
if json.Unmarshal(configData, &cfg) == nil {
args := cfg.Arguments
alertA, _ := args["alertA"].(map[string]any)
alertB, _ := args["alertB"].(map[string]any)
if alertA != nil {
alertA["sarifPath"] = sarifPath
params["alertA"] = alertA
}
if alertB != nil {
alertB["sarifPath"] = sarifPath
params["alertB"] = alertB
}
if mode, ok := args["overlapMode"]; ok {
params["overlapMode"] = mode
}
}
}
}
case "sarif_diff_by_commits":
sarifFiles := findFilesByExt(beforeDir, ".sarif")
if len(sarifFiles) > 0 {
params["sarifPath"] = filepath.Join(beforeDir, sarifFiles[0])
}
// Merge refRange, repoPath, and granularity from test-config.json
if configData, err := os.ReadFile(configPath); err == nil {
var cfg TestConfig
if json.Unmarshal(configData, &cfg) == nil {
if refRange, ok := cfg.Arguments["refRange"]; ok {
params["refRange"] = refRange
}
if repoPath, ok := cfg.Arguments["repoPath"]; ok {
params["repoPath"] = repoPath
}
if granularity, ok := cfg.Arguments["granularity"]; ok {
params["granularity"] = granularity
}
}
}
case "sarif_diff_runs":
sarifFiles := findFilesByExt(beforeDir, ".sarif")
sort.Strings(sarifFiles)
if len(sarifFiles) >= 2 {
params["sarifPathA"] = filepath.Join(beforeDir, sarifFiles[0])
params["sarifPathB"] = filepath.Join(beforeDir, sarifFiles[1])
} else if len(sarifFiles) == 1 {
params["sarifPathA"] = filepath.Join(beforeDir, sarifFiles[0])
params["sarifPathB"] = filepath.Join(beforeDir, sarifFiles[0])
}
if configData, err := os.ReadFile(configPath); err == nil {
var cfg TestConfig
if json.Unmarshal(configData, &cfg) == nil {
if labelA, ok := cfg.Arguments["labelA"]; ok {
params["labelA"] = labelA
}
if labelB, ok := cfg.Arguments["labelB"]; ok {
params["labelB"] = labelB
}
}
}
default:
// For annotation_*, audit_*, query_results_cache_*, profile_* tools:
// try test-config.json only (already handled above)
return nil, fmt.Errorf("no parameter logic for tool %q test %q", toolName, testCase)
}
return params, nil
}
// isSARIFTool returns true for tools that need SARIF file path injection
// from the before/ directory even when test-config.json provides base params.
func isSARIFTool(name string) bool {
return strings.HasPrefix(name, "sarif_")
}
// findFilesByExt returns filenames in dir matching the given extension.
func findFilesByExt(dir, ext string) []string {
entries, err := os.ReadDir(dir)
if err != nil {
return nil
}
var matches []string
for _, e := range entries {
if !e.IsDir() && strings.HasSuffix(e.Name(), ext) {
matches = append(matches, e.Name())
}
}
sort.Strings(matches)
return matches
}