Skip to content

Commit 5aef0ea

Browse files
authored
Wrap file closer jfrog/closer
Wrap file closer
2 parents 041a424 + 4077a01 commit 5aef0ea

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

.github/workflows/analysis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
runs-on: ubuntu-latest
1212
steps:
1313
- name: Checkout Source
14-
uses: actions/checkout@v3
14+
uses: actions/checkout@v4
1515

1616
- name: Install Go
1717
uses: actions/setup-go@v3
@@ -29,7 +29,7 @@ jobs:
2929
runs-on: ubuntu-latest
3030
steps:
3131
- name: Checkout Source
32-
uses: actions/checkout@v3
32+
uses: actions/checkout@v4
3333

3434
- name: Install Go
3535
uses: actions/setup-go@v3

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
uses: actions/checkout@v4
1919

2020
- name: Go Cache
21-
uses: actions/cache@v3
21+
uses: actions/cache@v4
2222
with:
2323
path: ~/go/pkg/mod
2424
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}

io/fileutils.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
cr "crypto/rand"
66
"errors"
77
"fmt"
8+
"io"
89
"math/rand"
910
"os"
1011
"path/filepath"
@@ -236,3 +237,10 @@ func GetFileInfo(path string, followSymlink bool) (fileInfo os.FileInfo, err err
236237
// We should not do CheckError here, because the error is checked by the calling functions.
237238
return fileInfo, err
238239
}
240+
241+
// Close the reader/writer and append the error to the given error.
242+
func Close(closer io.Closer, err *error) {
243+
if closeErr := closer.Close(); closeErr != nil {
244+
*err = errors.Join(*err, fmt.Errorf("failed to close %T: %w", closer, closeErr))
245+
}
246+
}

io/fileutils_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package io
2+
3+
import (
4+
"errors"
5+
"github.com/stretchr/testify/assert"
6+
"os"
7+
"path"
8+
"strings"
9+
"testing"
10+
)
11+
12+
func TestClose(t *testing.T) {
13+
var err error
14+
t.TempDir()
15+
f, err := os.Create(path.Join(t.TempDir(), "test"))
16+
assert.NoError(t, err)
17+
18+
Close(f, &err)
19+
assert.NoError(t, err)
20+
21+
// Try closing the same file again and expect error
22+
Close(f, &err)
23+
assert.Error(t, err)
24+
25+
// Check that both errors are aggregated
26+
err = errors.New("original error")
27+
Close(f, &err)
28+
assert.Len(t, strings.Split(err.Error(), "\n"), 2)
29+
}

0 commit comments

Comments
 (0)