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 7d71d31
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 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
29 changes: 14 additions & 15 deletions pkg/file/file_mgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,45 @@ 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)
})
}

func TestFileMgr_Read(t *testing.T) {
t.Parallel()
const (
blockSize = 4096
dbDir = "test"
)
const blockSize = 4096
dbDir, cleanup := testutil.SetupDir("test_new_file_mgr_read")
t.Cleanup(cleanup)
mgr := NewFileMgr(dbDir, blockSize)

setupFile := func(fileName string) (f *os.File, cleanup func()) {
testFilepath := filepath.Join(dbDir, fileName)
os.Mkdir(dbDir, 0755)
f, err := os.Create(testFilepath)
assert.NoError(t, err)
cleanup = func() {
os.RemoveAll(dbDir)
os.Remove(testFilepath)
}
return f, cleanup
Expand Down

0 comments on commit 7d71d31

Please sign in to comment.