Repository: dragonflyoss/Dragonfly2
Pull request #1136 (2022-03-08 09:24:25)

add dfpath error more message
Signed-off-by: sunwp <244372610@qq.com>

<!--- Provide a general summary of your changes in the Title above -->

## Description

<!--- Describe your changes in detail -->

## Related Issue

<!--- This project only accepts pull requests related to open issues -->
<!--- If suggesting a new feature or change, please discuss it in an issue first -->
<!--- If fixing a bug, there should be an issue describing it with steps to reproduce -->
<!--- Please link to the issue here: -->

## Motivation and Context

<!--- Why is this change required? What problem does it solve? -->

## Screenshots (if appropriate)

## Types of changes

<!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: -->

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)
- [ ] Documentation Update (if none of the other choices apply)

## Checklist

<!--- Go over all the following points, and put an `x` in all the boxes that apply. -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! -->

- [ ] Code compiles correctly.
- [ ] My code follows the code style of this project.
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [ ] I have read the **CONTRIBUTING** document.
- [ ] I have added tests to cover my changes.
- [ ] All new and existing tests passed.


codecov-commenter: # [Codecov](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1136?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss) Report
> Merging [#1136](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1136?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss) (1506b86) into [main](https://codecov.io/gh/dragonflyoss/Dragonfly2/commit/d1a4c9c91bb7031169512f05a8c603f4feab9e09?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss) (d1a4c9c) will **decrease** coverage by `0.35%`.
> The diff coverage is `40.00%`.

[![Impacted file tree graph](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1136/graphs/tree.svg?width=650&height=150&src=pr&token=PUICJYSAT7&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss)](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1136?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss)

```diff
@@            Coverage Diff             @@
##             main    #1136      +/-   ##
==========================================
- Coverage   45.87%   45.52%   -0.36%     
==========================================
  Files         130      130              
  Lines       10034    10037       +3     
==========================================
- Hits         4603     4569      -34     
- Misses       4909     4957      +48     
+ Partials      522      511      -11     
```

| Flag | Coverage Δ | |
|---|---|---|
| Object-compatibility-e2etests | `?` | |
| e2etests | `?` | |
| unittests | `45.52% <40.00%> (-0.36%)` | :arrow_down: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1136?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss) | Coverage Δ | |
|---|---|---|
| [pkg/dfpath/dfpath.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1136/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-cGtnL2RmcGF0aC9kZnBhdGguZ28=) | `51.42% <40.00%> (+2.94%)` | :arrow_up: |
| [client/daemon/peer/peertask\_piecetask\_poller.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1136/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-Y2xpZW50L2RhZW1vbi9wZWVyL3BlZXJ0YXNrX3BpZWNldGFza19wb2xsZXIuZ28=) | `28.96% <0.00%> (-21.38%)` | :arrow_down: |
| [cdn/supervisor/progress/progress.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1136/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-Y2RuL3N1cGVydmlzb3IvcHJvZ3Jlc3MvcHJvZ3Jlc3MuZ28=) | `90.62% <0.00%> (-6.25%)` | :arrow_down: |
| [client/daemon/peer/peertask\_conductor.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1136/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-Y2xpZW50L2RhZW1vbi9wZWVyL3BlZXJ0YXNrX2NvbmR1Y3Rvci5nbw==) | `56.85% <0.00%> (-0.73%)` | :arrow_down: |
| [scheduler/resource/cdn.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1136/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-c2NoZWR1bGVyL3Jlc291cmNlL2Nkbi5nbw==) | `62.29% <0.00%> (+0.31%)` | :arrow_up: |
| [scheduler/resource/host\_manager\_mock.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1136/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-c2NoZWR1bGVyL3Jlc291cmNlL2hvc3RfbWFuYWdlcl9tb2NrLmdv) | `59.37% <0.00%> (+12.50%)` | :arrow_up: |


Patch:
diff --git a/pkg/dfpath/dfpath.go b/pkg/dfpath/dfpath.go
index 75921fb8..a8407f1a 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


Test patch:
diff --git a/pkg/dfpath/dfpath_test.go b/pkg/dfpath/dfpath_test.go
index 16c17769..16975028 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)
 			},
 		},