mirror of
https://github.com/trufflesecurity/trufflehog.git
synced 2026-06-15 13:24:37 +00:00
[THOG-681] - Handle errors sources (#783)
* Handle errors w/ github source. * Fix loop var captured by func literal. * Fix loop var captured by func literal. * Set completed progress if the scan completes with no errors. * Set progress to 100% if the scope and iteration are both 0. * Fix commentary. * Fix test. * Return after the defer to os.RemoveAll. * Fix unauth scan. * Inline range loop. * update tests for partial scan completion with errors. Ensure correct progress is set. * Update progress for all sources. * Update github test. * Address comments.
This commit is contained in:
@@ -17,6 +17,7 @@ import (
|
||||
"github.com/google/go-github/v42/github"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"golang.org/x/sync/errgroup"
|
||||
"google.golang.org/protobuf/types/known/anypb"
|
||||
"gopkg.in/h2non/gock.v1"
|
||||
|
||||
@@ -67,7 +68,7 @@ func TestAddReposByOrg(t *testing.T) {
|
||||
|
||||
s := initTestSource(nil)
|
||||
// gock works here because github.NewClient is using the default HTTP Transport
|
||||
err := s.addReposByOrg(context.TODO(), github.NewClient(nil), "super-secret-org")
|
||||
err := s.addRepos(context.TODO(), github.NewClient(nil), "super-secret-org", s.getReposByOrg)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, 1, len(s.repos))
|
||||
assert.Equal(t, []string{"super-secret-repo"}, s.repos)
|
||||
@@ -83,7 +84,7 @@ func TestAddReposByUser(t *testing.T) {
|
||||
JSON([]map[string]string{{"clone_url": "super-secret-repo"}})
|
||||
|
||||
s := initTestSource(nil)
|
||||
err := s.addReposByUser(context.TODO(), github.NewClient(nil), "super-secret-user")
|
||||
err := s.addRepos(context.TODO(), github.NewClient(nil), "super-secret-user", s.getReposByUser)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, 1, len(s.repos))
|
||||
assert.Equal(t, []string{"super-secret-repo"}, s.repos)
|
||||
@@ -438,3 +439,41 @@ func Test_setProgressCompleteWithRepo_Progress(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func Test_scan_SetProgressComplete(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
repos []string
|
||||
wantComplete bool
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "no repos",
|
||||
wantComplete: true,
|
||||
},
|
||||
{
|
||||
name: "one valid repo",
|
||||
repos: []string{"a"},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
src := &Source{
|
||||
repos: tc.repos,
|
||||
}
|
||||
src.jobPool = &errgroup.Group{}
|
||||
|
||||
_ = src.scan(context.Background(), nil, nil)
|
||||
if !tc.wantErr {
|
||||
assert.Equal(t, "", src.GetProgress().EncodedResumeInfo)
|
||||
}
|
||||
|
||||
gotComplete := src.GetProgress().PercentComplete == 100
|
||||
if gotComplete != tc.wantComplete {
|
||||
t.Errorf("got: %v, want: %v", gotComplete, tc.wantComplete)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user