From 7b744c299e220d0e532dadbe59f4ea13b1c67444 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Mon, 10 Jul 2023 10:13:23 +0800 Subject: [PATCH] fix: fix bug of CreateDir --- docs/fileutil.md | 4 ++-- docs/fileutil_zh-CN.md | 4 ++-- fileutil/file.go | 3 ++- fileutil/file_example_test.go | 14 ++++++++++---- fileutil/file_test.go | 6 ++++-- 5 files changed, 20 insertions(+), 11 deletions(-) diff --git a/docs/fileutil.md b/docs/fileutil.md index 70e591e..4025ecd 100644 --- a/docs/fileutil.md +++ b/docs/fileutil.md @@ -108,7 +108,7 @@ func main() { ### CreateDir -

Create directory in absolute path. param `absPath` like /a/, /a/b/.

+

Create directory in absolute path. param `absPath` like /a, /a/b.

Signature: @@ -127,7 +127,7 @@ import ( ) func main() { - err := fileutil.CreateDir("/a/") + err := fileutil.CreateDir("/a/b") // will create folder /a/b fmt.Println(err) } ``` diff --git a/docs/fileutil_zh-CN.md b/docs/fileutil_zh-CN.md index b68cbd1..57f6f8c 100644 --- a/docs/fileutil_zh-CN.md +++ b/docs/fileutil_zh-CN.md @@ -108,7 +108,7 @@ func main() { ### CreateDir -

使用绝对路径创建嵌套目录,例如/a/, /a/b/

+

使用绝对路径创建嵌套目录,例如/a/, /a/b

函数签名: @@ -127,7 +127,7 @@ import ( ) func main() { - err := fileutil.CreateDir("/a/") + err := fileutil.CreateDir("/a/b") // will create folder /a/b fmt.Println(err) } ``` diff --git a/fileutil/file.go b/fileutil/file.go index d0e48cb..7ed2b38 100644 --- a/fileutil/file.go +++ b/fileutil/file.go @@ -52,7 +52,8 @@ func CreateFile(path string) bool { // CreateDir create directory in absolute path. param `absPath` like /a/, /a/b/. // Play: https://go.dev/play/p/qUuCe1OGQnM func CreateDir(absPath string) error { - return os.MkdirAll(path.Dir(absPath), os.ModePerm) + // return os.MkdirAll(path.Dir(absPath), os.ModePerm) + return os.MkdirAll(absPath, os.ModePerm) } // IsDir checks if the path is directory or not. diff --git a/fileutil/file_example_test.go b/fileutil/file_example_test.go index ece96a6..9bca78b 100644 --- a/fileutil/file_example_test.go +++ b/fileutil/file_example_test.go @@ -39,7 +39,7 @@ func ExampleCreateFile() { func ExampleCreateDir() { pwd, _ := os.Getwd() - dirPath := pwd + "/test_xxx/" + dirPath := pwd + "/createdir/a/b" result1 := IsExist(dirPath) @@ -48,16 +48,22 @@ func ExampleCreateDir() { return } - result2 := IsExist(dirPath) - - os.Remove(dirPath) + result2 := IsExist(pwd + "/createdir/") + result3 := IsExist(pwd + "/createdir/a") + result4 := IsExist(pwd + "/createdir/a/b") fmt.Println(result1) fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + os.RemoveAll(pwd + "/createdir/") // Output: // false // true + // true + // true } func ExampleIsDir() { diff --git a/fileutil/file_test.go b/fileutil/file_test.go index 70f907d..c16267c 100644 --- a/fileutil/file_test.go +++ b/fileutil/file_test.go @@ -50,7 +50,7 @@ func TestCreateDir(t *testing.T) { t.FailNow() } - dirPath := pwd + "/a/" + dirPath := pwd + "/a/b" err = CreateDir(dirPath) if err != nil { t.Error(err) @@ -58,7 +58,9 @@ func TestCreateDir(t *testing.T) { } assert.Equal(true, IsExist(dirPath)) - os.Remove(dirPath) + + os.RemoveAll(pwd + "/a") + assert.Equal(false, IsExist(dirPath)) }