fix: test case issue

This commit is contained in:
Marvin Zhang
2024-11-19 17:40:44 +08:00
parent 3dc66e48db
commit 98a9c907c9
5 changed files with 47 additions and 40 deletions

View File

@@ -2,9 +2,14 @@ package main
import (
"github.com/crawlab-team/crawlab/core/cmd"
"github.com/crawlab-team/crawlab/core/config"
"github.com/crawlab-team/crawlab/core/utils"
)
func init() {
config.InitConfig()
}
func main() {
go func() {
err := cmd.Execute()

View File

@@ -3,45 +3,18 @@ package config
import (
"errors"
"strings"
"sync"
"github.com/apex/log"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
)
func init() {
// config instance
c := Config{Name: ""}
// init config file
if err := c.Init(); err != nil {
log.Warn("unable to init config")
return
}
// watch config change and load responsively
c.WatchConfig()
// init log level
c.initLogLevel()
}
type Config struct {
Name string
}
type InitConfigOptions struct {
Name string
}
func (c *Config) WatchConfig() {
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
log.Infof("Config file changed: %s", e.Name)
})
}
func (c *Config) Init() (err error) {
func (c *Config) Init() {
// Set default values
c.setDefaults()
@@ -74,7 +47,15 @@ func (c *Config) Init() (err error) {
}
}
return nil
// init log level
c.initLogLevel()
}
func (c *Config) WatchConfig() {
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
log.Infof("Config file changed: %s", e.Name)
})
}
func (c *Config) setDefaults() {
@@ -95,3 +76,26 @@ func (c *Config) initLogLevel() {
}
log.SetLevel(l)
}
func newConfig() *Config {
return &Config{}
}
var _config *Config
var _configOnce sync.Once
func GetConfig() *Config {
_configOnce.Do(func() {
_config = newConfig()
_config.Init()
})
return _config
}
func InitConfig() {
// config instance
c := GetConfig()
// watch config change and load responsively
c.WatchConfig()
}

View File

@@ -20,11 +20,7 @@ func init() {
func TestInitConfig(t *testing.T) {
// Create a new Config instance
c := Config{Name: ""}
// Initialize the config
err := c.Init()
require.NoError(t, err, "Failed to initialize config")
InitConfig()
// Test default values
assert.Equal(t, "localhost", viper.GetString("mongo.host"), "Unexpected default value for mongo.host")
@@ -58,8 +54,7 @@ server:
// Create a new Config instance with the config file
cWithFile := Config{Name: configPath}
err = cWithFile.Init()
require.NoError(t, err, "Failed to initialize config with file")
cWithFile.Init()
// Test values from config file
assert.Equal(t, "global.edition.pro", viper.GetString("edition"), "Unexpected value for edition from config file")

View File

@@ -457,8 +457,6 @@ func newTaskHandlerService() *Service {
// grpc client
svc.c = grpcclient.GetGrpcClient()
log.Debugf("[NewTaskHandlerService] svc[cfgPath: %s]", svc.cfgSvc.GetConfigPath())
return svc
}

View File

@@ -81,10 +81,15 @@ func IsPro() bool {
}
func GetWorkspace() string {
homedirPath, err := homedir.Dir()
if err != nil {
log.Warnf("cannot find home directory: %v", err)
return DefaultWorkspace
}
if res := viper.GetString("workspace"); res != "" {
return res
}
return DefaultWorkspace
return filepath.Join(homedirPath, DefaultWorkspace)
}
func GetTaskLogPath() string {