Skip to content

Commit

Permalink
fix: test
Browse files Browse the repository at this point in the history
  • Loading branch information
kj455 committed Jan 4, 2025
1 parent fd5c030 commit ba65d7f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
7 changes: 5 additions & 2 deletions pkg/file/file_mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ type FileMgrImpl struct {

func NewFileMgr(dbDir string, blockSize int) *FileMgrImpl {
_, err := os.Stat(dbDir)
fileExists := !os.IsNotExist(err)
notExists := os.IsNotExist(err)
if notExists {
_ = os.MkdirAll(dbDir, 0755)
}

return &FileMgrImpl{
dbDir: dbDir,
blockSize: blockSize,
isNew: fileExists,
isNew: notExists,
openFiles: make(map[string]*os.File),
}
}
Expand Down
20 changes: 11 additions & 9 deletions pkg/file/file_mgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,30 @@ import (
"path/filepath"
"testing"

"github.com/kj455/simple-db/pkg/testutil"
"github.com/stretchr/testify/assert"
)

func TestNewFileMgr(t *testing.T) {
t.Parallel()
const (
dbDir = "test"
blockSize = 4096
)
const blockSize = 4096
t.Run("new", func(t *testing.T) {
t.Parallel()
dbDir := "test_new_file_mgr_new"
mgr := NewFileMgr(dbDir, blockSize)
t.Cleanup(func() {
os.RemoveAll(dbDir)
})
assert.Equal(t, dbDir, mgr.dbDir)
assert.Equal(t, blockSize, mgr.blockSize)
assert.False(t, mgr.isNew)
assert.True(t, mgr.isNew)
})
t.Run("existing", func(t *testing.T) {
t.Parallel()
os.Mkdir(dbDir, 0755)
defer os.RemoveAll(dbDir)
mgr := NewFileMgr(dbDir, blockSize)
assert.True(t, mgr.isNew)
dir, cleanup := testutil.SetupDir("test_new_file_mgr_existing")
t.Cleanup(cleanup)
mgr := NewFileMgr(dir, blockSize)
assert.False(t, mgr.isNew)
})
}

Expand Down

0 comments on commit ba65d7f

Please sign in to comment.