refactor: results dir

This commit is contained in:
debugtalk
2022-12-16 00:17:57 +08:00
parent 088974ddee
commit 0e0a4152cb
12 changed files with 24 additions and 26 deletions

View File

@@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"path/filepath"
"time"
"github.com/rs/zerolog/log"
@@ -36,7 +35,7 @@ func DoCurl(args []string) (err error) {
var curlResult CurlResult
defer func() {
if saveTests {
curlResultName := fmt.Sprintf("curl_result_%v.json", time.Now().Format("20060102150405"))
curlResultName := fmt.Sprintf("curl_result_%v.json", env.StartTimeStr)
curlResultPath := filepath.Join(env.RootDir, curlResultName)
err = builtin.Dump2JSON(curlResult, curlResultPath)
if err != nil {

View File

@@ -210,7 +210,7 @@ func DoDns(dnsOptions *DnsOptions, args []string) (err error) {
var dnsResult DnsResult
defer func() {
if dnsOptions.SaveTests {
dnsResultName := fmt.Sprintf("dns_result_%v.json", time.Now().Format("20060102150405"))
dnsResultName := fmt.Sprintf("dns_result_%v.json", env.StartTimeStr)
dnsResultPath := filepath.Join(env.RootDir, dnsResultName)
err = builtin.Dump2JSON(dnsResult, dnsResultPath)
if err != nil {

View File

@@ -46,7 +46,7 @@ func DoPing(pingOptions *PingOptions, args []string) (err error) {
var pingResult PingResult
defer func() {
if pingOptions.SaveTests {
pingResultName := fmt.Sprintf("ping_result_%v.json", time.Now().Format("20060102150405"))
pingResultName := fmt.Sprintf("ping_result_%v.json", env.StartTimeStr)
pingResultPath := filepath.Join(env.RootDir, pingResultName)
err = builtin.Dump2JSON(pingResult, pingResultPath)
if err != nil {

View File

@@ -34,7 +34,7 @@ func DoTraceRoute(traceRouteOptions *TraceRouteOptions, args []string) (err erro
var traceRouteResult TraceRouteResult
defer func() {
if traceRouteOptions.SaveTests {
traceRouteResultName := fmt.Sprintf("traceroute_result_%v.json", time.Now().Format("20060102150405"))
traceRouteResultName := fmt.Sprintf("traceroute_result_%v.json", env.StartTimeStr)
traceRouteResultPath := filepath.Join(env.RootDir, traceRouteResultName)
err = builtin.Dump2JSON(traceRouteResult, traceRouteResultPath)
if err != nil {

View File

@@ -32,7 +32,7 @@ func DoTraceRoute(traceRouteOptions *TraceRouteOptions, args []string) (err erro
var traceRouteResult TraceRouteResult
defer func() {
if traceRouteOptions.SaveTests {
traceRouteResultName := fmt.Sprintf("traceroute_result_%v.json", time.Now().Format("20060102150405"))
traceRouteResultName := fmt.Sprintf("traceroute_result_%v.json", env.StartTimeStr)
traceRouteResultPath := filepath.Join(env.RootDir, traceRouteResultName)
err = builtin.Dump2JSON(traceRouteResult, traceRouteResultPath)
if err != nil {

View File

@@ -3,6 +3,7 @@ package env
import (
"os"
"path/filepath"
"time"
)
var (
@@ -19,13 +20,17 @@ var (
)
const (
ResultsDir = "results"
ResultsDirName = "results"
ScreenshotsDirName = "screenshots"
)
var (
RootDir string
ResultsDir string
ResultsPath string
ScreenShotsPath string
StartTime = time.Now()
StartTimeStr = StartTime.Format("20060102150405")
)
func init() {
@@ -35,6 +40,7 @@ func init() {
panic(err)
}
ResultsDir = filepath.Join(ResultsDirName, StartTimeStr)
ResultsPath = filepath.Join(RootDir, ResultsDir)
ScreenShotsPath = filepath.Join(ResultsPath, "screenshots")
ScreenShotsPath = filepath.Join(ResultsPath, ScreenshotsDirName)
}

View File

@@ -92,10 +92,10 @@ func CreateScaffold(projectName string, pluginType PluginType, venv string, forc
if err := builtin.CreateFolder(filepath.Join(projectName, "testcases")); err != nil {
return err
}
if err := builtin.CreateFolder(filepath.Join(projectName, env.ResultsDir)); err != nil {
if err := builtin.CreateFolder(filepath.Join(projectName, env.ResultsDirName)); err != nil {
return err
}
if err := builtin.CreateFile(filepath.Join(projectName, env.ResultsDir, ".keep"), ""); err != nil {
if err := builtin.CreateFile(filepath.Join(projectName, env.ResultsDirName, ".keep"), ""); err != nil {
return err
}

View File

@@ -5,7 +5,6 @@ import (
"fmt"
"path/filepath"
"strings"
"time"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
@@ -188,7 +187,7 @@ type TCaseConverter struct {
func (c *TCaseConverter) genOutputPath(suffix string) string {
var outFileFullName string
if curlCmd := strings.TrimSpace(c.InputSample); strings.HasPrefix(curlCmd, "curl ") {
outFileFullName = fmt.Sprintf("curl_%v_test%v", time.Now().Format("20060102150405"), suffix)
outFileFullName = fmt.Sprintf("curl_%v_test%v", env.StartTimeStr, suffix)
if c.OutputDir != "" {
return filepath.Join(c.OutputDir, outFileFullName)
} else {

View File

@@ -208,7 +208,6 @@ type DriverExt struct {
doneMjpegStream chan bool
scale float64
ocrService OCRService // used to get text from image
StartTime time.Time // used to associate screenshots name
ScreenShots []string // save screenshots path
CVArgs
@@ -657,8 +656,8 @@ func (dExt *DriverExt) DoAction(action MobileAction) error {
case CtlScreenShot:
// take snapshot
log.Info().Msg("take snapshot for current screen")
screenshotPath, err := dExt.ScreenShot(fmt.Sprintf("%d_screenshot_%d",
dExt.StartTime.Unix(), time.Now().Unix()))
screenshotPath, err := dExt.ScreenShot(fmt.Sprintf("screenshot_%d",
time.Now().Unix()))
if err != nil {
return errors.Wrap(err, "take screenshot failed")
}

View File

@@ -349,9 +349,7 @@ func (dev *IOSDevice) StartPerf() error {
return err
}
dev.perfFile = filepath.Join(env.ResultsPath,
fmt.Sprintf("perf_%s.log", time.Now().Format("20060102150405")))
dev.perfFile = filepath.Join(env.ResultsPath, "perf.data")
log.Info().Str("perfFile", dev.perfFile).Msg("create perf file")
file, err := os.OpenFile(dev.perfFile,
os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0o755)
@@ -395,9 +393,7 @@ func (dev *IOSDevice) StartPcap() error {
return err
}
dev.pcapFile = filepath.Join(env.ResultsPath,
fmt.Sprintf("dump_%s.pcap", time.Now().Format("20060102150405")))
dev.pcapFile = filepath.Join(env.ResultsPath, "dump.pcap")
log.Info().Str("pcapFile", dev.pcapFile).Msg("create pcap file")
file, err := os.OpenFile(dev.pcapFile,
os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0o755)

View File

@@ -552,7 +552,6 @@ func runStepMobileUI(s *SessionRunner, step *TStep) (stepResult *StepResult, err
if err != nil {
return
}
uiDriver.StartTime = s.startTime
defer func() {
attachments := make(map[string]interface{})
@@ -598,7 +597,7 @@ func runStepMobileUI(s *SessionRunner, step *TStep) (stepResult *StepResult, err
// take snapshot
screenshotPath, err := uiDriver.ScreenShot(
fmt.Sprintf("%d_validate_%d", uiDriver.StartTime.Unix(), time.Now().Unix()))
fmt.Sprintf("validate_%d", time.Now().Unix()))
if err != nil {
log.Warn().Err(err).Str("step", step.Name).Msg("take screenshot failed")
} else {

View File

@@ -27,7 +27,7 @@ func newOutSummary() *Summary {
Success: true,
Stat: &Stat{},
Time: &TestCaseTime{
StartAt: time.Now(),
StartAt: env.StartTime,
},
Platform: platForm,
}
@@ -73,7 +73,7 @@ func (s *Summary) genHTMLReport() error {
return err
}
reportPath := filepath.Join(reportsDir, fmt.Sprintf("report-%v.html", s.Time.StartAt.Unix()))
reportPath := filepath.Join(reportsDir, "report.html")
file, err := os.OpenFile(reportPath, os.O_WRONLY|os.O_CREATE, 0o666)
if err != nil {
log.Error().Err(err).Msg("open file failed")
@@ -103,7 +103,7 @@ func (s *Summary) genSummary() error {
return err
}
summaryPath := filepath.Join(reportsDir, fmt.Sprintf("summary-%v.json", s.Time.StartAt.Unix()))
summaryPath := filepath.Join(reportsDir, "summary.json")
err = builtin.Dump2JSON(s, summaryPath)
if err != nil {
return err