Skip to content

Commit c03085a

Browse files
Remove duplicated function definitions (#1666)
Co-authored-by: Azeem Shaikh <azeems@google.com>
1 parent e5b62b5 commit c03085a

File tree

4 files changed

+19
-174
lines changed

4 files changed

+19
-174
lines changed

checks/fileparser/listing.go

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -196,42 +196,6 @@ func CheckIfFileExistsV6(repoClient clients.RepoClient,
196196
return nil
197197
}
198198

199-
// FileCb represents a callback fn.
200-
type FileCb func(path string,
201-
dl checker.DetailLogger, data FileCbData) (bool, error)
202-
203-
// CheckIfFileExists downloads the tar of the repository and calls the onFile() to check
204-
// for the occurrence.
205-
func CheckIfFileExists(c *checker.CheckRequest, onFile FileCb, data FileCbData) error {
206-
matchedFiles, err := c.RepoClient.ListFiles(func(string) (bool, error) { return true, nil })
207-
if err != nil {
208-
// nolint: wrapcheck
209-
return err
210-
}
211-
for _, filename := range matchedFiles {
212-
continueIter, err := onFile(filename, c.Dlogger, data)
213-
if err != nil {
214-
return err
215-
}
216-
217-
if !continueIter {
218-
break
219-
}
220-
}
221-
222-
return nil
223-
}
224-
225-
// FileGetCbDataAsBoolPointer returns callback data as bool.
226-
func FileGetCbDataAsBoolPointer(data FileCbData) *bool {
227-
pdata, ok := data.(*bool)
228-
if !ok {
229-
// This never happens.
230-
panic("invalid type")
231-
}
232-
return pdata
233-
}
234-
235199
// CheckFileContainsCommands checks if the file content contains commands or not.
236200
// `comment` is the string or character that indicates a comment:
237201
// for example for Dockerfiles, it would be `#`.

checks/fileparser/listing_test.go

Lines changed: 0 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -379,54 +379,6 @@ func Test_isTestdataFile(t *testing.T) {
379379
}
380380
}
381381

382-
// TestFileGetCbDataAsBoolPointer tests the FileGetCbDataAsBoolPointer function.
383-
func TestFileGetCbDataAsBoolPointer(t *testing.T) {
384-
t.Parallel()
385-
type args struct {
386-
data FileCbData
387-
}
388-
b := true
389-
//nolint
390-
tests := []struct {
391-
name string
392-
args args
393-
want *bool
394-
wantPanic bool
395-
}{
396-
{
397-
name: "true",
398-
args: args{
399-
data: &b,
400-
},
401-
want: &b,
402-
},
403-
{
404-
name: "nil",
405-
args: args{},
406-
want: &b,
407-
wantPanic: true,
408-
},
409-
}
410-
for _, tt := range tests {
411-
tt := tt // Re-initializing variable so it is not changed while executing the closure below
412-
t.Run(tt.name, func(t *testing.T) {
413-
t.Parallel()
414-
if tt.wantPanic {
415-
defer func() {
416-
if r := recover(); r == nil {
417-
t.Errorf("FileGetCbDataAsBoolPointer() did not panic")
418-
}
419-
}()
420-
FileGetCbDataAsBoolPointer(tt.args.data)
421-
return
422-
}
423-
if got := FileGetCbDataAsBoolPointer(tt.args.data); got != tt.want {
424-
t.Errorf("FileGetCbDataAsBoolPointer() = %v, want %v", got, tt.want)
425-
}
426-
})
427-
}
428-
}
429-
430382
// TestCheckFilesContentV6 tests the CheckFilesContentV6 function.
431383
func TestCheckFilesContentV6(t *testing.T) {
432384
t.Parallel()
@@ -699,68 +651,3 @@ func TestCheckIfFileExistsV6(t *testing.T) {
699651
})
700652
}
701653
}
702-
703-
// TestCheckIfFileExists tests the CheckIfFileExists function.
704-
func TestCheckIfFileExists(t *testing.T) {
705-
t.Parallel()
706-
//nolint
707-
type args struct {
708-
cbReturn bool
709-
cbErr error
710-
}
711-
//nolint
712-
tests := []struct {
713-
name string
714-
args args
715-
wantErr bool
716-
}{
717-
{
718-
name: "cb true and no error",
719-
args: args{
720-
cbReturn: true,
721-
cbErr: nil,
722-
},
723-
wantErr: false,
724-
},
725-
{
726-
name: "cb false and no error",
727-
args: args{
728-
cbReturn: false,
729-
cbErr: nil,
730-
},
731-
wantErr: false,
732-
},
733-
{
734-
name: "cb error",
735-
args: args{
736-
cbReturn: true,
737-
cbErr: errors.New("test error"),
738-
},
739-
wantErr: true,
740-
},
741-
}
742-
for _, tt := range tests {
743-
tt := tt // Re-initializing variable so it is not changed while executing the closure below
744-
t.Run(tt.name, func(t *testing.T) {
745-
t.Parallel()
746-
747-
ctrl := gomock.NewController(t)
748-
mockRepo := mockrepo.NewMockRepoClient(ctrl)
749-
mockRepo.EXPECT().ListFiles(gomock.Any()).Return([]string{"foo"}, nil).AnyTimes()
750-
c := checker.CheckRequest{
751-
RepoClient: mockRepo,
752-
}
753-
x := func(path string,
754-
dl checker.DetailLogger, data FileCbData) (bool, error) {
755-
return tt.args.cbReturn, tt.args.cbErr
756-
}
757-
758-
err := CheckIfFileExists(&c, x, x)
759-
760-
if (err != nil) != tt.wantErr {
761-
t.Errorf("CheckIfFileExists() error = %v, wantErr %v for %v", err, tt.wantErr, tt.name)
762-
return
763-
}
764-
})
765-
}
766-
}

