From d799c98cfec93d9adcc4edefbbda062b7b7cfe25 Mon Sep 17 00:00:00 2001 From: Vsevolod Brekelov Date: Wed, 6 Nov 2019 17:12:33 +0300 Subject: [PATCH] io: move common function and add unit tests for it --- cli/server/server.go | 13 +---------- pkg/core/storage/boltdb_store.go | 8 +++---- pkg/io/fileWriter.go | 18 +++++++++++++++ pkg/io/fileWriter_test.go | 39 ++++++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 17 deletions(-) create mode 100644 pkg/io/fileWriter.go create mode 100644 pkg/io/fileWriter_test.go diff --git a/cli/server/server.go b/cli/server/server.go index 56a3b1215..141e3220a 100644 --- a/cli/server/server.go +++ b/cli/server/server.go @@ -5,7 +5,6 @@ import ( "fmt" "os" "os/signal" - "path" "github.com/CityOfZion/neo-go/config" "github.com/CityOfZion/neo-go/pkg/core" @@ -117,7 +116,7 @@ func handleLoggingParams(ctx *cli.Context, cfg config.ApplicationConfiguration) } if logPath := cfg.LogPath; logPath != "" { - if err := makeDir(logPath); err != nil { + if err := io.MakeDirForFile(logPath, "logger"); err != nil { return err } f, err := os.Create(logPath) @@ -129,16 +128,6 @@ func handleLoggingParams(ctx *cli.Context, cfg config.ApplicationConfiguration) return nil } -func makeDir(filePath string) error { - fileName := filePath - dir := path.Dir(fileName) - err := os.MkdirAll(dir, os.ModePerm) - if err != nil { - return fmt.Errorf("could not create dir for logger: %v", err) - } - return nil -} - func getCountAndSkipFromContext(ctx *cli.Context) (uint32, uint32) { count := uint32(ctx.Uint("count")) skip := uint32(ctx.Uint("skip")) diff --git a/pkg/core/storage/boltdb_store.go b/pkg/core/storage/boltdb_store.go index 93a8ae6e9..c0c1e5f9e 100644 --- a/pkg/core/storage/boltdb_store.go +++ b/pkg/core/storage/boltdb_store.go @@ -4,8 +4,8 @@ import ( "bytes" "fmt" "os" - "path" + "github.com/CityOfZion/neo-go/pkg/io" "github.com/etcd-io/bbolt" log "github.com/sirupsen/logrus" "github.com/syndtr/goleveldb/leveldb/util" @@ -30,10 +30,8 @@ func NewBoltDBStore(cfg BoltDBOptions) (*BoltDBStore, error) { var opts *bbolt.Options // should be exposed via BoltDBOptions if anything needed fileMode := os.FileMode(0600) // should be exposed via BoltDBOptions if anything needed fileName := cfg.FilePath - dir := path.Dir(fileName) - err := os.MkdirAll(dir, os.ModePerm) - if err != nil { - return nil, fmt.Errorf("could not create dir for BoltDB: %v", err) + if err := io.MakeDirForFile(fileName, "BoltDB"); err != nil { + return nil, err } db, err := bbolt.Open(fileName, fileMode, opts) if err != nil { diff --git a/pkg/io/fileWriter.go b/pkg/io/fileWriter.go new file mode 100644 index 000000000..35d87f34f --- /dev/null +++ b/pkg/io/fileWriter.go @@ -0,0 +1,18 @@ +package io + +import ( + "fmt" + "os" + "path" +) + +// MakeDirForFile creates directory provided in filePath. +func MakeDirForFile(filePath string, creator string) error { + fileName := filePath + dir := path.Dir(fileName) + err := os.MkdirAll(dir, os.ModePerm) + if err != nil { + return fmt.Errorf("could not create dir for %s: %v", creator, err) + } + return nil +} diff --git a/pkg/io/fileWriter_test.go b/pkg/io/fileWriter_test.go new file mode 100644 index 000000000..a2c8f7e2d --- /dev/null +++ b/pkg/io/fileWriter_test.go @@ -0,0 +1,39 @@ +package io + +import ( + "io/ioutil" + "os" + "path" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestMakeDirForFile_HappyPath(t *testing.T) { + tempDir, err := ioutil.TempDir("", "test") + require.NoError(t, err) + + filePath := tempDir + "/testDir/testFile.test" + err = MakeDirForFile(filePath, "test") + defer removeDir(t, tempDir) + require.NoError(t, err) + + _, errChDir := os.Create(filePath) + require.NoError(t, errChDir) +} + +func removeDir(t *testing.T, dirName string) { + err := os.RemoveAll(dirName) + require.NoError(t, err) +} + +func TestMakeDirForFile_Negative(t *testing.T) { + file, err := ioutil.TempFile("", "test") + require.NoError(t, err) + + filePath := file.Name() + "/error" + dir := path.Dir(filePath) + err = MakeDirForFile(filePath, "test") + defer removeDir(t, dir) + require.Errorf(t, err, "could not create dir for test: mkdir %s : not a directory", filePath) +}