func TestCallAllAnalyzers(t *testing.T) { dispatcher := &fakeDispatcher{categories: []string{"Foo", "Bar"}, files: []string{"dir1/A.h", "dir1/A.cc"}} addr, cleanup, err := testutil.CreatekRPCTestServer(dispatcher, "AnalyzerService") if err != nil { t.Fatalf("Registering analyzer service failed: %v", err) } defer cleanup() driver := NewTestDriver([]serviceInfo{ serviceInfo{addr, strset.New("Foo", "Bar"), ctxpb.Stage_PRE_BUILD}, }) tests := []struct { files []string categories []string expect []*notepb.Note }{ { // - SomeOtherCategory should be dropped; if it weren't fakeDispatcher would return an error. // - fakeDispatcher produces notes for files we didn't ask about; they should be dropped. []string{"dir1/A.cc"}, []string{"Foo", "SomeOtherCategory"}, []*notepb.Note{ ¬epb.Note{ Category: proto.String("Foo"), Description: proto.String(""), Location: testutil.CreateLocation("dir1/A.cc"), }, }, }, } for _, test := range tests { ctx := &ctxpb.ShipshapeContext{FilePath: test.files} ars := driver.callAllAnalyzers(strset.New(test.categories...), ctx, ctxpb.Stage_PRE_BUILD) var notes []*notepb.Note for _, ar := range ars { notes = append(notes, ar.Note...) if len(ar.Failure) > 0 { t.Errorf("Received failures from analyze call: %v", ar.Failure) } } ok, results := testutil.CheckNoteContainsContent(test.expect, notes) if !ok { t.Errorf("Incorrect notes for categories %v: %s\n got %v, want %v", test.categories, results, notes, test.expect) } } }
func TestAnalyze(t *testing.T) { tests := []struct { file string words int }{ {"simple.txt", 6}, {"complex.txt", 10}, {"empty.txt", 0}, {"whitespace.txt", 0}, } var w WordCountAnalyzer for _, pair := range tests { ctx, err := test.CreateContext(dataDir, []string{pair.file}) if err != nil { t.Fatalf("error from CreateContext: %v", err) } actualNotes, err := test.RunAnalyzer(ctx, w, t) if err != nil { t.Errorf("received an analysis failure: %v", err) } expectedNotes := []*notespb.Note{ ¬espb.Note{ Category: proto.String("WordCount"), Description: proto.String(fmt.Sprintf("Word count: %d", pair.words)), }, } pass, message := test.CheckNoteContainsContent(expectedNotes, actualNotes) if !pass { t.Errorf(message) } } }
func TestCallAllAnalyzersErrorCases(t *testing.T) { ctx := &ctxpb.ShipshapeContext{FilePath: []string{"dir1/A", "dir2/B"}} tests := []struct { response *rpcpb.AnalyzeResponse expectNotes []*notepb.Note expectFailure []*rpcpb.AnalysisFailure }{ { //analysis had a failure &rpcpb.AnalyzeResponse{ Failure: []*rpcpb.AnalysisFailure{ &rpcpb.AnalysisFailure{ Category: proto.String("Foo"), FailureMessage: proto.String("badbadbad"), }, }, }, nil, []*rpcpb.AnalysisFailure{ &rpcpb.AnalysisFailure{ Category: proto.String("Foo"), FailureMessage: proto.String("badbadbad"), }, }, }, { //analysis had both failure and notes &rpcpb.AnalyzeResponse{ Note: []*notepb.Note{ ¬epb.Note{ Category: proto.String("Foo"), Description: proto.String("A note"), Location: testutil.CreateLocation("dir1/A"), }, ¬epb.Note{ Category: proto.String("Foo"), Description: proto.String("A note"), Location: testutil.CreateLocation("dir1/A"), }, }, Failure: []*rpcpb.AnalysisFailure{ &rpcpb.AnalysisFailure{ Category: proto.String("Foo"), FailureMessage: proto.String("badbadbad"), }, }, }, []*notepb.Note{ ¬epb.Note{ Category: proto.String("Foo"), Description: proto.String("A note"), Location: testutil.CreateLocation("dir1/A"), }, ¬epb.Note{ Category: proto.String("Foo"), Description: proto.String("A note"), Location: testutil.CreateLocation("dir1/A"), }, }, []*rpcpb.AnalysisFailure{ &rpcpb.AnalysisFailure{ Category: proto.String("Foo"), FailureMessage: proto.String("badbadbad"), }, }, }, } for _, test := range tests { addr, cleanup, err := testutil.CreatekRPCTestServer(&fullFakeDispatcher{test.response}, "AnalyzerService") if err != nil { t.Fatalf("Registering analyzer service failed: %v", err) } defer cleanup() driver := NewTestDriver([]serviceInfo{ serviceInfo{addr, strset.New("Foo"), ctxpb.Stage_PRE_BUILD}, }) ars := driver.callAllAnalyzers(strset.New("Foo"), ctx, ctxpb.Stage_PRE_BUILD) var notes []*notepb.Note var failures []*rpcpb.AnalysisFailure for _, ar := range ars { notes = append(notes, ar.Note...) failures = append(failures, ar.Failure...) } ok, results := testutil.CheckNoteContainsContent(test.expectNotes, notes) if !ok { t.Errorf("Incorrect notes for original response %v: %s\n got %v, want %v", test.response, results, notes, test.expectNotes) } ok, results = testutil.CheckFailureContainsContent(test.expectFailure, failures) if !ok { t.Errorf("Incorrect failures for original response %v: %s\n got %v, want %v", test.response, results, failures, test.expectFailure) } } }