4
4
package cmd
5
5
6
6
import (
7
+ "fmt"
8
+ "io"
7
9
"os"
8
10
"strings"
9
11
"testing"
@@ -21,7 +23,11 @@ type codeqlExecuteScanMockUtils struct {
21
23
22
24
func newCodeqlExecuteScanTestsUtils () codeqlExecuteScanMockUtils {
23
25
utils := codeqlExecuteScanMockUtils {
24
- ExecMockRunner : & mock.ExecMockRunner {},
26
+ ExecMockRunner : & mock.ExecMockRunner {
27
+ Stub : func (call string , stdoutReturn map [string ]string , shouldFailOnCommand map [string ]error , stdout io.Writer ) error {
28
+ return nil
29
+ },
30
+ },
25
31
FilesMock : & mock.FilesMock {},
26
32
HttpClientMock : & mock.HttpClientMock {},
27
33
}
@@ -406,12 +412,13 @@ func TestPrepareCmdForDatabaseCreate(t *testing.T) {
406
412
407
413
func TestPrepareCmdForDatabaseAnalyze (t * testing.T ) {
408
414
t .Parallel ()
415
+ utils := codeqlExecuteScanMockUtils {}
409
416
410
417
t .Run ("No additional flags, no querySuite, sarif format" , func (t * testing.T ) {
411
418
config := & codeqlExecuteScanOptions {
412
419
Database : "codeqlDB" ,
413
420
}
414
- cmd , err := prepareCmdForDatabaseAnalyze (map [string ]string {}, config , "sarif-latest" , "target/codeqlReport.sarif" )
421
+ cmd , err := prepareCmdForDatabaseAnalyze (utils , map [string ]string {}, config , "sarif-latest" , "target/codeqlReport.sarif" )
415
422
assert .NoError (t , err )
416
423
assert .NotEmpty (t , cmd )
417
424
assert .Equal (t , 5 , len (cmd ))
@@ -422,7 +429,7 @@ func TestPrepareCmdForDatabaseAnalyze(t *testing.T) {
422
429
config := & codeqlExecuteScanOptions {
423
430
Database : "codeqlDB" ,
424
431
}
425
- cmd , err := prepareCmdForDatabaseAnalyze (map [string ]string {}, config , "csv" , "target/codeqlReport.csv" )
432
+ cmd , err := prepareCmdForDatabaseAnalyze (utils , map [string ]string {}, config , "csv" , "target/codeqlReport.csv" )
426
433
assert .NoError (t , err )
427
434
assert .NotEmpty (t , cmd )
428
435
assert .Equal (t , 5 , len (cmd ))
@@ -434,7 +441,7 @@ func TestPrepareCmdForDatabaseAnalyze(t *testing.T) {
434
441
Database : "codeqlDB" ,
435
442
QuerySuite : "security.ql" ,
436
443
}
437
- cmd , err := prepareCmdForDatabaseAnalyze (map [string ]string {}, config , "sarif-latest" , "target/codeqlReport.sarif" )
444
+ cmd , err := prepareCmdForDatabaseAnalyze (utils , map [string ]string {}, config , "sarif-latest" , "target/codeqlReport.sarif" )
438
445
assert .NoError (t , err )
439
446
assert .NotEmpty (t , cmd )
440
447
assert .Equal (t , 6 , len (cmd ))
@@ -448,7 +455,7 @@ func TestPrepareCmdForDatabaseAnalyze(t *testing.T) {
448
455
Threads : "1" ,
449
456
Ram : "2000" ,
450
457
}
451
- cmd , err := prepareCmdForDatabaseAnalyze (map [string ]string {}, config , "sarif-latest" , "target/codeqlReport.sarif" )
458
+ cmd , err := prepareCmdForDatabaseAnalyze (utils , map [string ]string {}, config , "sarif-latest" , "target/codeqlReport.sarif" )
452
459
assert .NoError (t , err )
453
460
assert .NotEmpty (t , cmd )
454
461
assert .Equal (t , 8 , len (cmd ))
@@ -465,7 +472,7 @@ func TestPrepareCmdForDatabaseAnalyze(t *testing.T) {
465
472
customFlags := map [string ]string {
466
473
"--threads" : "--threads=2" ,
467
474
}
468
- cmd , err := prepareCmdForDatabaseAnalyze (customFlags , config , "sarif-latest" , "target/codeqlReport.sarif" )
475
+ cmd , err := prepareCmdForDatabaseAnalyze (utils , customFlags , config , "sarif-latest" , "target/codeqlReport.sarif" )
469
476
assert .NoError (t , err )
470
477
assert .NotEmpty (t , cmd )
471
478
assert .Equal (t , 8 , len (cmd ))
@@ -482,7 +489,7 @@ func TestPrepareCmdForDatabaseAnalyze(t *testing.T) {
482
489
customFlags := map [string ]string {
483
490
"-j" : "-j=2" ,
484
491
}
485
- cmd , err := prepareCmdForDatabaseAnalyze (customFlags , config , "sarif-latest" , "target/codeqlReport.sarif" )
492
+ cmd , err := prepareCmdForDatabaseAnalyze (utils , customFlags , config , "sarif-latest" , "target/codeqlReport.sarif" )
486
493
assert .NoError (t , err )
487
494
assert .NotEmpty (t , cmd )
488
495
assert .Equal (t , 8 , len (cmd ))
@@ -499,7 +506,7 @@ func TestPrepareCmdForDatabaseAnalyze(t *testing.T) {
499
506
customFlags := map [string ]string {
500
507
"--no-download" : "--no-download" ,
501
508
}
502
- cmd , err := prepareCmdForDatabaseAnalyze (customFlags , config , "sarif-latest" , "target/codeqlReport.sarif" )
509
+ cmd , err := prepareCmdForDatabaseAnalyze (utils , customFlags , config , "sarif-latest" , "target/codeqlReport.sarif" )
503
510
assert .NoError (t , err )
504
511
assert .NotEmpty (t , cmd )
505
512
assert .Equal (t , 9 , len (cmd ))
@@ -559,21 +566,54 @@ func TestPrepareCmdForUploadResults(t *testing.T) {
559
566
})
560
567
}
561
568
562
- func TestAppendCodeqlQuery (t * testing.T ) {
569
+ func TestAppendCodeqlQuerySuite (t * testing.T ) {
563
570
t .Parallel ()
564
571
565
572
t .Run ("Empty query" , func (t * testing.T ) {
573
+ utils := newCodeqlExecuteScanTestsUtils ()
566
574
cmd := []string {"database" , "analyze" }
567
- query := ""
568
- cmd = appendCodeqlQuery ( cmd , query )
575
+ querySuite := ""
576
+ cmd = appendCodeqlQuerySuite ( utils , cmd , querySuite , "" )
569
577
assert .Equal (t , 2 , len (cmd ))
570
578
})
571
579
572
580
t .Run ("Not empty query" , func (t * testing.T ) {
581
+ utils := newCodeqlExecuteScanTestsUtils ()
582
+ cmd := []string {"database" , "analyze" }
583
+ querySuite := "java-extended.ql"
584
+ cmd = appendCodeqlQuerySuite (utils , cmd , querySuite , "" )
585
+ assert .Equal (t , 3 , len (cmd ))
586
+ })
587
+
588
+ t .Run ("Add prefix to querySuite" , func (t * testing.T ) {
589
+ utils := codeqlExecuteScanMockUtils {
590
+ ExecMockRunner : & mock.ExecMockRunner {
591
+ Stub : func (call string , stdoutReturn map [string ]string , shouldFailOnCommand map [string ]error , stdout io.Writer ) error {
592
+ stdout .Write ([]byte ("test-java-security-extended.qls" ))
593
+ return nil
594
+ },
595
+ },
596
+ }
597
+ cmd := []string {"database" , "analyze" }
598
+ querySuite := "java-security-extended.qls"
599
+ cmd = appendCodeqlQuerySuite (utils , cmd , querySuite , `s/^(java|python)-(security-extended\.qls|security-and-quality\.qls)/test-\1-\2/` )
600
+ assert .Equal (t , 3 , len (cmd ))
601
+ assert .Equal (t , "test-java-security-extended.qls" , cmd [2 ])
602
+ })
603
+
604
+ t .Run ("Don't add prefix to querySuite" , func (t * testing.T ) {
605
+ utils := codeqlExecuteScanMockUtils {
606
+ ExecMockRunner : & mock.ExecMockRunner {
607
+ Stub : func (call string , stdoutReturn map [string ]string , shouldFailOnCommand map [string ]error , stdout io.Writer ) error {
608
+ return fmt .Errorf ("error" )
609
+ },
610
+ },
611
+ }
573
612
cmd := []string {"database" , "analyze" }
574
- query := "java- extended.ql "
575
- cmd = appendCodeqlQuery ( cmd , query )
613
+ querySuite := "php-security- extended.qls "
614
+ cmd = appendCodeqlQuerySuite ( utils , cmd , querySuite , `s/^(java|python)-(security-extended\.qls|security-and-quality\.qls)/test-\1-\2/` )
576
615
assert .Equal (t , 3 , len (cmd ))
616
+ assert .Equal (t , "php-security-extended.qls" , cmd [2 ])
577
617
})
578
618
}
579
619
0 commit comments