io: move common function and add unit tests for it

This commit is contained in:
Vsevolod Brekelov 2019-11-06 17:12:33 +03:00
parent 11ce73af28
commit d799c98cfe
4 changed files with 61 additions and 17 deletions

View file

@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"os" "os"
"os/signal" "os/signal"
"path"
"github.com/CityOfZion/neo-go/config" "github.com/CityOfZion/neo-go/config"
"github.com/CityOfZion/neo-go/pkg/core" "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 logPath := cfg.LogPath; logPath != "" {
if err := makeDir(logPath); err != nil { if err := io.MakeDirForFile(logPath, "logger"); err != nil {
return err return err
} }
f, err := os.Create(logPath) f, err := os.Create(logPath)
@ -129,16 +128,6 @@ func handleLoggingParams(ctx *cli.Context, cfg config.ApplicationConfiguration)
return nil 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) { func getCountAndSkipFromContext(ctx *cli.Context) (uint32, uint32) {
count := uint32(ctx.Uint("count")) count := uint32(ctx.Uint("count"))
skip := uint32(ctx.Uint("skip")) skip := uint32(ctx.Uint("skip"))

View file

@ -4,8 +4,8 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"os" "os"
"path"
"github.com/CityOfZion/neo-go/pkg/io"
"github.com/etcd-io/bbolt" "github.com/etcd-io/bbolt"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/syndtr/goleveldb/leveldb/util" "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 var opts *bbolt.Options // should be exposed via BoltDBOptions if anything needed
fileMode := os.FileMode(0600) // should be exposed via BoltDBOptions if anything needed fileMode := os.FileMode(0600) // should be exposed via BoltDBOptions if anything needed
fileName := cfg.FilePath fileName := cfg.FilePath
dir := path.Dir(fileName) if err := io.MakeDirForFile(fileName, "BoltDB"); err != nil {
err := os.MkdirAll(dir, os.ModePerm) return nil, err
if err != nil {
return nil, fmt.Errorf("could not create dir for BoltDB: %v", err)
} }
db, err := bbolt.Open(fileName, fileMode, opts) db, err := bbolt.Open(fileName, fileMode, opts)
if err != nil { if err != nil {

18
pkg/io/fileWriter.go Normal file
View file

@ -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
}

39
pkg/io/fileWriter_test.go Normal file
View file

@ -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)
}