github.com/massongit/reviewdog@v0.0.0-20240331071725-4a16675475a8/comment_test.go (about)

     1  package reviewdog
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/reviewdog/reviewdog/filter"
    10  	"github.com/reviewdog/reviewdog/proto/rdf"
    11  )
    12  
    13  func TestMultiCommentService_Post(t *testing.T) {
    14  	buf1 := new(bytes.Buffer)
    15  	buf2 := new(bytes.Buffer)
    16  	w := MultiCommentService(NewRawCommentWriter(buf1), NewRawCommentWriter(buf2))
    17  
    18  	const want = "line1\nline2"
    19  
    20  	c := &Comment{Result: &filter.FilteredDiagnostic{Diagnostic: &rdf.Diagnostic{OriginalOutput: want}}}
    21  	if err := w.Post(context.Background(), c); err != nil {
    22  		t.Fatal(err)
    23  	}
    24  
    25  	if got := strings.Trim(buf1.String(), "\n"); got != want {
    26  		t.Errorf("writer 1: got %v, want %v", got, want)
    27  	}
    28  
    29  	if got := strings.Trim(buf2.String(), "\n"); got != want {
    30  		t.Errorf("writer 2: got %v, want %v", got, want)
    31  	}
    32  
    33  	if err := w.(BulkCommentService).Flush(context.Background()); err != nil {
    34  		t.Errorf("MultiCommentService implements BulkCommentService and should not return error when any services implements it: %v", err)
    35  	}
    36  }
    37  
    38  type fakeBulkCommentService struct {
    39  	BulkCommentService
    40  	calledFlush bool
    41  }
    42  
    43  func (f *fakeBulkCommentService) Flush(_ context.Context) error {
    44  	f.calledFlush = true
    45  	return nil
    46  }
    47  
    48  func TestMultiCommentService_Flush(t *testing.T) {
    49  	f1 := &fakeBulkCommentService{}
    50  	f2 := &fakeBulkCommentService{}
    51  	w := MultiCommentService(f1, f2)
    52  	if err := w.(BulkCommentService).Flush(context.Background()); err != nil {
    53  		t.Fatal(err)
    54  	}
    55  	if !f1.calledFlush || !f2.calledFlush {
    56  		t.Error("MultiCommentService_Flush should run Flush() for every services")
    57  	}
    58  }