diff --git a/pkg/file/file_mgr.go b/pkg/file/file_mgr.go index b18efa9..c5c4df1 100644 --- a/pkg/file/file_mgr.go +++ b/pkg/file/file_mgr.go @@ -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), } } diff --git a/pkg/file/file_mgr_test.go b/pkg/file/file_mgr_test.go index 43b55d5..17c3ca3 100644 --- a/pkg/file/file_mgr_test.go +++ b/pkg/file/file_mgr_test.go @@ -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) }) }