You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
1.7 KiB
Go

package test
import (
"joybuywork/internal/app"
"joybuywork/internal/config"
"joybuywork/internal/consts"
"joybuywork/internal/repository/dao"
"joybuywork/internal/service/queue/producer"
"fmt"
"os"
"path/filepath"
)
func init() {
configPath, err := findConfigPath()
if err != nil {
panic(err)
}
var filePath = config.FilePath{
ConfigName: "config-dev",
ConfigType: "yaml",
ConfigPath: configPath,
}
err = config.LoadConfig(filePath)
if err != nil {
panic(err)
}
err = app.InitFeiShu()
if err != nil {
panic(err)
}
app.InitLogger()
err = app.InitDB()
if err != nil {
panic(err)
}
// 设置dao的数据库
dao.SetDefault(app.DB.Client)
err = app.InitRedis()
if err != nil {
panic(err)
}
app.InitSubLock()
err = app.InitEtcdCli()
if err != nil {
return
}
//kafka
err = app.InitKafka()
if err != nil {
panic(err)
}
//队列生产者
producer.SetUp()
err = app.InitNode()
if err != nil {
panic(err)
}
consts.RunTest = true
// 固定的奖品数据
err = app.LangDataInit()
if err != nil {
panic(err)
}
}
// findConfigPath 从当前测试包目录向上查找项目配置目录。
// go test 会把工作目录设为被测包目录,因此不能依赖固定层级或 os.Args。
func findConfigPath() (string, error) {
dir, err := os.Getwd()
if err != nil {
return "", fmt.Errorf("get working directory: %w", err)
}
for {
configPath := filepath.Join(dir, "etc", "config-dev.yaml")
if info, statErr := os.Stat(configPath); statErr == nil && !info.IsDir() {
return filepath.Dir(configPath), nil
}
parent := filepath.Dir(dir)
if parent == dir {
return "", fmt.Errorf("etc/config-dev.yaml not found from %s", dir)
}
dir = parent
}
}