Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change source reporter interfaces to not return errors #3900

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Address lint errors
  • Loading branch information
mcastorina committed Feb 11, 2025
commit 471744c11a1eb1d01e5d5d7ed76f42250633b5f6
46 changes: 0 additions & 46 deletions pkg/sources/filesystem/filesystem_test.go
Original file line number Diff line number Diff line change
@@ -252,52 +252,6 @@ func TestEnumerateReporterErr(t *testing.T) {
s := Source{}
err = s.Init(ctx, "test enumerate", 0, 0, true, conn, 1)
assert.NoError(t, err)

// Enumerate should always return an error if the reporter returns an
// error.
reporter := sourcestest.ErrReporter{}
err = s.Enumerate(ctx, &reporter)
assert.Error(t, err)
}

func TestChunkUnitReporterErr(t *testing.T) {
t.Parallel()
ctx := context.Background()

// Setup test file to chunk.
tmpfile, err := os.CreateTemp("", "example.txt")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name())

fileContents := []byte("TestChunkUnit")
_, err = tmpfile.Write(fileContents)
assert.NoError(t, err)
assert.NoError(t, tmpfile.Close())

conn, err := anypb.New(&sourcespb.Filesystem{})
assert.NoError(t, err)

// Initialize the source.
s := Source{}
err = s.Init(ctx, "test chunk unit", 0, 0, true, conn, 1)
assert.NoError(t, err)

// Happy path. ChunkUnit should always return an error if the reporter
// returns an error.
reporter := sourcestest.ErrReporter{}
err = s.ChunkUnit(ctx, sources.CommonSourceUnit{
ID: tmpfile.Name(),
}, &reporter)
assert.Error(t, err)

// Error path. ChunkUnit should always return an error if the reporter
// returns an error.
err = s.ChunkUnit(ctx, sources.CommonSourceUnit{
ID: "/file/not/found",
}, &reporter)
assert.Error(t, err)
}

// createTempFile is a helper function to create a temporary file in the given
7 changes: 2 additions & 5 deletions pkg/sources/github/github_test.go
Original file line number Diff line number Diff line change
@@ -606,10 +606,9 @@ func TestEnumerate(t *testing.T) {

var reportedRepos []string
reporter := sources.VisitorReporter{
VisitUnit: func(ctx context.Context, su sources.SourceUnit) error {
VisitUnit: func(ctx context.Context, su sources.SourceUnit) {
url, _ := su.SourceUnitID()
reportedRepos = append(reportedRepos, url)
return nil
},
}

@@ -961,8 +960,6 @@ func Test_ScanMultipleTargets_MultipleErrors(t *testing.T) {

func noopReporter() sources.UnitReporter {
return sources.VisitorReporter{
VisitUnit: func(context.Context, sources.SourceUnit) error {
return nil
},
VisitUnit: func(context.Context, sources.SourceUnit) {},
}
}
36 changes: 5 additions & 31 deletions pkg/sourcestest/sourcestest.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package sourcestest

import (
"fmt"

"github.com/trufflesecurity/trufflehog/v3/pkg/context"
"github.com/trufflesecurity/trufflehog/v3/pkg/sources"
)
@@ -12,10 +10,7 @@ type reporter interface {
sources.ChunkReporter
}

var (
_ reporter = (*TestReporter)(nil)
_ reporter = (*ErrReporter)(nil)
)
var _ reporter = (*TestReporter)(nil)

// TestReporter is a helper struct that implements both UnitReporter and
// ChunkReporter by simply recording the values passed in the methods.
@@ -26,36 +21,15 @@ type TestReporter struct {
ChunkErrs []error
}

func (t *TestReporter) UnitOk(_ context.Context, unit sources.SourceUnit) error {
func (t *TestReporter) UnitOk(_ context.Context, unit sources.SourceUnit) {
t.Units = append(t.Units, unit)
return nil
}
func (t *TestReporter) UnitErr(_ context.Context, err error) error {
func (t *TestReporter) UnitErr(_ context.Context, err error) {
t.UnitErrs = append(t.UnitErrs, err)
return nil
}
func (t *TestReporter) ChunkOk(_ context.Context, chunk sources.Chunk) error {
func (t *TestReporter) ChunkOk(_ context.Context, chunk sources.Chunk) {
t.Chunks = append(t.Chunks, chunk)
return nil
}
func (t *TestReporter) ChunkErr(_ context.Context, err error) error {
func (t *TestReporter) ChunkErr(_ context.Context, err error) {
t.ChunkErrs = append(t.ChunkErrs, err)
return nil
}

// ErrReporter implements UnitReporter and ChunkReporter but always returns an
// error.
type ErrReporter struct{}

func (ErrReporter) UnitOk(context.Context, sources.SourceUnit) error {
return fmt.Errorf("ErrReporter: UnitOk error")
}
func (ErrReporter) UnitErr(context.Context, error) error {
return fmt.Errorf("ErrReporter: UnitErr error")
}
func (ErrReporter) ChunkOk(context.Context, sources.Chunk) error {
return fmt.Errorf("ErrReporter: ChunkOk error")
}
func (ErrReporter) ChunkErr(context.Context, error) error {
return fmt.Errorf("ErrReporter: ChunkErr error")
}
Loading
Oops, something went wrong.