Skip to content

Commit

Permalink
tests: remove files generated during tests (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez authored Jun 30, 2024
1 parent 2bc2890 commit 41cd564
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 20 deletions.
8 changes: 5 additions & 3 deletions flock.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,10 @@ func (f *Flock) setFh() error {

// ensure the file handle is closed if no lock is held.
func (f *Flock) ensureFhState() {
if !f.l && !f.r && f.fh != nil {
f.fh.Close()
f.fh = nil
if f.l || f.r || f.fh == nil {
return
}

f.fh.Close()
f.fh = nil
}
35 changes: 20 additions & 15 deletions flock_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,32 @@ package flock
import (
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func Test(t *testing.T) {
tmpFileFh, _ := os.CreateTemp(os.TempDir(), "go-flock-")
tmpFileFh.Close()
tmpFile := tmpFileFh.Name()
os.Remove(tmpFile)
tmpFile, err := os.CreateTemp(t.TempDir(), "go-flock-")
require.NoError(t, err)

tmpFile.Close()
os.Remove(tmpFile.Name())

lock := New(tmpFile.Name())

lock := New(tmpFile)
locked, err := lock.TryLock()
if locked == false || err != nil {
t.Fatalf("failed to lock: locked: %t, err: %v", locked, err)
}
require.NoError(t, err)
require.True(t, locked)

newLock := New(tmpFile.Name())

newLock := New(tmpFile)
locked, err = newLock.TryLock()
if locked != false || err != nil {
t.Fatalf("should have failed locking: locked: %t, err: %v", locked, err)
}
require.NoError(t, err)
require.False(t, locked)

assert.Nil(t, newLock.fh, "file handle should have been released and be nil")

if newLock.fh != nil {
t.Fatal("file handle should have been released and be nil")
}
err = lock.Unlock()
require.NoError(t, err)
}
2 changes: 1 addition & 1 deletion flock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type TestSuite struct {
func Test(t *testing.T) { suite.Run(t, &TestSuite{}) }

func (s *TestSuite) SetupTest() {
tmpFile, err := os.CreateTemp(os.TempDir(), "go-flock-")
tmpFile, err := os.CreateTemp(s.T().TempDir(), "go-flock-")
s.Require().NoError(err)

s.Require().NotNil(tmpFile)
Expand Down
1 change: 0 additions & 1 deletion flock_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ func (f *Flock) try(locked *bool, flag uint32) (bool, error) {
}

_, errNo := lockFileEx(syscall.Handle(f.fh.Fd()), flag|winLockfileFailImmediately, 0, 1, 0, &syscall.Overlapped{})

if errNo > 0 {
if errNo == ErrorLockViolation || errNo == syscall.ERROR_IO_PENDING {
return false, nil
Expand Down

0 comments on commit 41cd564

Please sign in to comment.