fix(release): report a failed close and remove a partial asset
What does this MR do and why?
downloadAsset writes a release asset and closes it with the error discarded:
f, err := os.OpenFile(destinationPath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o644)
if err != nil {
return err
}
defer f.Close()
...
_, err = client.Do(r, f)
if err != nil {
return err
}
return nilTwo problems. The success path never checks the close error, so on a filesystem that only reports a buffered write failure at close time, a truncated asset is reported as downloaded. And because the file is only closed on function return, a mid-download failure returns while the partial file stays on disk.
The close is now handled in a deferred function that returns the close error
when the download otherwise succeeded, and removes the file on any failure.
securefile download already handles its close this way, so this follows the
existing pattern rather than inventing one.
Test coverage
Test_downloadAsset_RemovesPartialFileOnErrorStatus points the non-GitLab path
at a server returning 500 and asserts no file is left behind. This is a genuine
behavioural test of the new os.Remove, independent of the refactor.
Test_downloadAsset_SameHost_UsesContextCancellation already exercised a failed
download; it now also asserts the partial file is gone. Both fail on the
unfixed code:
--- FAIL: Test_downloadAsset_SameHost_UsesContextCancellation
--- FAIL: Test_downloadAsset_RemovesPartialFileOnErrorStatusWorth noting: that existing cancellation test was already leaving a partial file
behind on main, it just never checked for it.
go test ./internal/commands/release/download/ is green.
Related issues
Closes #8429 (closed)