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 38b79a3
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 53 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
37 changes: 10 additions & 27 deletions conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"os"
"path/filepath"

"github.com/auula/vasedb/clog"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
)
Expand Down Expand Up @@ -55,21 +54,15 @@ var DefaultConfig *ServerConfig = new(ServerConfig)
var Dirs = []string{"etc", "temp", "data", "index"}

func init() {

// 先读内置默认配置,设置为全局的配置
if err := DefaultConfig.Unmarshal([]byte(DefaultConfigJSON)); err != nil {
// 读取失败直接退出进程,打印对应堆栈信息
clog.Failed(err)
}
_ = DefaultConfig.Unmarshal([]byte(DefaultConfigJSON))

// 当初始化完成之后应该使用此 Settings 配置
if err := Settings.Unmarshal([]byte(DefaultConfigJSON)); err != nil {
clog.Failed(err)
}

_ = Settings.Unmarshal([]byte(DefaultConfigJSON))
}

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

Expand All @@ -87,11 +80,8 @@ func Load(file string, opt *ServerConfig) error {
return v.Unmarshal(&opt)
}

// ReloadConfig 此方法只会在初始化完成之后生效
// 否则找不到相关的配置文件
func ReloadConfig() (*ServerConfig, error) {

var opt ServerConfig
// Reload 此方法只会在初始化完成之后生效,否则找不到相关的配置文件
func Reload(opt *ServerConfig) error {

// 恢复默认的 ${Settings.Path}/etc/config.yaml
v := viper.New()
Expand All @@ -100,26 +90,19 @@ func ReloadConfig() (*ServerConfig, error) {
v.AddConfigPath(filepath.Join(Settings.Path, Dirs[0]))

if err := v.ReadInConfig(); err != nil {
return nil, err
}

if err := v.Unmarshal(&opt); err != nil {
return nil, err
return err
}

return &opt, nil
return v.Unmarshal(&opt)
}

// Saved Settings.Path 存储到磁盘中
func (opt *ServerConfig) Saved(path string) error {

// 将配置对象转换为 YAML 格式的字节数组
yamlData, err := yaml.Marshal(&opt)
if err != nil {
return err
}
yamlData, _ := yaml.Marshal(&opt)

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

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

// 调用 ReloadConfig 函数
reloadedConfig, err := ReloadConfig()
reloadedConfig := new(ServerConfig)
err = Reload(reloadedConfig)
if err != nil {
t.Fatalf("Error reloading config: %v", err)
}
Expand All @@ -104,6 +105,49 @@ func TestReloadConfig(t *testing.T) {
}
}

func TestConfigLoad_Error(t *testing.T) {

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

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

// 创建一个配置文件并写入测试数据
configFile := filepath.Join(tmpDir, "test-config.yaml")

// 调用 Load 函数
loadedConfig := new(ServerConfig)
if err := Load(configFile, loadedConfig); err != nil {
t.Log(err)
}

}

func TestReloadConfig_Error(t *testing.T) {

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

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

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

}

func TestReloadConfig_UnmarshalError(t *testing.T) {

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

}

func TestSavedConfig(t *testing.T) {

// 创建一个临时目录用于测试
Expand Down Expand Up @@ -140,6 +184,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 +215,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 +224,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(nil))
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(nil))
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 All @@ -203,3 +260,17 @@ func TestServerConfig_Marshal(t *testing.T) {
}

}

func TestDefaultConfigInitialization(t *testing.T) {

// 检查 DefaultConfig 是否被正确初始化
if DefaultConfig.Port != 2468 {
t.Errorf("Expected DefaultConfig.Port to be 2468, but got %d", DefaultConfig.Port)
}

// 检查 Settings 是否被正确初始化
if Settings.Port != 2468 {
t.Errorf("Expected Settings.Port to be 2468, but got %d", Settings.Port)
}

}
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 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
}
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 38b79a3

Please sign in to comment.