-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add: file system directory utils testing.
- Loading branch information
Showing
8 changed files
with
146 additions
and
53 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package utils | ||
|
||
import "os" | ||
|
||
// IsDirExist checked directory is exist | ||
func IsDirExist(dirPath string) bool { | ||
// 使用 os.Stat 检查目录是否存在 | ||
_, err := os.Stat(dirPath) | ||
if err != nil && os.IsNotExist(err) { | ||
// 如果 err 不为 nil 并且是目录不存在错误返回 false | ||
return false | ||
} | ||
// 如果 err 为 nil 或者是其他类型的错误,权限问题则返回 true | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package utils | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestIsDirExist(t *testing.T) { | ||
// 测试存在的目录 | ||
existingDir := os.TempDir() | ||
exists := IsDirExist(existingDir) | ||
if !exists { | ||
t.Errorf("Expected directory %s to exist, but it does not.", existingDir) | ||
} | ||
|
||
// 测试不存在的目录 | ||
nonExistingDir := "/aaa/bbb/cccc/directory" | ||
exists = IsDirExist(nonExistingDir) | ||
if exists { | ||
t.Errorf("Expected directory %s to not exist, but it does.", nonExistingDir) | ||
} | ||
|
||
// 测试无效路径 | ||
invalidPath := "/invalid/path" | ||
exists = IsDirExist(invalidPath) | ||
if exists { | ||
t.Errorf("Expected directory %s to not exist, but it does.", invalidPath) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters