@@ -25,49 +25,194 @@ const atlantisToken = "token"
25
25
26
26
func TestAPIController_Plan (t * testing.T ) {
27
27
ac , projectCommandBuilder , projectCommandRunner := setup (t )
28
- body , _ := json .Marshal (controllers.APIRequest {
29
- Repository : "Repo" ,
30
- Ref : "main" ,
31
- Type : "Gitlab" ,
32
- Projects : []string {"default" },
33
- })
34
- req , _ := http .NewRequest ("POST" , "" , bytes .NewBuffer (body ))
35
- req .Header .Set (atlantisTokenHeader , atlantisToken )
36
- w := httptest .NewRecorder ()
37
- ac .Plan (w , req )
38
- ResponseContains (t , w , http .StatusOK , "" )
39
- projectCommandBuilder .VerifyWasCalledOnce ().BuildPlanCommands (Any [* command.Context ](), Any [* events.CommentCommand ]())
40
- projectCommandRunner .VerifyWasCalledOnce ().Plan (Any [command.ProjectContext ]())
28
+
29
+ cases := []struct {
30
+ repository string
31
+ ref string
32
+ vcsType string
33
+ pr int
34
+ projects []string
35
+ paths []struct {
36
+ Directory string
37
+ Workspace string
38
+ }
39
+ }{
40
+ {
41
+ repository : "Repo" ,
42
+ ref : "main" ,
43
+ vcsType : "Gitlab" ,
44
+ projects : []string {"default" },
45
+ },
46
+ {
47
+ repository : "Repo" ,
48
+ ref : "main" ,
49
+ vcsType : "Gitlab" ,
50
+ pr : 1 ,
51
+ },
52
+ {
53
+ repository : "Repo" ,
54
+ ref : "main" ,
55
+ vcsType : "Gitlab" ,
56
+ paths : []struct {
57
+ Directory string
58
+ Workspace string
59
+ }{
60
+ {
61
+ Directory : "." ,
62
+ Workspace : "myworkspace" ,
63
+ },
64
+ {
65
+ Directory : "./myworkspace2" ,
66
+ Workspace : "myworkspace2" ,
67
+ },
68
+ },
69
+ },
70
+ {
71
+ repository : "Repo" ,
72
+ ref : "main" ,
73
+ vcsType : "Gitlab" ,
74
+ pr : 1 ,
75
+ projects : []string {"test" },
76
+ paths : []struct {
77
+ Directory string
78
+ Workspace string
79
+ }{
80
+ {
81
+ Directory : "." ,
82
+ Workspace : "myworkspace" ,
83
+ },
84
+ },
85
+ },
86
+ }
87
+
88
+ expectedCalls := 0
89
+ for _ , c := range cases {
90
+ body , _ := json .Marshal (controllers.APIRequest {
91
+ Repository : c .repository ,
92
+ Ref : c .ref ,
93
+ Type : c .vcsType ,
94
+ PR : c .pr ,
95
+ Projects : c .projects ,
96
+ Paths : c .paths ,
97
+ })
98
+
99
+ req , _ := http .NewRequest ("POST" , "" , bytes .NewBuffer (body ))
100
+ req .Header .Set (atlantisTokenHeader , atlantisToken )
101
+ w := httptest .NewRecorder ()
102
+ ac .Plan (w , req )
103
+ ResponseContains (t , w , http .StatusOK , "" )
104
+
105
+ expectedCalls += len (c .projects )
106
+ expectedCalls += len (c .paths )
107
+ }
108
+
109
+ projectCommandBuilder .VerifyWasCalled (Times (expectedCalls )).BuildPlanCommands (Any [* command.Context ](), Any [* events.CommentCommand ]())
110
+ projectCommandRunner .VerifyWasCalled (Times (expectedCalls )).Plan (Any [command.ProjectContext ]())
41
111
}
42
112
43
113
func TestAPIController_Apply (t * testing.T ) {
44
114
ac , projectCommandBuilder , projectCommandRunner := setup (t )
45
- body , _ := json .Marshal (controllers.APIRequest {
46
- Repository : "Repo" ,
47
- Ref : "main" ,
48
- Type : "Gitlab" ,
49
- Projects : []string {"default" },
50
- })
51
- req , _ := http .NewRequest ("POST" , "" , bytes .NewBuffer (body ))
52
- req .Header .Set (atlantisTokenHeader , atlantisToken )
53
- w := httptest .NewRecorder ()
54
- ac .Apply (w , req )
55
- ResponseContains (t , w , http .StatusOK , "" )
56
- projectCommandBuilder .VerifyWasCalledOnce ().BuildApplyCommands (Any [* command.Context ](), Any [* events.CommentCommand ]())
57
- projectCommandRunner .VerifyWasCalledOnce ().Plan (Any [command.ProjectContext ]())
58
- projectCommandRunner .VerifyWasCalledOnce ().Apply (Any [command.ProjectContext ]())
115
+
116
+ cases := []struct {
117
+ repository string
118
+ ref string
119
+ vcsType string
120
+ pr int
121
+ projects []string
122
+ paths []struct {
123
+ Directory string
124
+ Workspace string
125
+ }
126
+ }{
127
+ {
128
+ repository : "Repo" ,
129
+ ref : "main" ,
130
+ vcsType : "Gitlab" ,
131
+ projects : []string {"default" },
132
+ },
133
+ {
134
+ repository : "Repo" ,
135
+ ref : "main" ,
136
+ vcsType : "Gitlab" ,
137
+ pr : 1 ,
138
+ },
139
+ {
140
+ repository : "Repo" ,
141
+ ref : "main" ,
142
+ vcsType : "Gitlab" ,
143
+ paths : []struct {
144
+ Directory string
145
+ Workspace string
146
+ }{
147
+ {
148
+ Directory : "." ,
149
+ Workspace : "myworkspace" ,
150
+ },
151
+ {
152
+ Directory : "./myworkspace2" ,
153
+ Workspace : "myworkspace2" ,
154
+ },
155
+ },
156
+ },
157
+ {
158
+ repository : "Repo" ,
159
+ ref : "main" ,
160
+ vcsType : "Gitlab" ,
161
+ pr : 1 ,
162
+ projects : []string {"test" },
163
+ paths : []struct {
164
+ Directory string
165
+ Workspace string
166
+ }{
167
+ {
168
+ Directory : "." ,
169
+ Workspace : "myworkspace" ,
170
+ },
171
+ },
172
+ },
173
+ }
174
+
175
+ expectedCalls := 0
176
+ for _ , c := range cases {
177
+ body , _ := json .Marshal (controllers.APIRequest {
178
+ Repository : c .repository ,
179
+ Ref : c .ref ,
180
+ Type : c .vcsType ,
181
+ PR : c .pr ,
182
+ Projects : c .projects ,
183
+ Paths : c .paths ,
184
+ })
185
+
186
+ req , _ := http .NewRequest ("POST" , "" , bytes .NewBuffer (body ))
187
+ req .Header .Set (atlantisTokenHeader , atlantisToken )
188
+ w := httptest .NewRecorder ()
189
+ ac .Apply (w , req )
190
+ ResponseContains (t , w , http .StatusOK , "" )
191
+
192
+ expectedCalls += len (c .projects )
193
+ expectedCalls += len (c .paths )
194
+ }
195
+
196
+ projectCommandBuilder .VerifyWasCalled (Times (expectedCalls )).BuildApplyCommands (Any [* command.Context ](), Any [* events.CommentCommand ]())
197
+ projectCommandRunner .VerifyWasCalled (Times (expectedCalls )).Plan (Any [command.ProjectContext ]())
198
+ projectCommandRunner .VerifyWasCalled (Times (expectedCalls )).Apply (Any [command.ProjectContext ]())
59
199
}
60
200
61
201
func setup (t * testing.T ) (controllers.APIController , * MockProjectCommandBuilder , * MockProjectCommandRunner ) {
62
202
RegisterMockTestingT (t )
63
203
locker := NewMockLocker ()
64
204
logger := logging .NewNoopLogger (t )
65
- scope , _ , _ := metrics .NewLoggingScope (logger , "null" )
66
205
parser := NewMockEventParsing ()
67
- vcsClient := NewMockClient ()
68
206
repoAllowlistChecker , err := events .NewRepoAllowlistChecker ("*" )
207
+ scope , _ , _ := metrics .NewLoggingScope (logger , "null" )
208
+ vcsClient := NewMockClient ()
209
+ workingDir := NewMockWorkingDir ()
69
210
Ok (t , err )
70
211
212
+ workingDirLocker := NewMockWorkingDirLocker ()
213
+ When (workingDirLocker .TryLock (Any [string ](), Any [int ](), Eq (events .DefaultWorkspace ), Eq (events .DefaultRepoRelDir ))).
214
+ ThenReturn (func () {}, nil )
215
+
71
216
projectCommandBuilder := NewMockProjectCommandBuilder ()
72
217
When (projectCommandBuilder .BuildPlanCommands (Any [* command.Context ](), Any [* events.CommentCommand ]())).
73
218
ThenReturn ([]command.ProjectContext {{
@@ -111,6 +256,8 @@ func setup(t *testing.T) (controllers.APIController, *MockProjectCommandBuilder,
111
256
PostWorkflowHooksCommandRunner : postWorkflowHooksCommandRunner ,
112
257
VCSClient : vcsClient ,
113
258
RepoAllowlistChecker : repoAllowlistChecker ,
259
+ WorkingDir : workingDir ,
260
+ WorkingDirLocker : workingDirLocker ,
114
261
CommitStatusUpdater : commitStatusUpdater ,
115
262
}
116
263
return ac , projectCommandBuilder , projectCommandRunner
0 commit comments