From 95dc8d6e16a8a441c2f02635e4b86c63ac33ad39 Mon Sep 17 00:00:00 2001 From: Richard Gomez <32133502+rgmz@users.noreply.github.com> Date: Tue, 26 Mar 2024 09:34:12 -0400 Subject: [PATCH] Fix additional GitHub test errors #2614 --- pkg/sources/errors_test.go | 10 ++++++---- pkg/sources/github/github.go | 4 ++-- pkg/sources/github/github_test.go | 32 +++++++++++++++++++++++++++---- 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/pkg/sources/errors_test.go b/pkg/sources/errors_test.go index d691e2b47a46..e7b9d3a035a3 100644 --- a/pkg/sources/errors_test.go +++ b/pkg/sources/errors_test.go @@ -7,6 +7,8 @@ import ( "testing" ) +var testError = fmt.Errorf("simulated failure") + func TestNewScanErrors(t *testing.T) { testCases := []struct { name string @@ -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() }() @@ -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() }() @@ -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) } diff --git a/pkg/sources/github/github.go b/pkg/sources/github/github.go index 112e7bc82cac..8cf0665cb700 100644 --- a/pkg/sources/github/github.go +++ b/pkg/sources/github/github.go @@ -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 { diff --git a/pkg/sources/github/github_test.go b/pkg/sources/github/github_test.go index defe9777da04..9a8c362a5ac2 100644 --- a/pkg/sources/github/github_test.go +++ b/pkg/sources/github/github_test.go @@ -8,6 +8,7 @@ import ( "encoding/pem" "fmt" "net/http" + "net/url" "reflect" "strconv" "testing" @@ -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)) } @@ -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 {