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
Next Next commit
Update tests
  • Loading branch information
mcastorina committed Feb 11, 2025
commit bb576f7f3d00e658a6a1166a6536e728126060fe
5 changes: 2 additions & 3 deletions pkg/handlers/handlers_test.go
Original file line number Diff line number Diff line change
@@ -785,12 +785,11 @@ func getGitCommitHash(t *testing.T, gitDir string) string {

type mockReporter struct{ reportedChunks int }

func (m *mockReporter) ChunkOk(context.Context, sources.Chunk) error {
func (m *mockReporter) ChunkOk(context.Context, sources.Chunk) {
m.reportedChunks++
return nil
}

func (m *mockReporter) ChunkErr(context.Context, error) error { return nil }
func (m *mockReporter) ChunkErr(context.Context, error) {}

func TestHandleChunksWithError(t *testing.T) {
tests := []struct {
6 changes: 2 additions & 4 deletions pkg/sources/github/github_integration_test.go
Original file line number Diff line number Diff line change
@@ -869,12 +869,10 @@ type countChunkReporter struct {
errCount int
}

func (m *countChunkReporter) ChunkOk(ctx context.Context, chunk sources.Chunk) error {
func (m *countChunkReporter) ChunkOk(ctx context.Context, chunk sources.Chunk) {
m.chunkCount++
return nil
}

func (m *countChunkReporter) ChunkErr(ctx context.Context, err error) error {
func (m *countChunkReporter) ChunkErr(ctx context.Context, err error) {
m.errCount++
return nil
}
33 changes: 12 additions & 21 deletions pkg/sources/source_manager_test.go
Original file line number Diff line number Diff line change
@@ -35,8 +35,7 @@ func (d *DummySource) GetProgress() *Progress { return nil }
// Interface to easily test different chunking methods.
type chunker interface {
Chunks(context.Context, chan *Chunk, ...ChunkingTarget) error
ChunkUnit(ctx context.Context, unit SourceUnit, reporter ChunkReporter) error
Enumerate(ctx context.Context, reporter UnitReporter) error
SourceUnitEnumChunker
}

// Chunk method that writes count bytes to the channel before returning.
@@ -71,15 +70,14 @@ func (c countChunk) Display() string {

func (c *counterChunker) Enumerate(ctx context.Context, reporter UnitReporter) error {
for i := 0; i < c.count; i++ {
if err := reporter.UnitOk(ctx, countChunk(byte(i))); err != nil {
return err
}
reporter.UnitOk(ctx, countChunk(byte(i)))
}
return nil
}

func (c *counterChunker) ChunkUnit(ctx context.Context, unit SourceUnit, reporter ChunkReporter) error {
return reporter.ChunkOk(ctx, Chunk{Data: []byte{byte(unit.(countChunk))}})
reporter.ChunkOk(ctx, Chunk{Data: []byte{byte(unit.(countChunk))}})
return nil
}

// Chunk method that always returns an error.
@@ -179,9 +177,8 @@ func TestSourceManagerEnumerate(t *testing.T) {
assert.NoError(t, err)
var enumeratedUnits []SourceUnit
reporter := visitorUnitReporter{
ok: func(_ context.Context, unit SourceUnit) error {
ok: func(_ context.Context, unit SourceUnit) {
enumeratedUnits = append(enumeratedUnits, unit)
return nil
},
}
for i := 0; i < 3; i++ {
@@ -216,12 +213,12 @@ func TestSourceManagerScan(t *testing.T) {
}

type visitorUnitReporter struct {
ok func(context.Context, SourceUnit) error
err func(context.Context, error) error
ok func(context.Context, SourceUnit)
err func(context.Context, error)
}

func (v visitorUnitReporter) UnitOk(ctx context.Context, u SourceUnit) error { return v.ok(ctx, u) }
func (v visitorUnitReporter) UnitErr(ctx context.Context, err error) error { return v.err(ctx, err) }
func (v visitorUnitReporter) UnitOk(ctx context.Context, u SourceUnit) { v.ok(ctx, u) }
func (v visitorUnitReporter) UnitErr(ctx context.Context, err error) { v.err(ctx, err) }

type unitChunk struct {
unit string
@@ -244,9 +241,7 @@ func (c *unitChunker) Chunks(ctx context.Context, ch chan *Chunk, _ ...ChunkingT
}
func (c *unitChunker) Enumerate(ctx context.Context, rep UnitReporter) error {
for _, step := range c.steps {
if err := rep.UnitOk(ctx, CommonSourceUnit{ID: step.unit}); err != nil {
return err
}
rep.UnitOk(ctx, CommonSourceUnit{ID: step.unit})
}
return nil
}
@@ -257,16 +252,12 @@ func (c *unitChunker) ChunkUnit(ctx context.Context, unit SourceUnit, rep ChunkR
continue
}
if step.err != "" {
if err := rep.ChunkErr(ctx, fmt.Errorf("%s", step.err)); err != nil {
return err
}
rep.ChunkErr(ctx, fmt.Errorf("%s", step.err))
}
if step.output == "" {
continue
}
if err := rep.ChunkOk(ctx, Chunk{Data: []byte(step.output)}); err != nil {
return err
}
rep.ChunkOk(ctx, Chunk{Data: []byte(step.output)})
}
return nil
}
Loading
Oops, something went wrong.