Skip to content

Commit

Permalink
Fixed firecore.GetTmpDir not returning tmpDir after first call if…
Browse files Browse the repository at this point in the history
… already created
  • Loading branch information
maoueh committed Jan 16, 2025
1 parent 9be23e0 commit 65096c9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ var indexStoreCreated bool
var tmpDirCreated bool

func GetTmpDir(dataDir string) (tmpDir string, err error) {
tmpDir = MustReplaceDataDir(dataDir, viperExpandedEnvGetString("common-tmp-dir"))
if tmpDirCreated {
return
}

tmpDir = MustReplaceDataDir(dataDir, viperExpandedEnvGetString("common-tmp-dir"))
err = os.MkdirAll(tmpDir, 0755)
tmpDirCreated = true
return
Expand Down
23 changes: 23 additions & 0 deletions storage_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package firecore

import (
"path/filepath"
"testing"

"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -47,3 +49,24 @@ func Test_searchBlockNum(t *testing.T) {
func uptr(v uint64) *uint64 {
return &v
}

func TestGetTmpDir(t *testing.T) {
dataDir := t.TempDir()
testSetViper(t, "common-tmp-dir", "{data-dir}/value")

dir, err := GetTmpDir(dataDir)
assert.NoError(t, err)
assert.Equal(t, filepath.Join(dataDir, "value"), dir)

dir2, err2 := GetTmpDir(dataDir)
assert.NoError(t, err2)
assert.Equal(t, filepath.Join(dataDir, "value"), dir2)
}

func testSetViper(t *testing.T, key string, value string) {
current := viper.Get(key)
t.Cleanup(func() {
viper.Set(key, current)
})
viper.Set(key, value)
}

0 comments on commit 65096c9

Please sign in to comment.