Skip to content

Commit

Permalink
Replace context.TODO. (trufflesecurity#1349)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahrav authored May 19, 2023
1 parent 4ba0ad4 commit 1da7720
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 29 deletions.
4 changes: 2 additions & 2 deletions pkg/engine/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestGitEngine(t *testing.T) {
}
defer os.RemoveAll(path)

ctx, cancel := context.WithCancel(context.TODO())
ctx, cancel := context.WithCancel(ctx)
defer cancel()
type testProfile struct {
expected map[string]expResult
Expand Down Expand Up @@ -98,7 +98,7 @@ func BenchmarkGitEngine(b *testing.B) {
}
defer os.RemoveAll(path)

ctx, cancel := context.WithCancel(context.TODO())
ctx, cancel := context.WithCancel(ctx)
defer cancel()

e := Start(ctx,
Expand Down
8 changes: 4 additions & 4 deletions pkg/gitparse/gitparse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func TestSingleCommitSingleDiff(t *testing.T) {
},
}
go func() {
parser.FromReader(context.TODO(), r, commitChan)
parser.FromReader(context.Background(), r, commitChan)
}()
i := 0
for commit := range commitChan {
Expand Down Expand Up @@ -169,7 +169,7 @@ func TestMultiCommitContextDiff(t *testing.T) {
},
}
go func() {
NewParser().FromReader(context.TODO(), r, commitChan)
NewParser().FromReader(context.Background(), r, commitChan)
}()
i := 0
for commit := range commitChan {
Expand Down Expand Up @@ -199,7 +199,7 @@ func TestMaxDiffSize(t *testing.T) {

commitChan := make(chan Commit)
go func() {
parser.FromReader(context.TODO(), bigReader, commitChan)
parser.FromReader(context.Background(), bigReader, commitChan)
}()

commit := <-commitChan
Expand Down Expand Up @@ -236,7 +236,7 @@ func TestMessageParsing(t *testing.T) {
expectedMessage := singleCommitSingleDiffMessage

go func() {
parser.FromReader(context.TODO(), r, commitChan)
parser.FromReader(context.Background(), r, commitChan)
}()
for commit := range commitChan {
if commit.Message.String() != expectedMessage {
Expand Down
3 changes: 2 additions & 1 deletion pkg/handlers/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

diskbufferreader "github.com/bill-rich/disk-buffer-reader"
"github.com/stretchr/testify/assert"

"github.com/trufflesecurity/trufflehog/v3/pkg/sources"
)

Expand Down Expand Up @@ -79,7 +80,7 @@ func TestArchiveHandler(t *testing.T) {
if err != nil {
t.Errorf("error creating reusable reader: %s", err)
}
archiveChan := archive.FromFile(context.TODO(), newReader)
archiveChan := archive.FromFile(context.Background(), newReader)

count := 0
re := regexp.MustCompile(testCase.matchString)
Expand Down
2 changes: 1 addition & 1 deletion pkg/sources/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ func PrepareRepoSinceCommit(ctx context.Context, uriString, commitHash string) (
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(context.TODO(), ts)
tc := oauth2.NewClient(ctx, ts)
client = github.NewClient(tc)
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/sources/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func (s *Source) Init(aCtx context.Context, name string, jobID, sourceID int64,
Link: git.GenerateLink(repository, commit, file),
Timestamp: sanitizer.UTF8(timestamp),
Line: line,
Visibility: s.visibilityOf(repository),
Visibility: s.visibilityOf(aCtx, repository),
},
},
}
Expand All @@ -243,7 +243,7 @@ func (s *Source) Init(aCtx context.Context, name string, jobID, sourceID int64,
return nil
}

func (s *Source) visibilityOf(repoURL string) (visibility source_metadatapb.Visibility) {
func (s *Source) visibilityOf(ctx context.Context, repoURL string) (visibility source_metadatapb.Visibility) {
s.mu.Lock()
visibility, ok := s.publicMap[repoURL]
s.mu.Unlock()
Expand Down Expand Up @@ -274,7 +274,7 @@ func (s *Source) visibilityOf(repoURL string) (visibility source_metadatapb.Visi
repoName := urlPathParts[1]
repoName = strings.TrimSuffix(repoName, ".git")
for {
gist, resp, err = s.apiClient.Gists.Get(context.TODO(), repoName)
gist, resp, err = s.apiClient.Gists.Get(ctx, repoName)
if !s.handleRateLimit(err, resp) {
break
}
Expand All @@ -296,7 +296,7 @@ func (s *Source) visibilityOf(repoURL string) (visibility source_metadatapb.Visi
repoName := urlPathParts[2]
repoName = strings.TrimSuffix(repoName, ".git")
for {
repo, resp, err = s.apiClient.Repositories.Get(context.TODO(), owner, repoName)
repo, resp, err = s.apiClient.Repositories.Get(ctx, owner, repoName)
if !s.handleRateLimit(err, resp) {
break
}
Expand Down Expand Up @@ -448,7 +448,7 @@ func (s *Source) enumerateWithToken(ctx context.Context, apiEndpoint, token stri

ctx.Logger().V(1).Info("Enumerating with token", "endpoint", apiEndpoint)
for {
ghUser, resp, err = s.apiClient.Users.Get(context.TODO(), "")
ghUser, resp, err = s.apiClient.Users.Get(ctx, "")
if handled := s.handleRateLimit(err, resp); handled {
continue
}
Expand Down
32 changes: 16 additions & 16 deletions pkg/sources/github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func createTestSource(src *sourcespb.GitHub) (*Source, *anypb.Any) {

func initTestSource(src *sourcespb.GitHub) *Source {
s, conn := createTestSource(src)
if err := s.Init(context.TODO(), "test - github", 0, 1337, false, conn, 1); err != nil {
if err := s.Init(context.Background(), "test - github", 0, 1337, false, conn, 1); err != nil {
panic(err)
}
s.apiClient = github.NewClient(s.httpClient)
Expand All @@ -54,7 +54,7 @@ func TestInit(t *testing.T) {
},
})

err := source.Init(context.TODO(), "test - github", 0, 1337, false, conn, 1)
err := source.Init(context.Background(), "test - github", 0, 1337, false, conn, 1)
assert.Nil(t, err)

// TODO: test error case
Expand All @@ -79,7 +79,7 @@ func TestAddReposByOrg(t *testing.T) {
IgnoreRepos: []string{"secret/super-*-repo2"},
})
// gock works here because github.NewClient is using the default HTTP Transport
err := s.getReposByOrg(context.TODO(), "super-secret-org")
err := s.getReposByOrg(context.Background(), "super-secret-org")
assert.Nil(t, err)
assert.Equal(t, 1, s.filteredRepoCache.Count())
ok := s.filteredRepoCache.Exists("super-secret-repo")
Expand Down Expand Up @@ -107,7 +107,7 @@ func TestAddReposByOrg_IncludeRepos(t *testing.T) {
Organizations: []string{"super-secret-org"},
})
// gock works here because github.NewClient is using the default HTTP Transport
err := s.getReposByOrg(context.TODO(), "super-secret-org")
err := s.getReposByOrg(context.Background(), "super-secret-org")
assert.Nil(t, err)
assert.Equal(t, 2, s.filteredRepoCache.Count())
ok := s.filteredRepoCache.Exists("secret/super-secret-repo")
Expand All @@ -134,7 +134,7 @@ func TestAddReposByUser(t *testing.T) {
},
IgnoreRepos: []string{"secret/super-secret-repo2"},
})
err := s.getReposByUser(context.TODO(), "super-secret-user")
err := s.getReposByUser(context.Background(), "super-secret-user")
assert.Nil(t, err)
assert.Equal(t, 1, s.filteredRepoCache.Count())
ok := s.filteredRepoCache.Exists("super-secret-repo")
Expand All @@ -151,7 +151,7 @@ func TestAddGistsByUser(t *testing.T) {
JSON([]map[string]string{{"git_pull_url": "https://githug.com/super-secret-gist.git", "id": "super-secret-gist"}})

s := initTestSource(nil)
err := s.addUserGistsToCache(context.TODO(), "super-secret-user")
err := s.addUserGistsToCache(context.Background(), "super-secret-user")
assert.Nil(t, err)
assert.Equal(t, 1, s.filteredRepoCache.Count())
ok := s.filteredRepoCache.Exists("super-secret-gist")
Expand All @@ -171,7 +171,7 @@ func TestAddMembersByOrg(t *testing.T) {
})

s := initTestSource(nil)
err := s.addMembersByOrg(context.TODO(), "org1")
err := s.addMembersByOrg(context.Background(), "org1")
assert.Nil(t, err)
assert.Equal(t, 2, len(s.memberCache))
_, ok := s.memberCache["testman1"]
Expand Down Expand Up @@ -200,7 +200,7 @@ func TestAddMembersByApp(t *testing.T) {
})

s := initTestSource(nil)
err := s.addMembersByApp(context.TODO(), github.NewClient(nil))
err := s.addMembersByApp(context.Background(), github.NewClient(nil))
assert.Nil(t, err)
assert.Equal(t, 3, len(s.memberCache))
_, ok := s.memberCache["ssm1"]
Expand Down Expand Up @@ -249,7 +249,7 @@ func TestAddOrgsByUser(t *testing.T) {
})

s := initTestSource(nil)
s.addOrgsByUser(context.TODO(), "super-secret-user")
s.addOrgsByUser(context.Background(), "super-secret-user")
assert.Equal(t, 1, s.orgsCache.Count())
ok := s.orgsCache.Exists("sso2")
assert.True(t, ok)
Expand Down Expand Up @@ -347,7 +347,7 @@ func TestEnumerateUnauthenticated(t *testing.T) {
s := initTestSource(nil)
s.orgsCache = memory.New()
s.orgsCache.Set("super-secret-org", "super-secret-org")
s.enumerateUnauthenticated(context.TODO())
s.enumerateUnauthenticated(context.Background())
assert.Equal(t, 1, s.filteredRepoCache.Count())
ok := s.filteredRepoCache.Exists("super-secret-repo")
assert.True(t, ok)
Expand Down Expand Up @@ -379,7 +379,7 @@ func TestEnumerateWithToken(t *testing.T) {
JSON([]map[string]string{{"git_pull_url": "https://github.com/super-secret-gist.git", "id": "super-secret-gist"}})

s := initTestSource(nil)
err := s.enumerateWithToken(context.TODO(), "https://api.github.com", "token")
err := s.enumerateWithToken(context.Background(), "https://api.github.com", "token")
assert.Nil(t, err)
assert.Equal(t, 2, s.filteredRepoCache.Count())
ok := s.filteredRepoCache.Exists("super-secret-repo")
Expand Down Expand Up @@ -417,7 +417,7 @@ func BenchmarkEnumerateWithToken(b *testing.B) {

b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = s.enumerateWithToken(context.TODO(), "https://api.github.com", "token")
_ = s.enumerateWithToken(context.Background(), "https://api.github.com", "token")
}
}

Expand Down Expand Up @@ -451,7 +451,7 @@ func TestEnumerate(t *testing.T) {
},
})

_, err := s.enumerate(context.TODO(), "https://api.github.com")
_, err := s.enumerate(context.Background(), "https://api.github.com")
assert.Nil(t, err)
assert.Equal(t, 2, s.filteredRepoCache.Count())
ok := s.filteredRepoCache.Exists("super-secret-repo")
Expand Down Expand Up @@ -512,7 +512,7 @@ func BenchmarkEnumerate(b *testing.B) {
setupMocks(b)

b.StartTimer()
_, _ = s.enumerate(context.TODO(), "https://api.github.com")
_, _ = s.enumerate(context.Background(), "https://api.github.com")
}
}

Expand All @@ -527,7 +527,7 @@ func TestEnumerateWithToken_IncludeRepos(t *testing.T) {
s := initTestSource(nil)
s.repos = []string{"some-special-repo"}

err := s.enumerateWithToken(context.TODO(), "https://api.github.com", "token")
err := s.enumerateWithToken(context.Background(), "https://api.github.com", "token")
assert.Nil(t, err)
assert.Equal(t, 1, len(s.repos))
assert.Equal(t, []string{"some-special-repo"}, s.repos)
Expand Down Expand Up @@ -567,7 +567,7 @@ func TestEnumerateWithApp(t *testing.T) {

s := initTestSource(nil)
_, err := s.enumerateWithApp(
context.TODO(),
context.Background(),
"https://api.github.com",
&credentialspb.GitHubApp{
InstallationId: "1337",
Expand Down

0 comments on commit 1da7720

Please sign in to comment.