@@ -18,8 +18,11 @@ import (
18
18
"fmt"
19
19
"io/ioutil"
20
20
"os"
21
+ "os/exec"
21
22
"strconv"
22
23
"testing"
24
+
25
+ "github.com/google/go-cmp/cmp"
23
26
)
24
27
25
28
//not setting t.Parallel() here because we are mutating the env variables
@@ -482,3 +485,237 @@ func Test_validate(t *testing.T) {
482
485
})
483
486
}
484
487
}
488
+
489
+ func Test_runScorecardSettings (t * testing.T ) {
490
+ t .Parallel ()
491
+ type args struct {
492
+ githubEventName string
493
+ scorecardPolicyFile string
494
+ scorecardResultsFormat string
495
+ scorecardBin string
496
+ scorecardResultsFile string
497
+ githubRepository string
498
+ }
499
+ //nolint
500
+ tests := []struct {
501
+ wantErr bool
502
+ name string
503
+ args args
504
+ want * exec.Cmd
505
+ }{
506
+ {
507
+ name : "Success - scorecardFork set" ,
508
+ args : args {
509
+ githubEventName : "pull_request" ,
510
+ scorecardPolicyFile : "./testdata/scorecard.yaml" ,
511
+ scorecardResultsFormat : "json" ,
512
+ scorecardBin : "scorecard" ,
513
+ scorecardResultsFile : "./testdata/scorecard.json" ,
514
+ githubRepository : "foo/bar" ,
515
+ },
516
+ want : & exec.Cmd {
517
+ Path : "scorecard" ,
518
+ Args : []string {
519
+ "scorecard" ,
520
+ "--policy" ,
521
+ "./testdata/scorecard.yaml" ,
522
+ "--results-format" ,
523
+ "json" ,
524
+ "--results-file" ,
525
+ "./testdata/scorecard.json" ,
526
+ "--repo" ,
527
+ "foo/bar" ,
528
+ },
529
+ },
530
+ },
531
+ {
532
+ name : "Success - scorecardFork set" ,
533
+ args : args {
534
+ githubEventName : "pull_request" ,
535
+ scorecardPolicyFile : "./testdata/scorecard.yaml" ,
536
+ scorecardResultsFormat : "json" ,
537
+ scorecardBin : "scorecard" ,
538
+ scorecardResultsFile : "./testdata/scorecard.json" ,
539
+ githubRepository : "foo/bar" ,
540
+ },
541
+ want : & exec.Cmd {
542
+ Path : "scorecard" ,
543
+ Args : []string {
544
+ "scorecard" ,
545
+ "--policy" ,
546
+ "./testdata/scorecard.yaml" ,
547
+ "--results-format" ,
548
+ "json" ,
549
+ "--results-file" ,
550
+ "./testdata/scorecard.json" ,
551
+ "--repo" ,
552
+ "foo/bar" ,
553
+ },
554
+ },
555
+ },
556
+ {
557
+ name : "Success - scorecardFork set" ,
558
+ args : args {
559
+ githubEventName : "pull_request" ,
560
+ scorecardPolicyFile : "./testdata/scorecard.yaml" ,
561
+ scorecardResultsFormat : "json" ,
562
+ scorecardBin : "scorecard" ,
563
+ scorecardResultsFile : "./testdata/scorecard.json" ,
564
+ githubRepository : "foo/bar" ,
565
+ },
566
+ want : & exec.Cmd {
567
+ Path : "scorecard" ,
568
+ Args : []string {
569
+ "scorecard" ,
570
+ "--policy" ,
571
+ "./testdata/scorecard.yaml" ,
572
+ "--results-format" ,
573
+ "json" ,
574
+ "--results-file" ,
575
+ "./testdata/scorecard.json" ,
576
+ "--repo" ,
577
+ "foo/bar" ,
578
+ },
579
+ },
580
+ },
581
+ {
582
+ name : "Success - scorecardFork set" ,
583
+ args : args {
584
+ githubEventName : "pull_request" ,
585
+ scorecardResultsFormat : "json" ,
586
+ scorecardBin : "scorecard" ,
587
+ scorecardResultsFile : "./testdata/scorecard.json" ,
588
+ githubRepository : "foo/bar" ,
589
+ },
590
+ want : & exec.Cmd {
591
+ Path : "scorecard" ,
592
+ Args : []string {
593
+ "scorecard" ,
594
+ "--results-format" ,
595
+ "json" ,
596
+ "--results-file" ,
597
+ "./testdata/scorecard.json" ,
598
+ "--repo" ,
599
+ "foo/bar" ,
600
+ },
601
+ },
602
+ },
603
+ {
604
+ name : "Success - scorecardFork set" ,
605
+ args : args {
606
+ githubEventName : "pull_request" ,
607
+ scorecardResultsFormat : "json" ,
608
+ scorecardBin : "scorecard" ,
609
+ scorecardResultsFile : "./testdata/scorecard.json" ,
610
+ githubRepository : "foo/bar" ,
611
+ },
612
+ want : & exec.Cmd {
613
+ Path : "scorecard" ,
614
+ Args : []string {
615
+ "scorecard" ,
616
+ "--results-format" ,
617
+ "json" ,
618
+ "--results-file" ,
619
+ "./testdata/scorecard.json" ,
620
+ "--repo" ,
621
+ "foo/bar" ,
622
+ },
623
+ },
624
+ },
625
+ {
626
+ name : "Success - scorecardFork set" ,
627
+ args : args {
628
+ scorecardResultsFormat : "json" ,
629
+ scorecardBin : "scorecard" ,
630
+ scorecardResultsFile : "./testdata/scorecard.json" ,
631
+ githubRepository : "foo/bar" ,
632
+ },
633
+ want : & exec.Cmd {
634
+ Path : "scorecard" ,
635
+ Args : []string {
636
+ "scorecard" ,
637
+ "--results-format" ,
638
+ "json" ,
639
+ "--results-file" ,
640
+ "./testdata/scorecard.json" ,
641
+ "--repo" ,
642
+ "foo/bar" ,
643
+ },
644
+ },
645
+ },
646
+ {
647
+ name : "Success - Branch protection rule" ,
648
+ args : args {
649
+ githubEventName : "branch_protection_rule" ,
650
+ scorecardResultsFormat : "json" ,
651
+ scorecardBin : "scorecard" ,
652
+ scorecardResultsFile : "./testdata/scorecard.json" ,
653
+ githubRepository : "foo/bar" ,
654
+ },
655
+ want : & exec.Cmd {
656
+ Path : "scorecard" ,
657
+ Args : []string {
658
+ "scorecard" ,
659
+ "--results-format" ,
660
+ "json" ,
661
+ "--results-file" ,
662
+ "./testdata/scorecard.json" ,
663
+ "--repo" ,
664
+ "foo/bar" ,
665
+ },
666
+ },
667
+ },
668
+ {
669
+ name : "Success - Branch protection rule" ,
670
+ args : args {
671
+ scorecardPolicyFile : "./testdata/scorecard.yaml" ,
672
+ githubEventName : "branch_protection_rule" ,
673
+ scorecardResultsFormat : "json" ,
674
+ scorecardBin : "scorecard" ,
675
+ scorecardResultsFile : "./testdata/scorecard.json" ,
676
+ githubRepository : "foo/bar" ,
677
+ },
678
+ want : & exec.Cmd {
679
+ Path : "scorecard" ,
680
+ Args : []string {
681
+ "scorecard" ,
682
+ "--policy" ,
683
+ "./testdata/scorecard.yaml" ,
684
+ "--results-format" ,
685
+ "json" ,
686
+ "--results-file" ,
687
+ "./testdata/scorecard.json" ,
688
+ "--repo" ,
689
+ "foo/bar" ,
690
+ },
691
+ },
692
+ },
693
+ {
694
+ name : "Want error - Branch protection rule" ,
695
+ args : args {
696
+ githubEventName : "" ,
697
+ scorecardResultsFormat : "" ,
698
+ scorecardBin : "" ,
699
+ scorecardResultsFile : "" ,
700
+ githubRepository : "" ,
701
+ },
702
+ wantErr : true ,
703
+ },
704
+ }
705
+
706
+ for _ , tt := range tests {
707
+ tt := tt
708
+ t .Run (tt .name , func (t * testing.T ) {
709
+ t .Parallel ()
710
+ got , err := runScorecardSettings (tt .args .githubEventName , tt .args .scorecardPolicyFile ,
711
+ tt .args .scorecardResultsFormat , tt .args .scorecardBin , tt .args .scorecardResultsFile , tt .args .githubRepository )
712
+ if (err != nil ) != tt .wantErr {
713
+ t .Errorf ("runScorecardSettings() error = %v, wantErr %v" , err , tt .wantErr )
714
+ return
715
+ }
716
+ if ! tt .wantErr && cmp .Equal (got .Args , tt .want .Args ) {
717
+ t .Errorf ("runScorecardSettings() = %v, want %v" , got , tt .want )
718
+ }
719
+ })
720
+ }
721
+ }
0 commit comments