checks/license.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -103,28 +103,28 @@ func testLicenseCheck(name string) bool {
103103

104104
// LicenseCheck runs LicenseCheck check.
105105
func LicenseCheck(c *checker.CheckRequest) checker.CheckResult {
106-
var r bool
107-
108-
onFile := func(name string, dl checker.DetailLogger, data fileparser.FileCbData) (bool, error) {
109-
pdata := fileparser.FileGetCbDataAsBoolPointer(data)
106+
var s string
110107

108+
onFile := func(name string, data fileparser.FileCbData) (bool, error) {
111109
if checkLicense(name) {
112-
c.Dlogger.Info(&checker.LogMessage{
113-
Path: name,
114-
Type: checker.FileTypeSource,
115-
Offset: 1,
116-
})
117-
*pdata = true
110+
if strData, ok := data.(*string); ok && strData != nil {
111+
*strData = name
112+
}
118113
return false, nil
119114
}
120115
return true, nil
121116
}
122117

123-
err := fileparser.CheckIfFileExists(c, onFile, &r)
118+
err := fileparser.CheckIfFileExistsV6(c.RepoClient, onFile, &s)
124119
if err != nil {
125120
return checker.CreateRuntimeErrorResult(CheckLicense, err)
126121
}
127-
if r {
122+
if s != "" {
123+
c.Dlogger.Info(&checker.LogMessage{
124+
Path: s,
125+
Type: checker.FileTypeSource,
126+
Offset: 1,
127+
})
128128
return checker.CreateMaxScoreResult(CheckLicense, "license file detected")
129129
}
130130
return checker.CreateMinScoreResult(CheckLicense, "license file not detected")

checks/raw/security_policy.go

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func SecurityPolicy(c *checker.CheckRequest) (checker.SecurityPolicyData, error)
3232

3333
// Check repository for repository-specific policy.
3434
// https://docs.github.com/en/github/building-a-strong-community/creating-a-default-community-health-file.
35-
onFile := func(name string, dl checker.DetailLogger, data fileparser.FileCbData) (bool, error) {
35+
onFile := func(name string, data fileparser.FileCbData) (bool, error) {
3636
pfiles, ok := data.(*[]checker.File)
3737
if !ok {
3838
// This never happens.
@@ -62,7 +62,7 @@ func SecurityPolicy(c *checker.CheckRequest) (checker.SecurityPolicyData, error)
6262
}
6363

6464
files := make([]checker.File, 0)
65-
err := fileparser.CheckIfFileExists(c, onFile, &files)
65+
err := fileparser.CheckIfFileExistsV6(c.RepoClient, onFile, &files)
6666
if err != nil {
6767
return checker.SecurityPolicyData{}, err
6868
}
@@ -74,18 +74,12 @@ func SecurityPolicy(c *checker.CheckRequest) (checker.SecurityPolicyData, error)
7474

7575
// https://docs.github.com/en/github/building-a-strong-community/creating-a-default-community-health-file.
7676
logger := log.NewLogger(log.InfoLevel)
77-
dotGitHub := &checker.CheckRequest{
78-
Ctx: c.Ctx,
79-
Dlogger: c.Dlogger,
80-
RepoClient: githubrepo.CreateGithubRepoClient(c.Ctx, logger),
81-
Repo: c.Repo.Org(),
82-
}
83-
84-
err = dotGitHub.RepoClient.InitRepo(dotGitHub.Repo, clients.HeadSHA)
77+
dotGitHubClient := githubrepo.CreateGithubRepoClient(c.Ctx, logger)
78+
err = dotGitHubClient.InitRepo(c.Repo.Org(), clients.HeadSHA)
8579
switch {
8680
case err == nil:
87-
defer dotGitHub.RepoClient.Close()
88-
onFile = func(name string, dl checker.DetailLogger, data fileparser.FileCbData) (bool, error) {
81+
defer dotGitHubClient.Close()
82+
onFile = func(name string, data fileparser.FileCbData) (bool, error) {
8983
pfiles, ok := data.(*[]checker.File)
9084
if !ok {
9185
// This never happens.
@@ -106,7 +100,7 @@ func SecurityPolicy(c *checker.CheckRequest) (checker.SecurityPolicyData, error)
106100
}
107101
return true, nil
108102
}
109-
err = fileparser.CheckIfFileExists(dotGitHub, onFile, &files)
103+
err = fileparser.CheckIfFileExistsV6(dotGitHubClient, onFile, &files)
110104
if err != nil {
111105
return checker.SecurityPolicyData{}, err
112106
}

0 commit comments

Comments
 (0)