github.com/khulnasoft/codebase@v0.0.0-20231214144635-a707781cbb24/service/gerrit/change_review_test.go (about) 1 package gerrit 2 3 import ( 4 "context" 5 "encoding/json" 6 "fmt" 7 "net/http" 8 "net/http/httptest" 9 "os" 10 "testing" 11 12 "github.com/google/go-cmp/cmp" 13 "golang.org/x/build/gerrit" 14 15 "github.com/khulnasoft/codebase" 16 "github.com/khulnasoft/codebase/filter" 17 "github.com/khulnasoft/codebase/proto/rdf" 18 ) 19 20 func TestChangeReviewCommenter_Post_Flush(t *testing.T) { 21 cwd, _ := os.Getwd() 22 defer func(dir string) { 23 if err := os.Chdir(dir); err != nil { 24 t.Error(err) 25 } 26 }(cwd) 27 if err := os.Chdir("../.."); err != nil { 28 t.Error(err) 29 } 30 31 ctx := context.Background() 32 newLnum1 := 14 33 newComment1 := &codebase.Comment{ 34 Result: &filter.FilteredDiagnostic{ 35 Diagnostic: &rdf.Diagnostic{ 36 Location: &rdf.Location{ 37 Path: "file.go", 38 Range: &rdf.Range{Start: &rdf.Position{ 39 Line: int32(newLnum1), 40 }}, 41 }, 42 Message: "new comment", 43 }, 44 InDiffFile: true, 45 }, 46 } 47 newLnum2 := 15 48 newComment2 := &codebase.Comment{ 49 Result: &filter.FilteredDiagnostic{ 50 Diagnostic: &rdf.Diagnostic{ 51 Location: &rdf.Location{ 52 Path: "file2.go", 53 Range: &rdf.Range{Start: &rdf.Position{ 54 Line: int32(newLnum2), 55 }}, 56 }, 57 Message: "new comment 2", 58 }, 59 InDiffFile: true, 60 }, 61 } 62 commentOutsideDiff := &codebase.Comment{ 63 Result: &filter.FilteredDiagnostic{ 64 Diagnostic: &rdf.Diagnostic{ 65 Location: &rdf.Location{ 66 Path: "file3.go", 67 Range: &rdf.Range{Start: &rdf.Position{ 68 Line: 14, 69 }}, 70 }, 71 Message: "comment outside diff", 72 }, 73 InDiffFile: false, 74 }, 75 } 76 77 comments := []*codebase.Comment{ 78 newComment1, 79 newComment2, 80 commentOutsideDiff, 81 } 82 83 mux := http.NewServeMux() 84 mux.HandleFunc(`/changes/testChangeID/revisions/testRevisionID/review`, func(w http.ResponseWriter, r *http.Request) { 85 switch r.Method { 86 case http.MethodPost: 87 got := new(gerrit.ReviewInput) 88 if err := json.NewDecoder(r.Body).Decode(got); err != nil { 89 t.Error(err) 90 } 91 92 if want := len(comments) - 1; len(got.Comments) != want { 93 t.Errorf("got %d comments, want %d", len(got.Comments), want) 94 } 95 96 line1 := int(newComment1.Result.Diagnostic.GetLocation().GetRange().GetStart().GetLine()) 97 want := []gerrit.CommentInput{{ 98 Line: line1, Message: newComment1.Result.Diagnostic.GetMessage()}} 99 if diff := cmp.Diff(got.Comments["file.go"], want); diff != "" { 100 t.Error(diff) 101 } 102 103 line2 := int(newComment2.Result.Diagnostic.GetLocation().GetRange().GetStart().GetLine()) 104 want = []gerrit.CommentInput{{ 105 Line: line2, Message: newComment2.Result.Diagnostic.GetMessage()}} 106 if diff := cmp.Diff(got.Comments["file2.go"], want); diff != "" { 107 t.Error(diff) 108 } 109 110 fmt.Fprintf(w, ")]}\n{}") 111 default: 112 t.Errorf("unexpected access: %v %v", r.Method, r.URL) 113 } 114 }) 115 116 ts := httptest.NewServer(mux) 117 defer ts.Close() 118 119 cli := gerrit.NewClient(ts.URL, gerrit.NoAuth) 120 121 g, err := NewChangeReviewCommenter(cli, "testChangeID", "testRevisionID") 122 if err != nil { 123 t.Fatal(err) 124 } 125 126 for _, c := range comments { 127 if err := g.Post(ctx, c); err != nil { 128 t.Error(err) 129 } 130 } 131 132 if err := g.Flush(ctx); err != nil { 133 t.Errorf("%v", err) 134 } 135 }