Skip to content

Commit

Permalink
Merge pull request #2 from lesomnus/fix/rename-after-close
Browse files Browse the repository at this point in the history
rename after close
  • Loading branch information
lesomnus authored Oct 16, 2023
2 parents ad7d1e1 + ae518e7 commit aec6220
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
5 changes: 3 additions & 2 deletions fs_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,9 @@ func (s *fsStore) Put(ctx context.Context, desc Description, r io.Reader) error
return fmt.Errorf("create temp file: %w", err)
}

defer f.Close()
if _, err := io.Copy(f, r); err != nil {
_, err = io.Copy(f, r)
f.Close()
if err != nil {
return err
}

Expand Down
49 changes: 29 additions & 20 deletions fs_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"os"
"path/filepath"
"runtime"
"testing"
"time"

Expand Down Expand Up @@ -74,9 +75,36 @@ func TestFsStore(t *testing.T) {
_, err = os.Stat(work)
require.NoError(err)
})

t.Run("store cannot be closed if the work left", func(t *testing.T) {
require := require.New(t)

store, err := main.NewFsStore(t.TempDir())
require.NoError(err)

r, w := io.Pipe()
go store.Put(context.Background(), DescriptionFoo, r)

time.Sleep(time.Millisecond * 10)
err = store.Close()
require.ErrorContains(err, "not empty")

w.Close()
time.Sleep(time.Millisecond * 10)
err = store.Close()
require.NoError(err)
})
}

func TestFsStoreFail(t *testing.T) {
func TestNewFsStore(t *testing.T) {
if runtime.GOOS == "windows" {
// TODO:
// `NewFsStore` function checks whether a file can be created in the working directory.
// In this test, `NewFsStore`'s check failure is reproduced with Permission,
// but it does not work on Windows (probably because it uses a different form of access control than Unix).
t.Skip("Skipping test on Windows")
}

t.Run("it cannot create store directory", func(t *testing.T) {
require := require.New(t)

Expand Down Expand Up @@ -109,23 +137,4 @@ func TestFsStoreFail(t *testing.T) {
_, err = main.NewFsStore(root, main.WithWorkDir(t.TempDir()))
require.ErrorContains(err, "rename file")
})

t.Run("store cannot be closed if the work left", func(t *testing.T) {
require := require.New(t)

store, err := main.NewFsStore(t.TempDir())
require.NoError(err)

r, w := io.Pipe()
go store.Put(context.Background(), DescriptionFoo, r)

time.Sleep(time.Millisecond * 10)
err = store.Close()
require.ErrorContains(err, "not empty")

w.Close()
time.Sleep(time.Millisecond * 10)
err = store.Close()
require.NoError(err)
})
}

0 comments on commit aec6220

Please sign in to comment.