Skip to content

Commit

Permalink
add dfpath error more message (#1136)
Browse files Browse the repository at this point in the history
Signed-off-by: sunwp <[email protected]>
Co-authored-by: Gaius <[email protected]>
  • Loading branch information
244372610 and gaius-qi authored Mar 18, 2022
1 parent a27450c commit 612905f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
15 changes: 9 additions & 6 deletions pkg/dfpath/dfpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"path/filepath"
"sync"

"github.com/pkg/errors"

"d7y.io/dragonfly/v2/pkg/util/fileutils"
)

Expand Down Expand Up @@ -50,8 +52,8 @@ type dfpath struct {
// Cache of the dfpath
var cache struct {
sync.Once
d *dfpath
err error
d *dfpath
errs []error
}

// Option is a functional option for configuring the dfpath
Expand Down Expand Up @@ -105,17 +107,18 @@ func New(options ...Option) (Dfpath, error) {
d.dfgetLockPath = filepath.Join(d.workHome, "dfget.lock")

// Create directories
for _, dir := range []string{d.workHome, d.cacheDir, d.logDir, d.dataDir, d.pluginDir} {
for name, dir := range map[string]string{"workHome": d.workHome, "cacheDir": d.cacheDir, "logDir": d.logDir, "dataDir": d.dataDir,
"pluginDir": d.pluginDir} {
if err := fileutils.MkdirAll(dir); err != nil {
cache.err = err
cache.errs = append(cache.errs, errors.Errorf("create %s dir %s failed: %v", name, dir, err))
}
}

cache.d = d
})

if cache.err != nil {
return nil, cache.err
if len(cache.errs) > 0 {
return nil, errors.Errorf("create dfpath failed: %s", cache.errs)
}

d := *cache.d
Expand Down
24 changes: 21 additions & 3 deletions pkg/dfpath/dfpath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package dfpath

import (
"sync"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -28,10 +29,21 @@ func TestNew(t *testing.T) {
options []Option
expect func(t *testing.T, options []Option)
}{
{
name: "new dfpath failed",
options: []Option{WithLogDir("")},
expect: func(t *testing.T, options []Option) {
assert := assert.New(t)
_, err := New(options...)
assert.Error(err)
},
},
{
name: "new dfpath",
expect: func(t *testing.T, options []Option) {
assert := assert.New(t)
cache.Once = sync.Once{}
cache.errs = []error{}
d, err := New(options...)
assert.NoError(err)
assert.Equal(d.WorkHome(), DefaultWorkHome)
Expand All @@ -45,9 +57,11 @@ func TestNew(t *testing.T) {
options: []Option{WithWorkHome("foo")},
expect: func(t *testing.T, options []Option) {
assert := assert.New(t)
cache.Once = sync.Once{}
cache.errs = []error{}
d, err := New(options...)
assert.NoError(err)
assert.Equal(d.WorkHome(), DefaultWorkHome)
assert.Equal(d.WorkHome(), "foo")
assert.Equal(d.CacheDir(), DefaultCacheDir)
assert.Equal(d.LogDir(), DefaultLogDir)
assert.Equal(d.DataDir(), DefaultDataDir)
Expand All @@ -58,10 +72,12 @@ func TestNew(t *testing.T) {
options: []Option{WithCacheDir("foo")},
expect: func(t *testing.T, options []Option) {
assert := assert.New(t)
cache.Once = sync.Once{}
cache.errs = []error{}
d, err := New(options...)
assert.NoError(err)
assert.Equal(d.WorkHome(), DefaultWorkHome)
assert.Equal(d.CacheDir(), DefaultCacheDir)
assert.Equal(d.CacheDir(), "foo")
assert.Equal(d.LogDir(), DefaultLogDir)
assert.Equal(d.DataDir(), DefaultDataDir)
},
Expand All @@ -71,11 +87,13 @@ func TestNew(t *testing.T) {
options: []Option{WithLogDir("foo")},
expect: func(t *testing.T, options []Option) {
assert := assert.New(t)
cache.Once = sync.Once{}
cache.errs = []error{}
d, err := New(options...)
assert.NoError(err)
assert.Equal(d.WorkHome(), DefaultWorkHome)
assert.Equal(d.CacheDir(), DefaultCacheDir)
assert.Equal(d.LogDir(), DefaultLogDir)
assert.Equal(d.LogDir(), "foo")
assert.Equal(d.DataDir(), DefaultDataDir)
},
},
Expand Down

0 comments on commit 612905f

Please sign in to comment.