@@ -24,6 +24,12 @@ import (
2424 mockrepo "github.com/ossf/scorecard/v4/clients/mockclients"
2525)
2626
27+ var (
28+ errInvalidArgType = errors .New ("invalid arg type" )
29+ errInvalidArgLength = errors .New ("invalid arg length" )
30+ errTest = errors .New ("test" )
31+ )
32+
2733func TestIsTemplateFile (t * testing.T ) {
2834 t .Parallel ()
2935
@@ -572,81 +578,108 @@ func TestCheckFilesContent(t *testing.T) {
572578 }
573579}
574580
575- // TestCheckFilesContentV6 tests the CheckFilesContentV6 function.
576- func TestCheckIfFileExistsV6 (t * testing.T ) {
581+ // TestOnAllFilesDo tests the OnAllFilesDo function.
582+ // nolint:gocognit
583+ func TestOnAllFilesDo (t * testing.T ) {
577584 t .Parallel ()
578- //nolint
579- type args struct {
580- cbReturn bool
581- cbwantErr bool
582- listFilesReturnError error
585+
586+ type testArgsFn func (args ... interface {}) bool
587+ validateCountIs := func (count int ) testArgsFn {
588+ return func (args ... interface {}) bool {
589+ if len (args ) == 0 {
590+ return false
591+ }
592+ val , ok := args [0 ].(* int )
593+ if ! ok {
594+ return false
595+ }
596+ return val != nil && * val == count
597+ }
583598 }
584- //nolint
599+
600+ incrementCount := func (path string , args ... interface {}) (bool , error ) {
601+ if len (args ) < 1 {
602+ return false , errInvalidArgLength
603+ }
604+ val , ok := args [0 ].(* int )
605+ if ! ok || val == nil {
606+ return false , errInvalidArgType
607+ }
608+ (* val )++
609+ if len (args ) > 1 {
610+ maxVal , ok := args [1 ].(int )
611+ if ! ok {
612+ return false , errInvalidArgType
613+ }
614+ if * val >= maxVal {
615+ return false , nil
616+ }
617+ }
618+ return true , nil
619+ }
620+ alwaysFail := func (path string , args ... interface {}) (bool , error ) {
621+ return false , errTest
622+ }
623+ // nolint
585624 tests := []struct {
586- name string
587- args args
588- wantErr bool
625+ name string
626+ onFile DoWhileTrueOnFilename
627+ onFileArgs []interface {}
628+ listFiles []string
629+ errListFiles error
630+ err error
631+ testArgs testArgsFn
589632 }{
590633 {
591- name : "cb true and no error" ,
592- args : args {
593- cbReturn : true ,
594- cbwantErr : false ,
595- listFilesReturnError : nil ,
596- },
597- wantErr : false ,
634+ name : "error during ListFiles" ,
635+ errListFiles : errTest ,
636+ err : errTest ,
637+ onFile : alwaysFail ,
598638 },
599639 {
600- name : "cb false and no error" ,
601- args : args {
602- cbReturn : false ,
603- cbwantErr : false ,
604- listFilesReturnError : nil ,
605- },
606- wantErr : false ,
640+ name : "empty ListFiles" ,
641+ listFiles : []string {},
642+ onFile : incrementCount ,
643+ onFileArgs : []interface {}{new (int )},
644+ testArgs : validateCountIs (0 ),
607645 },
608646 {
609- name : "cb wantErr and error" ,
610- args : args {
611- cbReturn : true ,
612- cbwantErr : true ,
613- listFilesReturnError : nil ,
614- },
615- wantErr : true ,
647+ name : "onFile true and no error" ,
648+ listFiles : []string {"foo" , "bar" },
649+ onFile : incrementCount ,
650+ onFileArgs : []interface {}{new (int )},
651+ testArgs : validateCountIs (2 ),
616652 },
617653 {
618- name : "listFilesReturnError and error" ,
619- args : args {
620- cbReturn : true ,
621- cbwantErr : true ,
622- //nolint
623- listFilesReturnError : errors .New ("test error" ),
624- },
625- wantErr : true ,
654+ name : "onFile false and no error" ,
655+ listFiles : []string {"foo" , "bar" },
656+ onFile : incrementCount ,
657+ onFileArgs : []interface {}{new (int ), 1 /*maxVal*/ },
658+ testArgs : validateCountIs (1 ),
659+ },
660+ {
661+ name : "onFile has error" ,
662+ listFiles : []string {"foo" , "bar" },
663+ onFile : alwaysFail ,
664+ err : errTest ,
626665 },
627666 }
628667 for _ , tt := range tests {
629668 tt := tt // Re-initializing variable so it is not changed while executing the closure below
630669 t .Run (tt .name , func (t * testing.T ) {
631670 t .Parallel ()
632671
633- x := func (path string , data FileCbData ) (bool , error ) {
634- if tt .args .cbwantErr {
635- //nolint
636- return false , errors .New ("test error" )
637- }
638- return tt .args .cbReturn , nil
639- }
640-
641672 ctrl := gomock .NewController (t )
642- mockRepo := mockrepo .NewMockRepoClient (ctrl )
643- mockRepo .EXPECT ().ListFiles (gomock .Any ()).Return ([]string {"foo" }, nil ).AnyTimes ()
673+ mockRepoClient := mockrepo .NewMockRepoClient (ctrl )
674+ mockRepoClient .EXPECT ().ListFiles (gomock .Any ()).
675+ Return (tt .listFiles , tt .errListFiles ).AnyTimes ()
644676
645- err := CheckIfFileExistsV6 (mockRepo , x , x )
646-
647- if (err != nil ) != tt .wantErr {
648- t .Errorf ("CheckIfFileExistsV6() error = %v, wantErr %v for %v" , err , tt .wantErr , tt .name )
649- return
677+ err := OnAllFilesDo (mockRepoClient , tt .onFile , tt .onFileArgs ... )
678+ if ! errors .Is (err , tt .err ) {
679+ t .Errorf ("OnAllFilesDo() expected error = %v, got %v" , tt .err , err )
680+ }
681+ if tt .testArgs != nil && ! tt .testArgs (tt .onFileArgs ... ) {
682+ t .Error ("testArgs validation failed" )
650683 }
651684 })
652685 }
0 commit comments