@@ -116,3 +116,95 @@ func TestFindFilesByExt(t *testing.T) {
116116 t .Errorf ("found %d .txt files, want 1" , len (txtFiles ))
117117 }
118118}
119+
120+ func TestIsSARIFTool (t * testing.T ) {
121+ tests := []struct {
122+ name string
123+ want bool
124+ }{
125+ {"sarif_extract_rule" , true },
126+ {"sarif_list_rules" , true },
127+ {"sarif_compare_alerts" , true },
128+ {"sarif_diff_runs" , true },
129+ {"sarif_rule_to_markdown" , true },
130+ {"codeql_query_run" , false },
131+ {"validate_codeql_query" , false },
132+ {"codeql_pack_install" , false },
133+ }
134+ for _ , tt := range tests {
135+ if got := isSARIFTool (tt .name ); got != tt .want {
136+ t .Errorf ("isSARIFTool(%q) = %v, want %v" , tt .name , got , tt .want )
137+ }
138+ }
139+ }
140+
141+ func TestBuildToolParams_SARIFToolWithConfig (t * testing.T ) {
142+ dir := t .TempDir ()
143+ testDir := filepath .Join (dir , "tools" , "sarif_extract_rule" , "extract_sql_injection" )
144+ beforeDir := filepath .Join (testDir , "before" )
145+ os .MkdirAll (beforeDir , 0o755 )
146+ os .MkdirAll (filepath .Join (testDir , "after" ), 0o755 )
147+
148+ // Write test-config.json with ruleId but no sarifPath
149+ os .WriteFile (filepath .Join (testDir , "test-config.json" ),
150+ []byte (`{"toolName":"sarif_extract_rule","arguments":{"ruleId":"js/sql-injection"}}` ), 0o600 )
151+
152+ // Write a SARIF file in before/
153+ os .WriteFile (filepath .Join (beforeDir , "test-input.sarif" ),
154+ []byte (`{"version":"2.1.0"}` ), 0o600 )
155+
156+ params , err := buildToolParams (dir , "sarif_extract_rule" , "extract_sql_injection" , testDir )
157+ if err != nil {
158+ t .Fatalf ("unexpected error: %v" , err )
159+ }
160+
161+ // Should have sarifPath injected from before/
162+ sarifPath , ok := params ["sarifPath" ].(string )
163+ if ! ok || sarifPath == "" {
164+ t .Error ("expected sarifPath to be injected from before/ directory" )
165+ }
166+
167+ // Should still have ruleId from config
168+ if params ["ruleId" ] != "js/sql-injection" {
169+ t .Errorf ("params[ruleId] = %v, want js/sql-injection" , params ["ruleId" ])
170+ }
171+ }
172+
173+ func TestBuildToolParams_SARIFCompareAlertsWithConfig (t * testing.T ) {
174+ dir := t .TempDir ()
175+ testDir := filepath .Join (dir , "tools" , "sarif_compare_alerts" , "sink_overlap" )
176+ beforeDir := filepath .Join (testDir , "before" )
177+ os .MkdirAll (beforeDir , 0o755 )
178+ os .MkdirAll (filepath .Join (testDir , "after" ), 0o755 )
179+
180+ // Write test-config.json with alertA/alertB but no sarifPath
181+ os .WriteFile (filepath .Join (testDir , "test-config.json" ),
182+ []byte (`{"toolName":"sarif_compare_alerts","arguments":{"alertA":{"ruleId":"r1","resultIndex":0},"alertB":{"ruleId":"r2","resultIndex":0},"overlapMode":"sink"}}` ), 0o600 )
183+
184+ // Write a SARIF file in before/
185+ os .WriteFile (filepath .Join (beforeDir , "test-input.sarif" ),
186+ []byte (`{"version":"2.1.0"}` ), 0o600 )
187+
188+ params , err := buildToolParams (dir , "sarif_compare_alerts" , "sink_overlap" , testDir )
189+ if err != nil {
190+ t .Fatalf ("unexpected error: %v" , err )
191+ }
192+
193+ // alertA should have sarifPath injected
194+ alertA , ok := params ["alertA" ].(map [string ]any )
195+ if ! ok {
196+ t .Fatal ("expected alertA in params" )
197+ }
198+ if alertA ["sarifPath" ] == nil || alertA ["sarifPath" ] == "" {
199+ t .Error ("expected sarifPath injected into alertA" )
200+ }
201+
202+ // alertB should have sarifPath injected
203+ alertB , ok := params ["alertB" ].(map [string ]any )
204+ if ! ok {
205+ t .Fatal ("expected alertB in params" )
206+ }
207+ if alertB ["sarifPath" ] == nil || alertB ["sarifPath" ] == "" {
208+ t .Error ("expected sarifPath injected into alertB" )
209+ }
210+ }
0 commit comments