Skip to content

Commit

Permalink
Fix additional GitHub test errors #2614
Browse files Browse the repository at this point in the history
  • Loading branch information
rgmz committed Mar 26, 2024
1 parent de588bf commit 95dc8d6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
10 changes: 6 additions & 4 deletions pkg/sources/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"testing"
)

var testError = fmt.Errorf("simulated failure")

func TestNewScanErrors(t *testing.T) {
testCases := []struct {
name string
Expand Down Expand Up @@ -84,7 +86,7 @@ func TestScanErrorsAdd(t *testing.T) {
wg.Add(1)
go func() {
for j := 0; j < tc.wantErr/tc.concurrency; j++ {
se.Add(nil)
se.Add(testError)
}
wg.Done()
}()
Expand Down Expand Up @@ -135,7 +137,7 @@ func TestScanErrorsCount(t *testing.T) {
wg.Add(1)
go func() {
for j := 0; j < tc.wantErrCnt/tc.concurrency; j++ {
se.Add(nil)
se.Add(testError)
}
wg.Done()
}()
Expand All @@ -151,8 +153,8 @@ func TestScanErrorsCount(t *testing.T) {

func TestScanErrorsString(t *testing.T) {
se := NewScanErrors()
se.Add(nil)
want := "[]"
se.Add(testError)
want := `["` + testError.Error() + `"]`
if got := fmt.Sprintf("%v", se); got != want {
t.Errorf("got %q, want %q", got, want)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/sources/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -1151,8 +1151,8 @@ func (s *Source) processGistComments(ctx context.Context, gistURL string, urlPar
return nil
}

func extractGistID(url []string) string {
return url[len(url)-1]
func extractGistID(urlParts []string) string {
return urlParts[len(urlParts)-1]
}

func (s *Source) chunkGistComments(ctx context.Context, gistURL string, gistInfo repoInfo, comments []*github.GistComment, chunksChan chan *sources.Chunk) error {
Expand Down
32 changes: 28 additions & 4 deletions pkg/sources/github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/pem"
"fmt"
"net/http"
"net/url"
"reflect"
"strconv"
"testing"
Expand Down Expand Up @@ -329,10 +330,35 @@ func TestHandleRateLimit(t *testing.T) {
s := initTestSource(nil)
assert.False(t, s.handleRateLimit(nil))

err := &github.RateLimitError{}
res := &github.Response{Response: &http.Response{Header: make(http.Header)}}
// Request
reqUrl, _ := url.Parse("https://github.com/trufflesecurity/trufflehog")
res := &github.Response{
Response: &http.Response{
StatusCode: 429,
Header: make(http.Header),
Request: &http.Request{
Method: "GET",
URL: reqUrl,
},
},
}
res.Header.Set("x-ratelimit-remaining", "0")
res.Header.Set("x-ratelimit-reset", strconv.FormatInt(time.Now().Unix()+1, 10))

// Error
resetTime := github.Timestamp{
Time: time.Now().Add(time.Millisecond),
}
err := &github.RateLimitError{
Rate: github.Rate{
Limit: 5000,
Remaining: 0,
Reset: resetTime,
},
Response: res.Response,
Message: "Too Many Requests",
}

assert.True(t, s.handleRateLimit(err))
}

Expand Down Expand Up @@ -742,8 +768,6 @@ func TestGetGistID(t *testing.T) {
}{
{[]string{"https://gist.github.com", "12345"}, "12345"},
{[]string{"https://gist.github.com", "owner", "12345"}, "12345"},
{[]string{"https://gist.github.com"}, ""},
{[]string{"https://gist.github.com", "owner", "12345", "extra"}, ""},
}

for _, tt := range tests {
Expand Down

0 comments on commit 95dc8d6

Please sign in to comment.