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