@@ -70,36 +70,194 @@ func testPlan(t *testing.T, ref, commit, baseBranch string) {
7070
7171 projectCommandBuilder .VerifyWasCalledOnce ().BuildPlanCommands (cmdContext , commentCmd )
7272 projectCommandRunner .VerifyWasCalledOnce ().Plan (Any [command.ProjectContext ]())
73+
74+ cases := []struct {
75+ repository string
76+ ref string
77+ vcsType string
78+ pr int
79+ projects []string
80+ paths []struct {
81+ Directory string
82+ Workspace string
83+ }
84+ }{
85+ {
86+ repository : "Repo" ,
87+ ref : "main" ,
88+ vcsType : "Gitlab" ,
89+ projects : []string {"default" },
90+ },
91+ {
92+ repository : "Repo" ,
93+ ref : "main" ,
94+ vcsType : "Gitlab" ,
95+ pr : 1 ,
96+ },
97+ {
98+ repository : "Repo" ,
99+ ref : "main" ,
100+ vcsType : "Gitlab" ,
101+ paths : []struct {
102+ Directory string
103+ Workspace string
104+ }{
105+ {
106+ Directory : "." ,
107+ Workspace : "myworkspace" ,
108+ },
109+ {
110+ Directory : "./myworkspace2" ,
111+ Workspace : "myworkspace2" ,
112+ },
113+ },
114+ },
115+ {
116+ repository : "Repo" ,
117+ ref : "main" ,
118+ vcsType : "Gitlab" ,
119+ pr : 1 ,
120+ projects : []string {"test" },
121+ paths : []struct {
122+ Directory string
123+ Workspace string
124+ }{
125+ {
126+ Directory : "." ,
127+ Workspace : "myworkspace" ,
128+ },
129+ },
130+ },
131+ }
132+
133+ expectedCalls := 0
134+ for _ , c := range cases {
135+ body , _ := json .Marshal (controllers.APIRequest {
136+ Repository : c .repository ,
137+ Ref : c .ref ,
138+ Type : c .vcsType ,
139+ PR : c .pr ,
140+ Projects : c .projects ,
141+ Paths : c .paths ,
142+ })
143+
144+ req , _ := http .NewRequest ("POST" , "" , bytes .NewBuffer (body ))
145+ req .Header .Set (atlantisTokenHeader , atlantisToken )
146+ w := httptest .NewRecorder ()
147+ ac .Plan (w , req )
148+ ResponseContains (t , w , http .StatusOK , "" )
149+
150+ expectedCalls += len (c .projects )
151+ expectedCalls += len (c .paths )
152+ }
153+
154+ projectCommandBuilder .VerifyWasCalled (Times (expectedCalls )).BuildPlanCommands (Any [* command.Context ](), Any [* events.CommentCommand ]())
155+ projectCommandRunner .VerifyWasCalled (Times (expectedCalls )).Plan (Any [command.ProjectContext ]())
73156}
74157
75158func TestAPIController_Apply (t * testing.T ) {
76159 ac , projectCommandBuilder , projectCommandRunner := setup (t )
77- body , _ := json .Marshal (controllers.APIRequest {
78- Repository : "Repo" ,
79- Ref : "main" ,
80- Type : "Gitlab" ,
81- Projects : []string {"default" },
82- })
83- req , _ := http .NewRequest ("POST" , "" , bytes .NewBuffer (body ))
84- req .Header .Set (atlantisTokenHeader , atlantisToken )
85- w := httptest .NewRecorder ()
86- ac .Apply (w , req )
87- ResponseContains (t , w , http .StatusOK , "" )
88- projectCommandBuilder .VerifyWasCalledOnce ().BuildApplyCommands (Any [* command.Context ](), Any [* events.CommentCommand ]())
89- projectCommandRunner .VerifyWasCalledOnce ().Plan (Any [command.ProjectContext ]())
90- projectCommandRunner .VerifyWasCalledOnce ().Apply (Any [command.ProjectContext ]())
160+
161+ cases := []struct {
162+ repository string
163+ ref string
164+ vcsType string
165+ pr int
166+ projects []string
167+ paths []struct {
168+ Directory string
169+ Workspace string
170+ }
171+ }{
172+ {
173+ repository : "Repo" ,
174+ ref : "main" ,
175+ vcsType : "Gitlab" ,
176+ projects : []string {"default" },
177+ },
178+ {
179+ repository : "Repo" ,
180+ ref : "main" ,
181+ vcsType : "Gitlab" ,
182+ pr : 1 ,
183+ },
184+ {
185+ repository : "Repo" ,
186+ ref : "main" ,
187+ vcsType : "Gitlab" ,
188+ paths : []struct {
189+ Directory string
190+ Workspace string
191+ }{
192+ {
193+ Directory : "." ,
194+ Workspace : "myworkspace" ,
195+ },
196+ {
197+ Directory : "./myworkspace2" ,
198+ Workspace : "myworkspace2" ,
199+ },
200+ },
201+ },
202+ {
203+ repository : "Repo" ,
204+ ref : "main" ,
205+ vcsType : "Gitlab" ,
206+ pr : 1 ,
207+ projects : []string {"test" },
208+ paths : []struct {
209+ Directory string
210+ Workspace string
211+ }{
212+ {
213+ Directory : "." ,
214+ Workspace : "myworkspace" ,
215+ },
216+ },
217+ },
218+ }
219+
220+ expectedCalls := 0
221+ for _ , c := range cases {
222+ body , _ := json .Marshal (controllers.APIRequest {
223+ Repository : c .repository ,
224+ Ref : c .ref ,
225+ Type : c .vcsType ,
226+ PR : c .pr ,
227+ Projects : c .projects ,
228+ Paths : c .paths ,
229+ })
230+
231+ req , _ := http .NewRequest ("POST" , "" , bytes .NewBuffer (body ))
232+ req .Header .Set (atlantisTokenHeader , atlantisToken )
233+ w := httptest .NewRecorder ()
234+ ac .Apply (w , req )
235+ ResponseContains (t , w , http .StatusOK , "" )
236+
237+ expectedCalls += len (c .projects )
238+ expectedCalls += len (c .paths )
239+ }
240+
241+ projectCommandBuilder .VerifyWasCalled (Times (expectedCalls )).BuildApplyCommands (Any [* command.Context ](), Any [* events.CommentCommand ]())
242+ projectCommandRunner .VerifyWasCalled (Times (expectedCalls )).Plan (Any [command.ProjectContext ]())
243+ projectCommandRunner .VerifyWasCalled (Times (expectedCalls )).Apply (Any [command.ProjectContext ]())
91244}
92245
93246func setup (t * testing.T ) (controllers.APIController , * MockProjectCommandBuilder , * MockProjectCommandRunner ) {
94247 RegisterMockTestingT (t )
95248 locker := NewMockLocker ()
96249 logger := logging .NewNoopLogger (t )
97- scope , _ , _ := metrics .NewLoggingScope (logger , "null" )
98250 parser := NewMockEventParsing ()
99- vcsClient := NewMockClient ()
100251 repoAllowlistChecker , err := events .NewRepoAllowlistChecker ("*" )
252+ scope , _ , _ := metrics .NewLoggingScope (logger , "null" )
253+ vcsClient := NewMockClient ()
254+ workingDir := NewMockWorkingDir ()
101255 Ok (t , err )
102256
257+ workingDirLocker := NewMockWorkingDirLocker ()
258+ When (workingDirLocker .TryLock (Any [string ](), Any [int ](), Eq (events .DefaultWorkspace ), Eq (events .DefaultRepoRelDir ))).
259+ ThenReturn (func () {}, nil )
260+
103261 projectCommandBuilder := NewMockProjectCommandBuilder ()
104262 When (projectCommandBuilder .BuildPlanCommands (Any [* command.Context ](), Any [* events.CommentCommand ]())).
105263 ThenReturn ([]command.ProjectContext {{
@@ -143,6 +301,8 @@ func setup(t *testing.T) (controllers.APIController, *MockProjectCommandBuilder,
143301 PostWorkflowHooksCommandRunner : postWorkflowHooksCommandRunner ,
144302 VCSClient : vcsClient ,
145303 RepoAllowlistChecker : repoAllowlistChecker ,
304+ WorkingDir : workingDir ,
305+ WorkingDirLocker : workingDirLocker ,
146306 CommitStatusUpdater : commitStatusUpdater ,
147307 }
148308 return ac , projectCommandBuilder , projectCommandRunner
0 commit comments