@@ -192,6 +192,92 @@ func TestIsPostDeleteHook(t *testing.T) {
192192 }
193193}
194194
195+ // TestPartitionTargetObjsForSync covers partitionTargetObjsForSync in state.go.
196+ func TestPartitionTargetObjsForSync (t * testing.T ) {
197+ newObj := func (name string , annot map [string ]string ) * unstructured.Unstructured {
198+ u := & unstructured.Unstructured {}
199+ u .SetName (name )
200+ u .SetAnnotations (annot )
201+ return u
202+ }
203+
204+ tests := []struct {
205+ name string
206+ in []* unstructured.Unstructured
207+ wantNames []string
208+ wantPreDelete bool
209+ wantPostDelete bool
210+ }{
211+ {
212+ name : "PostSync with PreDelete and PostDelete in same annotation stays in sync set" ,
213+ in : []* unstructured.Unstructured {
214+ newObj ("combined" , map [string ]string {"argocd.argoproj.io/hook" : "PostSync,PreDelete,PostDelete" }),
215+ },
216+ wantNames : []string {"combined" },
217+ wantPreDelete : true ,
218+ wantPostDelete : true ,
219+ },
220+ {
221+ name : "PreDelete-only manifest excluded from sync" ,
222+ in : []* unstructured.Unstructured {
223+ newObj ("pre-del" , map [string ]string {"argocd.argoproj.io/hook" : "PreDelete" }),
224+ },
225+ wantNames : nil ,
226+ wantPreDelete : true ,
227+ wantPostDelete : false ,
228+ },
229+ {
230+ name : "PostDelete-only manifest excluded from sync" ,
231+ in : []* unstructured.Unstructured {
232+ newObj ("post-del" , map [string ]string {"argocd.argoproj.io/hook" : "PostDelete" }),
233+ },
234+ wantNames : nil ,
235+ wantPreDelete : false ,
236+ wantPostDelete : true ,
237+ },
238+ {
239+ name : "Helm pre-delete only excluded from sync" ,
240+ in : []* unstructured.Unstructured {
241+ newObj ("helm-pre-del" , map [string ]string {"helm.sh/hook" : "pre-delete" }),
242+ },
243+ wantNames : nil ,
244+ wantPreDelete : true ,
245+ wantPostDelete : false ,
246+ },
247+ {
248+ name : "Helm pre-install with pre-delete stays in sync (sync-phase hook wins)" ,
249+ in : []* unstructured.Unstructured {
250+ newObj ("helm-mixed" , map [string ]string {"helm.sh/hook" : "pre-install,pre-delete" }),
251+ },
252+ wantNames : []string {"helm-mixed" },
253+ wantPreDelete : true ,
254+ wantPostDelete : false ,
255+ },
256+ {
257+ name : "Non-hook resource unchanged" ,
258+ in : []* unstructured.Unstructured {
259+ newObj ("pod" , map [string ]string {"app" : "x" }),
260+ },
261+ wantNames : []string {"pod" },
262+ wantPreDelete : false ,
263+ wantPostDelete : false ,
264+ },
265+ }
266+
267+ for _ , tt := range tests {
268+ t .Run (tt .name , func (t * testing.T ) {
269+ got , hasPre , hasPost := partitionTargetObjsForSync (tt .in )
270+ var names []string
271+ for _ , o := range got {
272+ names = append (names , o .GetName ())
273+ }
274+ assert .Equal (t , tt .wantNames , names )
275+ assert .Equal (t , tt .wantPreDelete , hasPre , "hasPreDeleteHooks" )
276+ assert .Equal (t , tt .wantPostDelete , hasPost , "hasPostDeleteHooks" )
277+ })
278+ }
279+ }
280+
195281func TestMultiHookOfType (t * testing.T ) {
196282 tests := []struct {
197283 name string
0 commit comments