1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-15 18:22:27 +08:00

fix: fix bug of CreateDir

This commit is contained in:
dudaodong
2023-07-10 10:13:23 +08:00
parent 154ec56780
commit 7b744c299e
5 changed files with 20 additions and 11 deletions

View File

@@ -108,7 +108,7 @@ func main() {
### <span id="CreateDir">CreateDir</span> ### <span id="CreateDir">CreateDir</span>
<p>Create directory in absolute path. param `absPath` like /a/, /a/b/.</p> <p>Create directory in absolute path. param `absPath` like /a, /a/b.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -127,7 +127,7 @@ import (
) )
func main() { func main() {
err := fileutil.CreateDir("/a/") err := fileutil.CreateDir("/a/b") // will create folder /a/b
fmt.Println(err) fmt.Println(err)
} }
``` ```

View File

@@ -108,7 +108,7 @@ func main() {
### <span id="CreateDir">CreateDir</span> ### <span id="CreateDir">CreateDir</span>
<p>使用绝对路径创建嵌套目录,例如/a/, /a/b/</p> <p>使用绝对路径创建嵌套目录,例如/a/, /a/b</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -127,7 +127,7 @@ import (
) )
func main() { func main() {
err := fileutil.CreateDir("/a/") err := fileutil.CreateDir("/a/b") // will create folder /a/b
fmt.Println(err) fmt.Println(err)
} }
``` ```

View File

@@ -52,7 +52,8 @@ func CreateFile(path string) bool {
// CreateDir create directory in absolute path. param `absPath` like /a/, /a/b/. // CreateDir create directory in absolute path. param `absPath` like /a/, /a/b/.
// Play: https://go.dev/play/p/qUuCe1OGQnM // Play: https://go.dev/play/p/qUuCe1OGQnM
func CreateDir(absPath string) error { 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. // IsDir checks if the path is directory or not.

View File

@@ -39,7 +39,7 @@ func ExampleCreateFile() {
func ExampleCreateDir() { func ExampleCreateDir() {
pwd, _ := os.Getwd() pwd, _ := os.Getwd()
dirPath := pwd + "/test_xxx/" dirPath := pwd + "/createdir/a/b"
result1 := IsExist(dirPath) result1 := IsExist(dirPath)
@@ -48,16 +48,22 @@ func ExampleCreateDir() {
return return
} }
result2 := IsExist(dirPath) result2 := IsExist(pwd + "/createdir/")
result3 := IsExist(pwd + "/createdir/a")
os.Remove(dirPath) result4 := IsExist(pwd + "/createdir/a/b")
fmt.Println(result1) fmt.Println(result1)
fmt.Println(result2) fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
os.RemoveAll(pwd + "/createdir/")
// Output: // Output:
// false // false
// true // true
// true
// true
} }
func ExampleIsDir() { func ExampleIsDir() {

View File

@@ -50,7 +50,7 @@ func TestCreateDir(t *testing.T) {
t.FailNow() t.FailNow()
} }
dirPath := pwd + "/a/" dirPath := pwd + "/a/b"
err = CreateDir(dirPath) err = CreateDir(dirPath)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
@@ -58,7 +58,9 @@ func TestCreateDir(t *testing.T) {
} }
assert.Equal(true, IsExist(dirPath)) assert.Equal(true, IsExist(dirPath))
os.Remove(dirPath)
os.RemoveAll(pwd + "/a")
assert.Equal(false, IsExist(dirPath)) assert.Equal(false, IsExist(dirPath))
} }