github.com/vipcoin-gold/reviewdog@v1.0.2/service/gitlab/gitlab_mr_commit_test.go (about) 1 package gitlab 2 3 import ( 4 "context" 5 "encoding/json" 6 "net/http" 7 "net/http/httptest" 8 "os" 9 "testing" 10 11 "github.com/kylelemons/godebug/pretty" 12 "github.com/vipcoin-gold/go-gitlab" 13 14 "github.com/vipcoin-gold/reviewdog" 15 "github.com/vipcoin-gold/reviewdog/filter" 16 "github.com/vipcoin-gold/reviewdog/proto/rdf" 17 "github.com/vipcoin-gold/reviewdog/service/commentutil" 18 ) 19 20 func TestGitLabMergeRequestCommitCommenter_Post_Flush_review_api(t *testing.T) { 21 cwd, _ := os.Getwd() 22 defer os.Chdir(cwd) 23 os.Chdir("../..") 24 25 apiCalled := 0 26 mux := http.NewServeMux() 27 mux.HandleFunc("/api/v4/projects/o/r/merge_requests/14/commits", func(w http.ResponseWriter, r *http.Request) { 28 apiCalled++ 29 if r.Method != http.MethodGet { 30 t.Errorf("unexpected access: %v %v", r.Method, r.URL) 31 } 32 cs := []*gitlab.Commit{ 33 { 34 ID: "0123456789abcdef", 35 ShortID: "012345678", 36 }, 37 } 38 if err := json.NewEncoder(w).Encode(cs); err != nil { 39 t.Fatal(err) 40 } 41 }) 42 mux.HandleFunc("/api/v4/projects/o/r/repository/commits/0123456789abcdef/comments", func(w http.ResponseWriter, r *http.Request) { 43 apiCalled++ 44 if r.Method != http.MethodGet { 45 t.Errorf("unexpected access: %v %v", r.Method, r.URL) 46 } 47 cs := []*gitlab.CommitComment{ 48 { 49 Path: "notExistFile.go", 50 Line: 1, 51 Note: commentutil.BodyPrefix + "already commented", 52 }, 53 } 54 if err := json.NewEncoder(w).Encode(cs); err != nil { 55 t.Fatal(err) 56 } 57 }) 58 mux.HandleFunc("/api/v4/projects/o/r/repository/commits/sha/comments", func(w http.ResponseWriter, r *http.Request) { 59 apiCalled++ 60 if r.Method != http.MethodPost { 61 t.Errorf("unexpected access: %v %v", r.Method, r.URL) 62 } 63 var req gitlab.CommitComment 64 if err := json.NewDecoder(r.Body).Decode(&req); err != nil { 65 t.Error(err) 66 } 67 want := gitlab.CommitComment{ 68 Path: "notExistFile.go", 69 Line: 14, 70 Note: commentutil.BodyPrefix + "new comment", 71 LineType: "new", 72 } 73 if diff := pretty.Compare(want, req); diff != "" { 74 t.Errorf("req.Comments diff: (-got +want)\n%s", diff) 75 } 76 if err := json.NewEncoder(w).Encode(req); err != nil { 77 t.Fatal(err) 78 } 79 }) 80 ts := httptest.NewServer(mux) 81 defer ts.Close() 82 83 cli, err := gitlab.NewClient("", gitlab.WithBaseURL(ts.URL+"/api/v4")) 84 if err != nil { 85 t.Fatal(err) 86 } 87 88 g, err := NewGitLabMergeRequestCommitCommenter(cli, "o", "r", 14, "sha") 89 if err != nil { 90 t.Fatal(err) 91 } 92 // Path is set to non existing file path for mock test not to use last commit id of the line. 93 // If setting exists file path, sha is changed by last commit id. 94 comments := []*reviewdog.Comment{ 95 { 96 Result: &filter.FilteredDiagnostic{ 97 Diagnostic: &rdf.Diagnostic{ 98 Location: &rdf.Location{ 99 Path: "notExistFile.go", 100 Range: &rdf.Range{Start: &rdf.Position{ 101 Line: 1, 102 }}, 103 }, 104 Message: "already commented", 105 }, 106 InDiffFile: true, 107 }, 108 }, 109 { 110 Result: &filter.FilteredDiagnostic{ 111 Diagnostic: &rdf.Diagnostic{ 112 Location: &rdf.Location{ 113 Path: "notExistFile.go", 114 Range: &rdf.Range{Start: &rdf.Position{ 115 Line: 14, 116 }}, 117 }, 118 Message: "new comment", 119 }, 120 InDiffFile: true, 121 }, 122 }, 123 } 124 for _, c := range comments { 125 if err := g.Post(context.Background(), c); err != nil { 126 t.Error(err) 127 } 128 } 129 if err := g.Flush(context.Background()); err != nil { 130 t.Error(err) 131 } 132 if want := 3; apiCalled != want { 133 t.Errorf("GitLab API is called %d times, want %d times", apiCalled, want) 134 } 135 } 136 137 func TestGitLabPullRequest_workdir(t *testing.T) { 138 cwd, _ := os.Getwd() 139 defer os.Chdir(cwd) 140 os.Chdir("../..") 141 142 g, err := NewGitLabMergeRequestCommitCommenter(nil, "", "", 0, "") 143 if err != nil { 144 t.Fatal(err) 145 } 146 if g.wd != "" { 147 t.Fatalf("g.wd = %q, want empty", g.wd) 148 } 149 ctx := context.Background() 150 want := "a/b/c" 151 g.Post(ctx, &reviewdog.Comment{Result: &filter.FilteredDiagnostic{ 152 Diagnostic: &rdf.Diagnostic{Location: &rdf.Location{Path: want}}}}) 153 if got := g.postComments[0].Result.Diagnostic.GetLocation().GetPath(); got != want { 154 t.Errorf("wd=%q path=%q, want %q", g.wd, got, want) 155 } 156 157 subDir := "cmd/" 158 if err := os.Chdir(subDir); err != nil { 159 t.Fatal(err) 160 } 161 g, _ = NewGitLabMergeRequestCommitCommenter(nil, "", "", 0, "") 162 if g.wd != subDir { 163 t.Fatalf("gitRelWorkdir() = %q, want %q", g.wd, subDir) 164 } 165 path := "a/b/c" 166 wantPath := "cmd/" + path 167 g.Post(ctx, &reviewdog.Comment{Result: &filter.FilteredDiagnostic{ 168 Diagnostic: &rdf.Diagnostic{Location: &rdf.Location{Path: want}}}}) 169 if got := g.postComments[0].Result.Diagnostic.GetLocation().GetPath(); got != wantPath { 170 t.Errorf("wd=%q path=%q, want %q", g.wd, got, wantPath) 171 } 172 }