From 612905f4ed7166cd8cb68b852c237bc2cfd87f2c Mon Sep 17 00:00:00 2001 From: sunwp <244372610@qq.com> Date: Fri, 18 Mar 2022 16:18:35 +0800 Subject: [PATCH] add dfpath error more message (#1136) Signed-off-by: sunwp <244372610@qq.com> Co-authored-by: Gaius --- pkg/dfpath/dfpath.go | 15 +++++++++------ pkg/dfpath/dfpath_test.go | 24 +++++++++++++++++++++--- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/pkg/dfpath/dfpath.go b/pkg/dfpath/dfpath.go index 75921fb8eda..a8407f1a9be 100644 --- a/pkg/dfpath/dfpath.go +++ b/pkg/dfpath/dfpath.go @@ -20,6 +20,8 @@ import ( "path/filepath" "sync" + "github.com/pkg/errors" + "d7y.io/dragonfly/v2/pkg/util/fileutils" ) @@ -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 @@ -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 diff --git a/pkg/dfpath/dfpath_test.go b/pkg/dfpath/dfpath_test.go index 16c17769f19..1697502834e 100644 --- a/pkg/dfpath/dfpath_test.go +++ b/pkg/dfpath/dfpath_test.go @@ -17,6 +17,7 @@ package dfpath import ( + "sync" "testing" "github.com/stretchr/testify/assert" @@ -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) @@ -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) @@ -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) }, @@ -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) }, },