scorecard/clients/githubrepo/searchCommits_test.go

80 lines
2.0 KiB
Go
Raw Normal View History

// Copyright 2022 OpenSSF Scorecard Authors
:sparkles: Feature: Improve Dependabot detection through PRs (#2125) * clients: Update client type to add SearchCommits function Signed-off-by: Alvaro Frias Garay <alvaro.frias@eclypsium.com> * checks/raw: Update Dependency Update Tool check to search for commits made by dependabot in default branch Signed-off-by: Alvaro Frias Garay <alvaro.frias@eclypsium.com> * clients/mockclients: Update mock for repoClient to add SearchCommits function mocks Signed-off-by: Alvaro Frias Garay <alvaro.frias@eclypsium.com> * checks: Update unit tests for Dependency Update tool with new feature of SearchCommits Signed-off-by: Alvaro Frias Garay <alvaro.frias@eclypsium.com> * clients/githubrepo: Update SearchCommitsHandler's buildQuery function & add tests Add "author:" to the query. Remove ReplaceAll unused for Author formatting. Signed-off-by: Alvaro Frias Garay <alvaro.frias@eclypsium.com> * clients: Add explanatory comment for SearchCommits Signed-off-by: Alvaro Frias Garay <alvaro.frias@eclypsium.com> * Clients: Update SearchCommits to return []Commit instead of SearchCommitsResponse Signed-off-by: Alvaro Frias Garay <alvaro.frias@eclypsium.com> * checks: Update dependency update tool check according to change by SearchCommits now returning []Commit Signed-off-by: Alvaro Frias Garay <alvaro.frias@eclypsium.com> * clients/githubrepo: Add license header Signed-off-by: Alvaro Frias Garay <alvaro.frias@eclypsium.com> * clients: Add exported comment & remove unused structs Signed-off-by: Alvaro Frias Garay <alvaro.frias@eclypsium.com> * checks/raw: Address rangeValCopy issue when iterating commits Signed-off-by: Alvaro Frias Garay <alvaro.frias@eclypsium.com> * clients: Address issue concerning field alignment in User struct Signed-off-by: Alvaro Frias Garay <alvaro.frias@eclypsium.com> * clients/githubrepo: Addres line length linter issue Signed-off-by: Alvaro Frias Garay <alvaro.frias@eclypsium.com> Signed-off-by: Alvaro Frias Garay <alvaro.frias@eclypsium.com> Co-authored-by: Alvaro Frias Garay <alvaro.frias@eclypsium.com>
2022-08-11 18:09:21 +03:00
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package githubrepo
import (
"errors"
"testing"
"github.com/ossf/scorecard/v4/clients"
)
func TestSearchCommitsBuildQuery(t *testing.T) {
t.Parallel()
testcases := []struct {
searchReq clients.SearchCommitsOptions
expectedErrType error
name string
repourl *repoURL
expectedQuery string
hasError bool
}{
{
name: "Basic",
repourl: &repoURL{
owner: "testowner",
repo: "testrepo",
},
searchReq: clients.SearchCommitsOptions{
Author: "testAuthor",
},
expectedQuery: "repo:testowner/testrepo author:testAuthor",
},
{
name: "EmptyQuery",
repourl: &repoURL{
owner: "testowner",
repo: "testrepo",
},
searchReq: clients.SearchCommitsOptions{},
hasError: true,
expectedErrType: errEmptyQuery,
},
}
for _, testcase := range testcases {
testcase := testcase
t.Run(testcase.name, func(t *testing.T) {
t.Parallel()
handler := searchCommitsHandler{
repourl: testcase.repourl,
}
query, err := handler.buildQuery(testcase.searchReq)
if !testcase.hasError && err != nil {
t.Fatalf("expected - no error, got: %v", err)
}
if testcase.hasError && !errors.Is(err, testcase.expectedErrType) {
t.Fatalf("expectedErrType - %v, got - %v",
testcase.expectedErrType, err)
} else if query != testcase.expectedQuery {
t.Fatalf("expectedQuery - %s, got - %s",
testcase.expectedQuery, query)
}
})
}
}