refactor: get current dir

This commit is contained in:
debugtalk
2022-12-15 23:20:49 +08:00
parent 358b7dd685
commit ee68b954fb
14 changed files with 64 additions and 39 deletions

3
.gitignore vendored
View File

@@ -23,8 +23,11 @@ __debug_bin
site/ site/
output/ output/
logs logs
*.log
*.pcap
.coverage .coverage
reports reports
results
*.xml *.xml
htmlcov/ htmlcov/
screenshots/ screenshots/

View File

@@ -16,6 +16,7 @@ import (
"github.com/httprunner/httprunner/v4/hrp/internal/builtin" "github.com/httprunner/httprunner/v4/hrp/internal/builtin"
"github.com/httprunner/httprunner/v4/hrp/internal/code" "github.com/httprunner/httprunner/v4/hrp/internal/code"
"github.com/httprunner/httprunner/v4/hrp/internal/env"
"github.com/httprunner/httprunner/v4/hrp/internal/myexec" "github.com/httprunner/httprunner/v4/hrp/internal/myexec"
"github.com/httprunner/httprunner/v4/hrp/internal/version" "github.com/httprunner/httprunner/v4/hrp/internal/version"
) )
@@ -105,8 +106,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 == "" {
dir, _ := os.Getwd() output = filepath.Join(env.RootDir, PluginPySourceGenFile)
output = filepath.Join(dir, PluginPySourceGenFile)
} else if builtin.IsFolderPathExists(output) { } else if builtin.IsFolderPathExists(output) {
output = filepath.Join(output, PluginPySourceGenFile) output = filepath.Join(output, PluginPySourceGenFile)
} }
@@ -154,8 +154,7 @@ func (pt *pluginTemplate) generateGo(output string) error {
// specify output file path // specify output file path
if output == "" { if output == "" {
dir, _ := os.Getwd() output = filepath.Join(env.RootDir, PluginHashicorpGoBuiltFile)
output = filepath.Join(dir, PluginHashicorpGoBuiltFile)
} else if builtin.IsFolderPathExists(output) { } else if builtin.IsFolderPathExists(output) {
output = filepath.Join(output, PluginHashicorpGoBuiltFile) output = filepath.Join(output, PluginHashicorpGoBuiltFile)
} }

View File

@@ -9,6 +9,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/httprunner/httprunner/v4/hrp" "github.com/httprunner/httprunner/v4/hrp"
"github.com/httprunner/httprunner/v4/hrp/internal/env"
"github.com/httprunner/httprunner/v4/hrp/pkg/boomer" "github.com/httprunner/httprunner/v4/hrp/pkg/boomer"
"github.com/httprunner/httprunner/v4/hrp/pkg/convert" "github.com/httprunner/httprunner/v4/hrp/pkg/convert"
) )
@@ -76,8 +77,7 @@ func makeCurlTestCase(args []string) *hrp.TestCase {
log.Error().Err(err).Msg("convert curl command failed") log.Error().Err(err).Msg("convert curl command failed")
os.Exit(1) os.Exit(1)
} }
casePath, _ := os.Getwd() testCase, err := tCase.ToTestCase(env.RootDir)
testCase, err := tCase.ToTestCase(casePath)
if err != nil { if err != nil {
log.Error().Err(err).Msg("convert testcase to failed") log.Error().Err(err).Msg("convert testcase to failed")
os.Exit(1) os.Exit(1)

View File

@@ -3,13 +3,13 @@ package dial
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"os"
"path/filepath" "path/filepath"
"time" "time"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/httprunner/httprunner/v4/hrp/internal/builtin" "github.com/httprunner/httprunner/v4/hrp/internal/builtin"
"github.com/httprunner/httprunner/v4/hrp/internal/env"
"github.com/httprunner/httprunner/v4/hrp/internal/myexec" "github.com/httprunner/httprunner/v4/hrp/internal/myexec"
) )
@@ -36,9 +36,8 @@ func DoCurl(args []string) (err error) {
var curlResult CurlResult var curlResult CurlResult
defer func() { defer func() {
if saveTests { if saveTests {
dir, _ := os.Getwd()
curlResultName := fmt.Sprintf("curl_result_%v.json", time.Now().Format("20060102150405")) curlResultName := fmt.Sprintf("curl_result_%v.json", time.Now().Format("20060102150405"))
curlResultPath := filepath.Join(dir, curlResultName) curlResultPath := filepath.Join(env.RootDir, curlResultName)
err = builtin.Dump2JSON(curlResult, curlResultPath) err = builtin.Dump2JSON(curlResult, curlResultPath)
if err != nil { if err != nil {
log.Error().Err(err).Msg("save dns resolution result failed") log.Error().Err(err).Msg("save dns resolution result failed")

View File

@@ -6,7 +6,6 @@ import (
"net" "net"
"net/http" "net/http"
"net/url" "net/url"
"os"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
@@ -17,6 +16,7 @@ import (
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/httprunner/httprunner/v4/hrp/internal/builtin" "github.com/httprunner/httprunner/v4/hrp/internal/builtin"
"github.com/httprunner/httprunner/v4/hrp/internal/env"
"github.com/httprunner/httprunner/v4/hrp/internal/json" "github.com/httprunner/httprunner/v4/hrp/internal/json"
) )
@@ -210,9 +210,8 @@ func DoDns(dnsOptions *DnsOptions, args []string) (err error) {
var dnsResult DnsResult var dnsResult DnsResult
defer func() { defer func() {
if dnsOptions.SaveTests { if dnsOptions.SaveTests {
dir, _ := os.Getwd()
dnsResultName := fmt.Sprintf("dns_result_%v.json", time.Now().Format("20060102150405")) dnsResultName := fmt.Sprintf("dns_result_%v.json", time.Now().Format("20060102150405"))
dnsResultPath := filepath.Join(dir, dnsResultName) dnsResultPath := filepath.Join(env.RootDir, dnsResultName)
err = builtin.Dump2JSON(dnsResult, dnsResultPath) err = builtin.Dump2JSON(dnsResult, dnsResultPath)
if err != nil { if err != nil {
log.Error().Err(err).Msg("save dns resolution result failed") log.Error().Err(err).Msg("save dns resolution result failed")

View File

@@ -3,7 +3,6 @@ package dial
import ( import (
"fmt" "fmt"
"net/url" "net/url"
"os"
"path/filepath" "path/filepath"
"strings" "strings"
"time" "time"
@@ -13,6 +12,7 @@ import (
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/httprunner/httprunner/v4/hrp/internal/builtin" "github.com/httprunner/httprunner/v4/hrp/internal/builtin"
"github.com/httprunner/httprunner/v4/hrp/internal/env"
) )
type PingOptions struct { type PingOptions struct {
@@ -46,9 +46,8 @@ func DoPing(pingOptions *PingOptions, args []string) (err error) {
var pingResult PingResult var pingResult PingResult
defer func() { defer func() {
if pingOptions.SaveTests { if pingOptions.SaveTests {
dir, _ := os.Getwd()
pingResultName := fmt.Sprintf("ping_result_%v.json", time.Now().Format("20060102150405")) pingResultName := fmt.Sprintf("ping_result_%v.json", time.Now().Format("20060102150405"))
pingResultPath := filepath.Join(dir, pingResultName) pingResultPath := filepath.Join(env.RootDir, pingResultName)
err = builtin.Dump2JSON(pingResult, pingResultPath) err = builtin.Dump2JSON(pingResult, pingResultPath)
if err != nil { if err != nil {
log.Error().Err(err).Msg("save ping result failed") log.Error().Err(err).Msg("save ping result failed")

View File

@@ -6,7 +6,6 @@ import (
"bufio" "bufio"
"fmt" "fmt"
"net/url" "net/url"
"os"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strconv" "strconv"
@@ -17,6 +16,7 @@ import (
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/httprunner/httprunner/v4/hrp/internal/builtin" "github.com/httprunner/httprunner/v4/hrp/internal/builtin"
"github.com/httprunner/httprunner/v4/hrp/internal/env"
"github.com/httprunner/httprunner/v4/hrp/internal/myexec" "github.com/httprunner/httprunner/v4/hrp/internal/myexec"
) )
@@ -34,9 +34,8 @@ func DoTraceRoute(traceRouteOptions *TraceRouteOptions, args []string) (err erro
var traceRouteResult TraceRouteResult var traceRouteResult TraceRouteResult
defer func() { defer func() {
if traceRouteOptions.SaveTests { if traceRouteOptions.SaveTests {
dir, _ := os.Getwd()
traceRouteResultName := fmt.Sprintf("traceroute_result_%v.json", time.Now().Format("20060102150405")) traceRouteResultName := fmt.Sprintf("traceroute_result_%v.json", time.Now().Format("20060102150405"))
traceRouteResultPath := filepath.Join(dir, traceRouteResultName) traceRouteResultPath := filepath.Join(env.RootDir, traceRouteResultName)
err = builtin.Dump2JSON(traceRouteResult, traceRouteResultPath) err = builtin.Dump2JSON(traceRouteResult, traceRouteResultPath)
if err != nil { if err != nil {
log.Error().Err(err).Msg("save traceroute result failed") log.Error().Err(err).Msg("save traceroute result failed")

View File

@@ -32,9 +32,8 @@ func DoTraceRoute(traceRouteOptions *TraceRouteOptions, args []string) (err erro
var traceRouteResult TraceRouteResult var traceRouteResult TraceRouteResult
defer func() { defer func() {
if traceRouteOptions.SaveTests { if traceRouteOptions.SaveTests {
dir, _ := os.Getwd()
traceRouteResultName := fmt.Sprintf("traceroute_result_%v.json", time.Now().Format("20060102150405")) traceRouteResultName := fmt.Sprintf("traceroute_result_%v.json", time.Now().Format("20060102150405"))
traceRouteResultPath := filepath.Join(dir, traceRouteResultName) traceRouteResultPath := filepath.Join(env.RootDir, traceRouteResultName)
err = builtin.Dump2JSON(traceRouteResult, traceRouteResultPath) err = builtin.Dump2JSON(traceRouteResult, traceRouteResultPath)
if err != nil { if err != nil {
log.Error().Err(err).Msg("save traceroute result failed") log.Error().Err(err).Msg("save traceroute result failed")

View File

@@ -1,6 +1,9 @@
package env package env
import "os" import (
"os"
"path/filepath"
)
var ( var (
WDA_USB_DRIVER = os.Getenv("WDA_USB_DRIVER") WDA_USB_DRIVER = os.Getenv("WDA_USB_DRIVER")
@@ -14,3 +17,24 @@ var (
PYPI_INDEX_URL = os.Getenv("PYPI_INDEX_URL") PYPI_INDEX_URL = os.Getenv("PYPI_INDEX_URL")
PATH = os.Getenv("PATH") PATH = os.Getenv("PATH")
) )
const (
ResultsDir = "results"
)
var (
RootDir string
ResultsPath string
ScreenShotsPath string
)
func init() {
var err error
RootDir, err = os.Getwd()
if err != nil {
panic(err)
}
ResultsPath = filepath.Join(RootDir, ResultsDir)
ScreenShotsPath = filepath.Join(ResultsPath, "screenshots")
}

View File

@@ -13,6 +13,7 @@ import (
"github.com/httprunner/httprunner/v4/hrp" "github.com/httprunner/httprunner/v4/hrp"
"github.com/httprunner/httprunner/v4/hrp/internal/builtin" "github.com/httprunner/httprunner/v4/hrp/internal/builtin"
"github.com/httprunner/httprunner/v4/hrp/internal/env"
"github.com/httprunner/httprunner/v4/hrp/internal/myexec" "github.com/httprunner/httprunner/v4/hrp/internal/myexec"
"github.com/httprunner/httprunner/v4/hrp/internal/sdk" "github.com/httprunner/httprunner/v4/hrp/internal/sdk"
"github.com/httprunner/httprunner/v4/hrp/internal/version" "github.com/httprunner/httprunner/v4/hrp/internal/version"
@@ -91,10 +92,10 @@ func CreateScaffold(projectName string, pluginType PluginType, venv string, forc
if err := builtin.CreateFolder(filepath.Join(projectName, "testcases")); err != nil { if err := builtin.CreateFolder(filepath.Join(projectName, "testcases")); err != nil {
return err return err
} }
if err := builtin.CreateFolder(filepath.Join(projectName, "reports")); err != nil { if err := builtin.CreateFolder(filepath.Join(projectName, env.ResultsDir)); err != nil {
return err return err
} }
if err := builtin.CreateFile(filepath.Join(projectName, "reports", ".keep"), ""); err != nil { if err := builtin.CreateFile(filepath.Join(projectName, env.ResultsDir, ".keep"), ""); err != nil {
return err return err
} }

View File

@@ -3,7 +3,6 @@ package convert
import ( import (
_ "embed" _ "embed"
"fmt" "fmt"
"os"
"path/filepath" "path/filepath"
"strings" "strings"
"time" "time"
@@ -13,6 +12,7 @@ import (
"github.com/httprunner/httprunner/v4/hrp" "github.com/httprunner/httprunner/v4/hrp"
"github.com/httprunner/httprunner/v4/hrp/internal/builtin" "github.com/httprunner/httprunner/v4/hrp/internal/builtin"
"github.com/httprunner/httprunner/v4/hrp/internal/env"
"github.com/httprunner/httprunner/v4/hrp/internal/myexec" "github.com/httprunner/httprunner/v4/hrp/internal/myexec"
"github.com/httprunner/httprunner/v4/hrp/internal/sdk" "github.com/httprunner/httprunner/v4/hrp/internal/sdk"
) )
@@ -192,8 +192,7 @@ func (c *TCaseConverter) genOutputPath(suffix string) string {
if c.OutputDir != "" { if c.OutputDir != "" {
return filepath.Join(c.OutputDir, outFileFullName) return filepath.Join(c.OutputDir, outFileFullName)
} else { } else {
curWorkDir, _ := os.Getwd() return filepath.Join(env.RootDir, outFileFullName)
return filepath.Join(curWorkDir, outFileFullName)
} }
} }
outFileFullName = builtin.GetFileNameWithoutExtension(c.InputSample) + "_test" + suffix outFileFullName = builtin.GetFileNameWithoutExtension(c.InputSample) + "_test" + suffix

View File

@@ -13,6 +13,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/httprunner/httprunner/v4/hrp/internal/builtin"
"github.com/httprunner/httprunner/v4/hrp/internal/env" "github.com/httprunner/httprunner/v4/hrp/internal/env"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
@@ -234,6 +235,13 @@ func NewDriverExt(device Device, driver WebDriver) (dExt *DriverExt, err error)
return nil, err return nil, err
} }
// create results directory
if err = builtin.EnsureFolderExists(env.ResultsPath); err != nil {
return nil, errors.Wrap(err, "create results directory failed")
}
if err = builtin.EnsureFolderExists(env.ScreenShotsPath); err != nil {
return nil, errors.Wrap(err, "create screenshots directory failed")
}
return dExt, nil return dExt, nil
} }
@@ -291,11 +299,7 @@ func (dExt *DriverExt) ScreenShot(fileName string) (string, error) {
return "", errors.Wrap(err, "screenshot failed") return "", errors.Wrap(err, "screenshot failed")
} }
screenshotsDir := env.ScreenShotsPath fileName = filepath.Join(env.ScreenShotsPath, fileName)
if err = os.MkdirAll(screenshotsDir, os.ModePerm); err != nil {
return "", errors.Wrap(err, "create screenshots directory failed")
}
fileName = filepath.Join(screenshotsDir, fileName)
path, err := saveScreenShot(raw, fileName) path, err := saveScreenShot(raw, fileName)
if err != nil { if err != nil {
return "", errors.Wrap(err, "save screenshot failed") return "", errors.Wrap(err, "save screenshot failed")

View File

@@ -13,6 +13,7 @@ import (
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/httprunner/httprunner/v4/hrp/internal/code" "github.com/httprunner/httprunner/v4/hrp/internal/code"
"github.com/httprunner/httprunner/v4/hrp/internal/env"
"github.com/httprunner/httprunner/v4/hrp/internal/myexec" "github.com/httprunner/httprunner/v4/hrp/internal/myexec"
"github.com/httprunner/httprunner/v4/hrp/internal/sdk" "github.com/httprunner/httprunner/v4/hrp/internal/sdk"
) )
@@ -163,5 +164,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 os.Getwd() return env.RootDir, nil
} }

View File

@@ -13,6 +13,7 @@ import (
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/httprunner/httprunner/v4/hrp/internal/builtin" "github.com/httprunner/httprunner/v4/hrp/internal/builtin"
"github.com/httprunner/httprunner/v4/hrp/internal/env"
"github.com/httprunner/httprunner/v4/hrp/internal/version" "github.com/httprunner/httprunner/v4/hrp/internal/version"
) )
@@ -61,12 +62,12 @@ func (s *Summary) appendCaseSummary(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, _ = os.Getwd() s.rootDir = env.RootDir
} }
} }
func (s *Summary) genHTMLReport() error { func (s *Summary) genHTMLReport() error {
reportsDir := filepath.Join(s.rootDir, resultsDir) reportsDir := filepath.Join(s.rootDir, env.ResultsDir)
err := builtin.EnsureFolderExists(reportsDir) err := builtin.EnsureFolderExists(reportsDir)
if err != nil { if err != nil {
return err return err
@@ -96,7 +97,7 @@ func (s *Summary) genHTMLReport() error {
} }
func (s *Summary) genSummary() error { func (s *Summary) genSummary() error {
reportsDir := filepath.Join(s.rootDir, resultsDir) reportsDir := filepath.Join(s.rootDir, env.ResultsDir)
err := builtin.EnsureFolderExists(reportsDir) err := builtin.EnsureFolderExists(reportsDir)
if err != nil { if err != nil {
return err return err
@@ -113,11 +114,9 @@ func (s *Summary) genSummary() error {
//go:embed internal/scaffold/templates/report/template.html //go:embed internal/scaffold/templates/report/template.html
var reportTemplate string var reportTemplate string
const resultsDir = "reports"
type Stat struct { type Stat struct {
TestCases TestCaseStat `json:"testcases" yaml:"test_cases"` TestCases TestCaseStat `json:"testcases" yaml:"testcases"`
TestSteps TestStepStat `json:"teststeps" yaml:"test_steps"` TestSteps TestStepStat `json:"teststeps" yaml:"teststeps"`
} }
type TestCaseStat struct { type TestCaseStat struct {