@@ -22,21 +22,39 @@ async fn mock_server() -> ServerGuard {
22
22
Server :: new_async ( ) . await
23
23
}
24
24
25
- async fn setup (
26
- lib_root : & Path ,
27
- lines_changed_only : & str ,
28
- tidy_review : bool ,
29
- format_review : bool ,
30
- passive_reviews : bool ,
31
- no_lgtm : bool ,
32
- summary_only : bool ,
33
- ) {
25
+ struct TestParams {
26
+ pub lines_changed_only : String ,
27
+ pub tidy_review : bool ,
28
+ pub format_review : bool ,
29
+ pub passive_reviews : bool ,
30
+ pub no_lgtm : bool ,
31
+ pub summary_only : bool ,
32
+ pub pr_state : String ,
33
+ pub pr_draft : bool ,
34
+ }
35
+
36
+ impl Default for TestParams {
37
+ fn default ( ) -> Self {
38
+ Self {
39
+ lines_changed_only : "true" . to_string ( ) ,
40
+ tidy_review : true ,
41
+ format_review : true ,
42
+ passive_reviews : Default :: default ( ) ,
43
+ no_lgtm : true ,
44
+ summary_only : Default :: default ( ) ,
45
+ pr_state : "open" . to_string ( ) ,
46
+ pr_draft : Default :: default ( ) ,
47
+ }
48
+ }
49
+ }
50
+
51
+ async fn setup ( lib_root : & Path , test_params : & TestParams ) {
34
52
env:: set_var ( "GITHUB_EVENT_NAME" , "pull_request" ) ;
35
53
env:: set_var ( "GITHUB_REPOSITORY" , REPO ) ;
36
54
env:: set_var ( "GITHUB_SHA" , SHA ) ;
37
55
env:: set_var ( "GITHUB_TOKEN" , TOKEN ) ;
38
56
env:: set_var ( "CI" , "true" ) ;
39
- if summary_only {
57
+ if test_params . summary_only {
40
58
env:: set_var ( "CPP_LINTER_PR_REVIEW_SUMMARY_ONLY" , "true" ) ;
41
59
}
42
60
let mut event_payload_path = NamedTempFile :: new ( ) . unwrap ( ) ;
@@ -45,7 +63,8 @@ async fn setup(
45
63
. expect ( "Failed to create mock event payload." ) ;
46
64
env:: set_var ( "GITHUB_EVENT_PATH" , event_payload_path. path ( ) ) ;
47
65
let reset_timestamp = ( Utc :: now ( ) . timestamp ( ) + 60 ) . to_string ( ) ;
48
- let is_lgtm = !no_lgtm || ( ( false , false ) == ( tidy_review, format_review) ) ;
66
+ let is_lgtm = !test_params. no_lgtm
67
+ || ( ( false , false ) == ( test_params. tidy_review , test_params. format_review ) ) ;
49
68
let asset_path = format ! ( "{}/{MOCK_ASSETS_PATH}" , lib_root. to_str( ) . unwrap( ) ) ;
50
69
51
70
let mut server = mock_server ( ) . await ;
@@ -64,7 +83,9 @@ async fn setup(
64
83
. mock ( "GET" , pr_endpoint. as_str ( ) )
65
84
. match_header ( "Accept" , "application/vnd.github.raw+json" )
66
85
. match_header ( "Authorization" , TOKEN )
67
- . with_body_from_file ( format ! ( "{asset_path}pr_{PR}.json" ) )
86
+ . with_body (
87
+ json ! ( { "state" : test_params. pr_state, "draft" : test_params. pr_draft} ) . to_string ( ) ,
88
+ )
68
89
. with_header ( REMAINING_RATE_LIMIT_HEADER , "50" )
69
90
. with_header ( RESET_RATE_LIMIT_HEADER , reset_timestamp. as_str ( ) )
70
91
. create ( ) ;
@@ -89,7 +110,7 @@ async fn setup(
89
110
. with_header ( RESET_RATE_LIMIT_HEADER , reset_timestamp. as_str ( ) )
90
111
. create ( ) ;
91
112
92
- let review_reaction = if passive_reviews {
113
+ let review_reaction = if test_params . passive_reviews {
93
114
"COMMENT"
94
115
} else if is_lgtm {
95
116
"APPROVE"
@@ -108,7 +129,8 @@ async fn setup(
108
129
. create ( ) ;
109
130
110
131
let mut tool_ignore = "**.c" . to_string ( ) ;
111
- if !no_lgtm || ( false , true ) == ( tidy_review, format_review) {
132
+ if !test_params. no_lgtm || ( false , true ) == ( test_params. tidy_review , test_params. format_review )
133
+ {
112
134
// force a LGTM condition by skipping analysis on all files
113
135
tool_ignore. push ( '|' ) ;
114
136
tool_ignore. push_str ( "src" ) ;
@@ -117,15 +139,15 @@ async fn setup(
117
139
"cpp-linter" . to_string( ) ,
118
140
"-v=debug" . to_string( ) ,
119
141
format!( "-V={}" , env:: var( "CLANG_VERSION" ) . unwrap_or( "" . to_string( ) ) ) ,
120
- format!( "-l={lines_changed_only}" ) ,
142
+ format!( "-l={}" , test_params . lines_changed_only ) ,
121
143
format!( "--ignore-tidy={}" , tool_ignore) ,
122
144
format!( "--ignore-format={}" , tool_ignore) ,
123
145
// "--tidy-checks=".to_string(), // use .clang-tidy file
124
146
"--style=file" . to_string( ) , // use .clang-format file
125
- format!( "--tidy-review={tidy_review}" ) ,
126
- format!( "--format-review={format_review}" ) ,
127
- format!( "--passive-reviews={passive_reviews}" ) ,
128
- format!( "--no-lgtm={no_lgtm}" ) ,
147
+ format!( "--tidy-review={}" , test_params . tidy_review ) ,
148
+ format!( "--format-review={}" , test_params . format_review ) ,
149
+ format!( "--passive-reviews={}" , test_params . passive_reviews ) ,
150
+ format!( "--no-lgtm={}" , test_params . no_lgtm ) ,
129
151
"-p=build" . to_string( ) ,
130
152
"-i=build" . to_string( ) ,
131
153
] ;
@@ -136,131 +158,103 @@ async fn setup(
136
158
assert_eq ! ( result, 0 ) ;
137
159
}
138
160
139
- async fn test_review (
140
- lines_changed_only : & str ,
141
- tidy_review : bool ,
142
- format_review : bool ,
143
- passive_reviews : bool ,
144
- no_lgtm : bool ,
145
- summary_only : bool ,
146
- ) {
161
+ async fn test_review ( test_params : & TestParams ) {
147
162
let tmp_dir = create_test_space ( ) ;
148
163
let lib_root = env:: current_dir ( ) . unwrap ( ) ;
149
164
env:: set_current_dir ( tmp_dir. path ( ) ) . unwrap ( ) ;
150
- setup (
151
- & lib_root,
152
- lines_changed_only,
153
- tidy_review,
154
- format_review,
155
- passive_reviews,
156
- no_lgtm,
157
- summary_only,
158
- )
159
- . await ;
165
+ setup ( & lib_root, & test_params) . await ;
160
166
env:: set_current_dir ( lib_root. as_path ( ) ) . unwrap ( ) ;
161
167
drop ( tmp_dir) ;
162
168
}
163
169
164
170
#[ tokio:: test]
165
171
async fn all_lines ( ) {
166
- test_review (
167
- "false" , // lines_changed_only
168
- true , // tidy-review
169
- true , // format-review
170
- false , // passive-reviews
171
- true , // no_lgtm
172
- false , // summary_only
173
- )
172
+ test_review ( & TestParams {
173
+ lines_changed_only : "false" . to_string ( ) ,
174
+ ..Default :: default ( )
175
+ } )
174
176
. await ;
175
177
}
176
178
177
179
#[ tokio:: test]
178
180
async fn changed_lines ( ) {
179
- test_review (
180
- "true" , // lines_changed_only
181
- true , // tidy-review
182
- true , // format-review
183
- false , // passive-reviews
184
- true , // no_lgtm
185
- false , // summary_only
186
- )
187
- . await ;
181
+ test_review ( & TestParams :: default ( ) ) . await ;
188
182
}
189
183
190
184
#[ tokio:: test]
191
185
async fn all_lines_passive ( ) {
192
- test_review (
193
- "false" , // lines_changed_only
194
- true , // tidy-review
195
- true , // format-review
196
- true , // passive-reviews
197
- true , // no_lgtm
198
- false , // summary_only
199
- )
186
+ test_review ( & TestParams {
187
+ lines_changed_only : "false" . to_string ( ) ,
188
+ passive_reviews : true ,
189
+ ..Default :: default ( )
190
+ } )
200
191
. await ;
201
192
}
202
193
203
194
#[ tokio:: test]
204
195
async fn changed_lines_passive ( ) {
205
- test_review (
206
- "true" , // lines_changed_only
207
- true , // tidy-review
208
- true , // format-review
209
- true , // passive-reviews
210
- true , // no_lgtm
211
- false , // summary_only
212
- )
196
+ test_review ( & TestParams {
197
+ passive_reviews : true ,
198
+ ..Default :: default ( )
199
+ } )
213
200
. await ;
214
201
}
215
202
216
203
#[ tokio:: test]
217
204
async fn summary_only ( ) {
218
- test_review (
219
- "false" , // lines_changed_only
220
- true , // tidy-review
221
- true , // format-review
222
- false , // passive-reviews
223
- true , // no_lgtm
224
- true , // summary_only
225
- )
205
+ test_review ( & TestParams {
206
+ lines_changed_only : "false" . to_string ( ) ,
207
+ summary_only : true ,
208
+ ..Default :: default ( )
209
+ } )
226
210
. await ;
227
211
}
228
212
229
213
#[ tokio:: test]
230
214
async fn lgtm ( ) {
231
- test_review (
232
- "false" , // lines_changed_only
233
- true , // tidy-review
234
- true , // format-review
235
- false , // passive-reviews
236
- false , // no_lgtm
237
- false , // summary_only
238
- )
215
+ test_review ( & TestParams {
216
+ lines_changed_only : "false" . to_string ( ) ,
217
+ no_lgtm : false ,
218
+ ..Default :: default ( )
219
+ } )
239
220
. await ;
240
221
}
241
222
242
223
#[ tokio:: test]
243
224
async fn lgtm_passive ( ) {
244
- test_review (
245
- "false" , // lines_changed_only
246
- true , // tidy-review
247
- true , // format-review
248
- true , // passive-reviews
249
- false , // no_lgtm
250
- false , // summary_only
251
- )
225
+ test_review ( & TestParams {
226
+ lines_changed_only : "false" . to_string ( ) ,
227
+ no_lgtm : false ,
228
+ passive_reviews : true ,
229
+ ..Default :: default ( )
230
+ } )
252
231
. await ;
253
232
}
254
233
255
234
#[ tokio:: test]
256
235
async fn no_lgtm ( ) {
257
- test_review (
258
- "false" , // lines_changed_only
259
- false , // tidy-review
260
- true , // format-review
261
- false , // passive-reviews
262
- true , // no_lgtm
263
- false , // summary_only
264
- )
236
+ test_review ( & TestParams {
237
+ lines_changed_only : "false" . to_string ( ) ,
238
+ tidy_review : false ,
239
+ ..Default :: default ( )
240
+ } )
241
+ . await ;
242
+ }
243
+
244
+ #[ tokio:: test]
245
+ async fn is_draft ( ) {
246
+ test_review ( & TestParams {
247
+ pr_draft : true ,
248
+ ..Default :: default ( )
249
+ } )
250
+ . await ;
251
+ }
252
+
253
+ #[ tokio:: test]
254
+ async fn is_closed ( ) {
255
+ test_review ( & TestParams {
256
+ pr_state : "closed" . to_string ( ) ,
257
+ ..Default :: default ( )
258
+ } )
265
259
. await ;
266
260
}
0 commit comments