io: move common function and add unit tests for it
This commit is contained in:
parent
11ce73af28
commit
d799c98cfe
4 changed files with 61 additions and 17 deletions
|
@ -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"))
|
||||
|
|
|
@ -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 {
|
||||
|
|
18
pkg/io/fileWriter.go
Normal file
18
pkg/io/fileWriter.go
Normal 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
39
pkg/io/fileWriter_test.go
Normal 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)
|
||||
}
|
Loading…
Reference in a new issue