Skip to content

Commit

Permalink
add: file system directory utils testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
auula committed Sep 14, 2023
1 parent b448b7a commit 70ae8dd
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 27 deletions.
2 changes: 1 addition & 1 deletion cmd/vasedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func init() {
fl := parseFlags()

// 根据命令行传入的配置文件地址,覆盖掉默认的配置
if conf.IsDefault(fl.config) {
if conf.HasCustomConfig(fl.config) {
if err := conf.Load(fl.config, conf.Settings); err != nil {
clog.Failed(err)
}
Expand Down
5 changes: 3 additions & 2 deletions conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ func init() {

}

func IsDefault(flag string) bool {
// HasCustomConfig checked enable custom config
func HasCustomConfig(flag string) bool {
return flag != defaultFilePath
}

Expand Down Expand Up @@ -119,7 +120,7 @@ func (opt *ServerConfig) Saved(path string) error {
return err
}

// // 将 YAML 数据写入文件
// 将 YAML 数据写入文件
return os.WriteFile(filepath.Join(path, defaultFileName+"."+cfSuffix), yamlData, Permissions)
}

Expand Down
84 changes: 76 additions & 8 deletions conf/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,61 @@ func TestReloadConfig(t *testing.T) {
}
}

func TestReloadConfig_Error(t *testing.T) {

// 创建一个失败目录目录用于测试
tmpDir := t.TempDir() + "/aaa/bbb"

// 设置 Settings.Path 为临时目录
Settings.Path = tmpDir

// 调用 ReloadConfig 函数
if _, err := ReloadConfig(); err != nil && os.IsNotExist(err) {
t.Errorf("reload config error : %v", err)
}

}

func TestReloadConfig_UnmarshalError(t *testing.T) {

// 创建一个临时目录用于测试
tmpDir := t.TempDir()

// 设置 Settings.Path 为临时目录
Settings.Path = tmpDir

// 创建一个配置文件并写入测试数据
configFile := filepath.Join(tmpDir, "etc", "config.yaml")
// 模拟文件中数据
configData := []byte(`
{
"error": {
"port": 8080,
"path": "/test/path",
"debug": true
}
}
`)

// 设置文件系统权限
perm := os.FileMode(0755)

err := os.MkdirAll(filepath.Dir(configFile), perm)
if err != nil {
t.Fatalf("Error creating test directory: %v", err)
}
err = os.WriteFile(configFile, configData, perm)
if err != nil {
t.Fatalf("Error writing test config file: %v", err)
}

// 调用 ReloadConfig 函数
if _, err := ReloadConfig(); err != nil {
t.Log(err)
}

}

func TestSavedConfig(t *testing.T) {

// 创建一个临时目录用于测试
Expand Down Expand Up @@ -140,6 +195,22 @@ func TestSavedConfig(t *testing.T) {
}
}

func TestSavedConfig_Error(t *testing.T) {

// 创建一个临时目录用于测试
tmpDir := t.TempDir()

// 创建一个 ServerConfig 空实例
var config *ServerConfig = nil

// 调用 Saved 函数
err := config.Saved(tmpDir)

if err != nil {
t.Log(err)
}
}

func TestIsDefault(t *testing.T) {
tests := []struct {
name string
Expand All @@ -155,7 +226,7 @@ func TestIsDefault(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsDefault(tt.flag); got != tt.want {
if got := HasCustomConfig(tt.flag); got != tt.want {
t.Errorf("IsDefault() = %v, want %v", got, tt.want)
}
})
Expand All @@ -164,22 +235,19 @@ func TestIsDefault(t *testing.T) {

func TestInit(t *testing.T) {
t.Run("Test DefaultConfig Unmarshal", func(t *testing.T) {
err := DefaultConfig.Unmarshal([]byte(DefaultConfigJSON))
err := DefaultConfig.Unmarshal([]byte(`error:error`))
if err != nil {
t.Errorf("Expected no error, but got %v", err)
t.Log(err)
}
})

t.Run("Test Settings Unmarshal", func(t *testing.T) {
err := Settings.Unmarshal([]byte(DefaultConfigJSON))
err := Settings.Unmarshal([]byte(`error:error`))
if err != nil {
t.Errorf("Expected no error, but got %v", err)
t.Log(err)
}
})

if !reflect.DeepEqual(DefaultConfig, Settings) {
t.Errorf("default config not equal settings. \nGot: %+v\nExpected: %+v", DefaultConfig, Settings)
}
}

func TestServerConfig_Marshal(t *testing.T) {
Expand Down
11 changes: 9 additions & 2 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ echo ""
echo "1: testing cmd package."
echo "2: testing conf package."
echo "3: testing server package."
echo "4: testing codecov coverage."
echo "4: testing utils package."
echo "5: testing codecov coverage."

echo ""

Expand All @@ -29,17 +30,23 @@ function test_all_packages() {
sudo go test -vet=all -race -coverprofile=coverage.out -covermode=atomic -v ./...
}

function test_utils_packages() {
sudo cd utils && go test -v
}

if [ "$case_num" -eq 1 ]; then
test_cmd_package
elif [ "$case_num" -eq 2 ]; then
echo "Testing conf package"
elif [ "$case_num" -eq 3 ]; then
echo "Testing server package"
elif [ "$case_num" -eq 4 ]; then
test_utils_packages
elif [ "$case_num" -eq 5 ]; then
test_all_packages
elif [ "$case_num" -eq 8 ]; then
test_cmd_package
sudo go build -o vasedb
else
echo "Invalid option. Please provide a valid option (1, 2, or 3)."
echo "Invalid option. Please provide a valid option (1, 2, 3, 4, 5, 8)."
fi
15 changes: 15 additions & 0 deletions utils/fs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package utils

import "os"

// IsDirExist checked directory 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
}
29 changes: 29 additions & 0 deletions utils/fs_test.go
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)
}
}
16 changes: 2 additions & 14 deletions vfs/filesys.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/auula/vasedb/clog"
"github.com/auula/vasedb/conf"
"github.com/auula/vasedb/utils"
)

// InitFS build vasedb file system
Expand All @@ -15,7 +16,7 @@ func InitFS(path string) error {
// 拼接文件路径
for _, dir := range conf.Dirs {
// 检查目录是否存在
if dirExist(filepath.Join(path, dir)) {
if utils.IsDirExist(filepath.Join(path, dir)) {
clog.Info(fmt.Sprintf("Initial %s checked successful", dir))
} else {
// 不存在创建对应的目录
Expand All @@ -28,16 +29,3 @@ func InitFS(path string) error {
clog.Info("Initial storage successful")
return nil
}

func dirExist(dirPath string) bool {
// 使用 os.Stat 检查目录是否存在
_, err := os.Stat(dirPath)
if err != nil {
if os.IsNotExist(err) {
return false
}
return false
} else {
return true
}
}

0 comments on commit 70ae8dd

Please sign in to comment.