mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-07 08:27:35 +08:00
fix: get config
This commit is contained in:
@@ -107,7 +107,7 @@ func (pt *pluginTemplate) generate(tmpl, output string) error {
|
|||||||
func (pt *pluginTemplate) generatePy(output string) error {
|
func (pt *pluginTemplate) generatePy(output string) error {
|
||||||
// specify output file path
|
// specify output file path
|
||||||
if output == "" {
|
if output == "" {
|
||||||
output = filepath.Join(config.RootDir, PluginPySourceGenFile)
|
output = filepath.Join(config.GetConfig().RootDir, PluginPySourceGenFile)
|
||||||
} else if builtin.IsFolderPathExists(output) {
|
} else if builtin.IsFolderPathExists(output) {
|
||||||
output = filepath.Join(output, PluginPySourceGenFile)
|
output = filepath.Join(output, PluginPySourceGenFile)
|
||||||
}
|
}
|
||||||
@@ -155,7 +155,7 @@ func (pt *pluginTemplate) generateGo(output string) error {
|
|||||||
|
|
||||||
// specify output file path
|
// specify output file path
|
||||||
if output == "" {
|
if output == "" {
|
||||||
output = filepath.Join(config.RootDir, PluginHashicorpGoBuiltFile)
|
output = filepath.Join(config.GetConfig().RootDir, PluginHashicorpGoBuiltFile)
|
||||||
} else if builtin.IsFolderPathExists(output) {
|
} else if builtin.IsFolderPathExists(output) {
|
||||||
output = filepath.Join(output, PluginHashicorpGoBuiltFile)
|
output = filepath.Join(output, PluginHashicorpGoBuiltFile)
|
||||||
}
|
}
|
||||||
|
|||||||
+41
-25
@@ -3,6 +3,7 @@ package config
|
|||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
@@ -17,40 +18,55 @@ const (
|
|||||||
ActionLogDirName = "action_log"
|
ActionLogDirName = "action_log"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
type Config struct {
|
||||||
RootDir string
|
RootDir string
|
||||||
ResultsDir string
|
ResultsDir string
|
||||||
ResultsPath string
|
ResultsPath string
|
||||||
DownloadsPath string
|
DownloadsPath string
|
||||||
ScreenShotsPath string
|
ScreenShotsPath string
|
||||||
StartTime = time.Now()
|
StartTime time.Time
|
||||||
StartTimeStr = StartTime.Format("20060102150405")
|
|
||||||
ActionLogFilePath string
|
ActionLogFilePath string
|
||||||
DeviceActionLogFilePath string
|
DeviceActionLogFilePath string
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
globalConfig *Config
|
||||||
|
once sync.Once
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func GetConfig() *Config {
|
||||||
var err error
|
once.Do(func() {
|
||||||
RootDir, err = os.Getwd()
|
cfg := &Config{
|
||||||
if err != nil {
|
StartTime: time.Now(),
|
||||||
panic(err)
|
}
|
||||||
}
|
|
||||||
|
|
||||||
ResultsDir = filepath.Join(ResultsDirName, StartTimeStr)
|
var err error
|
||||||
ResultsPath = filepath.Join(RootDir, ResultsDir)
|
cfg.RootDir, err = os.Getwd()
|
||||||
DownloadsPath = filepath.Join(RootDir, filepath.Join(DownloadsDirName, StartTimeStr))
|
if err != nil {
|
||||||
ScreenShotsPath = filepath.Join(ResultsPath, ScreenshotsDirName)
|
panic(err)
|
||||||
ActionLogFilePath = filepath.Join(ResultsDir, ActionLogDirName)
|
}
|
||||||
DeviceActionLogFilePath = "/sdcard/Android/data/io.appium.uiautomator2.server/files/hodor"
|
|
||||||
|
|
||||||
// create results directory
|
startTimeStr := cfg.StartTime.Format("20060102150405")
|
||||||
if err := builtin.EnsureFolderExists(ResultsPath); err != nil {
|
cfg.ResultsDir = filepath.Join(ResultsDirName, startTimeStr)
|
||||||
log.Fatal().Err(err).Msg("create results directory failed")
|
cfg.ResultsPath = filepath.Join(cfg.RootDir, cfg.ResultsDir)
|
||||||
}
|
cfg.DownloadsPath = filepath.Join(cfg.RootDir, filepath.Join(DownloadsDirName, startTimeStr))
|
||||||
if err := builtin.EnsureFolderExists(DownloadsPath); err != nil {
|
cfg.ScreenShotsPath = filepath.Join(cfg.ResultsPath, ScreenshotsDirName)
|
||||||
log.Fatal().Err(err).Msg("create results directory failed")
|
cfg.ActionLogFilePath = filepath.Join(cfg.ResultsDir, ActionLogDirName)
|
||||||
}
|
cfg.DeviceActionLogFilePath = "/sdcard/Android/data/io.appium.uiautomator2.server/files/hodor"
|
||||||
if err := builtin.EnsureFolderExists(ScreenShotsPath); err != nil {
|
|
||||||
log.Fatal().Err(err).Msg("create screenshots directory failed")
|
// create results directory
|
||||||
}
|
if err := builtin.EnsureFolderExists(cfg.ResultsPath); err != nil {
|
||||||
|
log.Fatal().Err(err).Msg("create results directory failed")
|
||||||
|
}
|
||||||
|
if err := builtin.EnsureFolderExists(cfg.DownloadsPath); err != nil {
|
||||||
|
log.Fatal().Err(err).Msg("create downloads directory failed")
|
||||||
|
}
|
||||||
|
if err := builtin.EnsureFolderExists(cfg.ScreenShotsPath); err != nil {
|
||||||
|
log.Fatal().Err(err).Msg("create screenshots directory failed")
|
||||||
|
}
|
||||||
|
|
||||||
|
globalConfig = cfg
|
||||||
|
})
|
||||||
|
|
||||||
|
return globalConfig
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
v5.0.0+2503041818
|
v5.0.0+2503041939
|
||||||
|
|||||||
@@ -100,7 +100,7 @@ type AndroidDevice struct {
|
|||||||
|
|
||||||
func (dev *AndroidDevice) Setup() error {
|
func (dev *AndroidDevice) Setup() error {
|
||||||
dev.Device.RunShellCommand("ime", "enable", option.UnicodeImePackageName)
|
dev.Device.RunShellCommand("ime", "enable", option.UnicodeImePackageName)
|
||||||
dev.Device.RunShellCommand("rm", "-r", config.DeviceActionLogFilePath)
|
dev.Device.RunShellCommand("rm", "-r", config.GetConfig().DeviceActionLogFilePath)
|
||||||
|
|
||||||
// setup evalite
|
// setup evalite
|
||||||
evalToolRaw, err := evalite.ReadFile("evalite")
|
evalToolRaw, err := evalite.ReadFile("evalite")
|
||||||
|
|||||||
@@ -677,10 +677,10 @@ func (ad *ADBDriver) StopCaptureLog() (result interface{}, err error) {
|
|||||||
// 没有解析到打点日志,走兜底逻辑
|
// 没有解析到打点日志,走兜底逻辑
|
||||||
if len(pointRes) == 0 {
|
if len(pointRes) == 0 {
|
||||||
log.Info().Msg("action log is null, use action file >>>")
|
log.Info().Msg("action log is null, use action file >>>")
|
||||||
logFilePathPrefix := fmt.Sprintf("%v/data", config.ActionLogFilePath)
|
logFilePathPrefix := fmt.Sprintf("%v/data", config.GetConfig().ActionLogFilePath)
|
||||||
files := []string{}
|
files := []string{}
|
||||||
ad.Device.RunShellCommand("pull", config.DeviceActionLogFilePath, config.ActionLogFilePath)
|
ad.Device.RunShellCommand("pull", config.GetConfig().DeviceActionLogFilePath, config.GetConfig().ActionLogFilePath)
|
||||||
err = filepath.Walk(config.ActionLogFilePath, func(path string, info fs.FileInfo, err error) error {
|
err = filepath.Walk(config.GetConfig().ActionLogFilePath, func(path string, info fs.FileInfo, err error) error {
|
||||||
// 只是需要日志文件
|
// 只是需要日志文件
|
||||||
if ok := strings.Contains(path, logFilePathPrefix); ok {
|
if ok := strings.Contains(path, logFilePathPrefix); ok {
|
||||||
files = append(files, path)
|
files = append(files, path)
|
||||||
@@ -800,7 +800,7 @@ func (ad *ADBDriver) GetIme() (ime string, err error) {
|
|||||||
|
|
||||||
func (ad *ADBDriver) ScreenRecord(duration time.Duration) (videoPath string, err error) {
|
func (ad *ADBDriver) ScreenRecord(duration time.Duration) (videoPath string, err error) {
|
||||||
timestamp := time.Now().Format("20060102_150405") + fmt.Sprintf("_%03d", time.Now().UnixNano()/1e6%1000)
|
timestamp := time.Now().Format("20060102_150405") + fmt.Sprintf("_%03d", time.Now().UnixNano()/1e6%1000)
|
||||||
fileName := filepath.Join(config.ScreenShotsPath, fmt.Sprintf("%s.mp4", timestamp))
|
fileName := filepath.Join(config.GetConfig().ScreenShotsPath, fmt.Sprintf("%s.mp4", timestamp))
|
||||||
|
|
||||||
file, err := os.Create(fileName)
|
file, err := os.Create(fileName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ func (dExt *XTDriver) GetScreenResult(opts ...option.ActionOption) (screenResult
|
|||||||
|
|
||||||
// get screenshot info with retry
|
// get screenshot info with retry
|
||||||
for i := 0; i < 3; i++ {
|
for i := 0; i < 3; i++ {
|
||||||
imagePath = filepath.Join(config.ScreenShotsPath, fileName)
|
imagePath = filepath.Join(config.GetConfig().ScreenShotsPath, fileName)
|
||||||
bufSource, err = dExt.ScreenShot()
|
bufSource, err = dExt.ScreenShot()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
lastErr = err
|
lastErr = err
|
||||||
|
|||||||
@@ -276,7 +276,7 @@ var (
|
|||||||
func DownloadFileByUrl(fileUrl string) (filePath string, err error) {
|
func DownloadFileByUrl(fileUrl string) (filePath string, err error) {
|
||||||
hash := md5.Sum([]byte(fileUrl))
|
hash := md5.Sum([]byte(fileUrl))
|
||||||
fileName := fmt.Sprintf("%x", hash)
|
fileName := fmt.Sprintf("%x", hash)
|
||||||
filePath = filepath.Join(config.DownloadsPath, fileName)
|
filePath = filepath.Join(config.GetConfig().DownloadsPath, fileName)
|
||||||
|
|
||||||
// get or create file lock
|
// get or create file lock
|
||||||
lockI, _ := fileLocks.LoadOrStore(filePath, &sync.Mutex{})
|
lockI, _ := fileLocks.LoadOrStore(filePath, &sync.Mutex{})
|
||||||
|
|||||||
@@ -894,7 +894,7 @@ func (wd *WDADriver) triggerWDALog(data map[string]interface{}) (rawResp []byte,
|
|||||||
|
|
||||||
func (wd *WDADriver) ScreenRecord(duration time.Duration) (videoPath string, err error) {
|
func (wd *WDADriver) ScreenRecord(duration time.Duration) (videoPath string, err error) {
|
||||||
timestamp := time.Now().Format("20060102_150405") + fmt.Sprintf("_%03d", time.Now().UnixNano()/1e6%1000)
|
timestamp := time.Now().Format("20060102_150405") + fmt.Sprintf("_%03d", time.Now().UnixNano()/1e6%1000)
|
||||||
fileName := filepath.Join(config.ScreenShotsPath, fmt.Sprintf("%s.mp4", timestamp))
|
fileName := filepath.Join(config.GetConfig().ScreenShotsPath, fmt.Sprintf("%s.mp4", timestamp))
|
||||||
|
|
||||||
file, err := os.Create(fileName)
|
file, err := os.Create(fileName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -188,5 +188,5 @@ func GetProjectRootDirPath(path string) (rootDir string, err error) {
|
|||||||
// failed to locate project root dir
|
// failed to locate project root dir
|
||||||
// maybe project plugin debugtalk.xx and proj.json are not exist
|
// maybe project plugin debugtalk.xx and proj.json are not exist
|
||||||
// use current dir instead
|
// use current dir instead
|
||||||
return config.RootDir, nil
|
return config.GetConfig().RootDir, nil
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-3
@@ -32,7 +32,7 @@ func NewSummary() *Summary {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
Time: &TestCaseTime{
|
Time: &TestCaseTime{
|
||||||
StartAt: config.StartTime,
|
StartAt: config.GetConfig().StartTime,
|
||||||
},
|
},
|
||||||
Platform: platForm,
|
Platform: platForm,
|
||||||
}
|
}
|
||||||
@@ -67,7 +67,7 @@ func (s *Summary) AddCaseSummary(caseSummary *TestCaseSummary) {
|
|||||||
s.rootDir = caseSummary.RootDir
|
s.rootDir = caseSummary.RootDir
|
||||||
} else if s.rootDir != caseSummary.RootDir {
|
} else if s.rootDir != caseSummary.RootDir {
|
||||||
// if multiple testcases have different root path, use current working dir
|
// if multiple testcases have different root path, use current working dir
|
||||||
s.rootDir = config.RootDir
|
s.rootDir = config.GetConfig().RootDir
|
||||||
}
|
}
|
||||||
|
|
||||||
// merge action stats
|
// merge action stats
|
||||||
@@ -80,7 +80,7 @@ func (s *Summary) AddCaseSummary(caseSummary *TestCaseSummary) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Summary) SetupDirPath() (path string, err error) {
|
func (s *Summary) SetupDirPath() (path string, err error) {
|
||||||
dirPath := filepath.Join(s.rootDir, config.ResultsDir)
|
dirPath := filepath.Join(s.rootDir, config.GetConfig().ResultsDir)
|
||||||
err = builtin.EnsureFolderExists(dirPath)
|
err = builtin.EnsureFolderExists(dirPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
|
|||||||
Reference in New Issue
Block a user