@@ -58,11 +58,36 @@ func TestNewCmdDelete(t *testing.T) {
5858 cli : "--succeed-on-no-caches 123" ,
5959 wantsErr : "--succeed-on-no-caches must be used in conjunction with --all" ,
6060 },
61+ {
62+ name : "key argument and delete all flag" ,
63+ cli : "cache-key --all" ,
64+ wantsErr : "specify only one of cache id, cache key, or --all" ,
65+ },
6166 {
6267 name : "id argument and delete all flag" ,
6368 cli : "1 --all" ,
6469 wantsErr : "specify only one of cache id, cache key, or --all" ,
6570 },
71+ {
72+ name : "key argument with ref" ,
73+ cli : "cache-key --ref refs/heads/main" ,
74+ wants : DeleteOptions {Identifier : "cache-key" , Ref : "refs/heads/main" },
75+ },
76+ {
77+ name : "ref flag without cache key" ,
78+ cli : "--ref refs/heads/main" ,
79+ wantsErr : "must provide a cache key" ,
80+ },
81+ {
82+ name : "ref flag with cache id" ,
83+ cli : "123 --ref refs/heads/main" ,
84+ wantsErr : "--ref cannot be used with cache ID" ,
85+ },
86+ {
87+ name : "ref flag with all flag" ,
88+ cli : "--all --ref refs/heads/main" ,
89+ wantsErr : "--ref cannot be used with --all" ,
90+ },
6691 }
6792
6893 for _ , tt := range tests {
@@ -89,6 +114,7 @@ func TestNewCmdDelete(t *testing.T) {
89114 assert .Equal (t , tt .wants .DeleteAll , gotOpts .DeleteAll )
90115 assert .Equal (t , tt .wants .SucceedOnNoCaches , gotOpts .SucceedOnNoCaches )
91116 assert .Equal (t , tt .wants .Identifier , gotOpts .Identifier )
117+ assert .Equal (t , tt .wants .Ref , gotOpts .Ref )
92118 })
93119 }
94120}
@@ -209,7 +235,8 @@ func TestDeleteRun(t *testing.T) {
209235 httpmock .QueryMatcher ("DELETE" , "repos/OWNER/REPO/actions/caches" , url.Values {
210236 "key" : []string {"a weird_cache+key" },
211237 }),
212- httpmock .StatusStringResponse (204 , "" ),
238+ // The response is a JSON object but we don't need it here.
239+ httpmock .StatusStringResponse (200 , "{}" ),
213240 )
214241 },
215242 tty : true ,
@@ -263,6 +290,54 @@ func TestDeleteRun(t *testing.T) {
263290 wantErr : false ,
264291 wantStdout : "" ,
265292 },
293+ {
294+ name : "deletes cache with ref tty" ,
295+ opts : DeleteOptions {Identifier : "cache-key" , Ref : "refs/heads/main" },
296+ stubs : func (reg * httpmock.Registry ) {
297+ reg .Register (
298+ httpmock .QueryMatcher ("DELETE" , "repos/OWNER/REPO/actions/caches" , url.Values {
299+ "key" : []string {"cache-key" },
300+ "ref" : []string {"refs/heads/main" },
301+ }),
302+ // The response is a JSON object but we don't need it here.
303+ httpmock .StatusStringResponse (200 , "{}" ),
304+ )
305+ },
306+ tty : true ,
307+ wantStdout : "✓ Deleted 1 cache from OWNER/REPO\n " ,
308+ },
309+ {
310+ name : "deletes cache with ref non-tty" ,
311+ opts : DeleteOptions {Identifier : "cache-key" , Ref : "refs/heads/main" },
312+ stubs : func (reg * httpmock.Registry ) {
313+ reg .Register (
314+ httpmock .QueryMatcher ("DELETE" , "repos/OWNER/REPO/actions/caches" , url.Values {
315+ "key" : []string {"cache-key" },
316+ "ref" : []string {"refs/heads/main" },
317+ }),
318+ // The response is a JSON object but we don't need it here.
319+ httpmock .StatusStringResponse (200 , "{}" ),
320+ )
321+ },
322+ tty : false ,
323+ wantStdout : "" ,
324+ },
325+ {
326+ // As of now, the API returns HTTP 404 for invalid or non-existent refs.
327+ name : "cache key exists but ref is invalid/not-found" ,
328+ opts : DeleteOptions {Identifier : "existing-cache-key" , Ref : "invalid-ref" },
329+ stubs : func (reg * httpmock.Registry ) {
330+ reg .Register (
331+ httpmock .QueryMatcher ("DELETE" , "repos/OWNER/REPO/actions/caches" , url.Values {
332+ "key" : []string {"existing-cache-key" },
333+ "ref" : []string {"invalid-ref" },
334+ }),
335+ httpmock .StatusStringResponse (404 , "" ),
336+ )
337+ },
338+ wantErr : true ,
339+ wantErrMsg : "X Could not find a cache matching existing-cache-key (with ref invalid-ref) in OWNER/REPO" ,
340+ },
266341 }
267342
268343 for _ , tt := range tests {
0 commit comments