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

View File

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

View File

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

View File

@@ -3,13 +3,13 @@ package dial
import (
"bytes"
"fmt"
"os"
"path/filepath"
"time"
"github.com/rs/zerolog/log"
"github.com/httprunner/httprunner/v4/hrp/internal/builtin"
"github.com/httprunner/httprunner/v4/hrp/internal/env"
"github.com/httprunner/httprunner/v4/hrp/internal/myexec"
)
@@ -36,9 +36,8 @@ func DoCurl(args []string) (err error) {
var curlResult CurlResult
defer func() {
if saveTests {
dir, _ := os.Getwd()
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)
if err != nil {
log.Error().Err(err).Msg("save dns resolution result failed")

View File

@@ -6,7 +6,6 @@ import (
"net"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
@@ -17,6 +16,7 @@ import (
"github.com/rs/zerolog/log"
"github.com/httprunner/httprunner/v4/hrp/internal/builtin"
"github.com/httprunner/httprunner/v4/hrp/internal/env"
"github.com/httprunner/httprunner/v4/hrp/internal/json"
)
@@ -210,9 +210,8 @@ func DoDns(dnsOptions *DnsOptions, args []string) (err error) {
var dnsResult DnsResult
defer func() {
if dnsOptions.SaveTests {
dir, _ := os.Getwd()
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)
if err != nil {
log.Error().Err(err).Msg("save dns resolution result failed")

View File

@@ -3,7 +3,6 @@ package dial
import (
"fmt"
"net/url"
"os"
"path/filepath"
"strings"
"time"
@@ -13,6 +12,7 @@ import (
"github.com/rs/zerolog/log"
"github.com/httprunner/httprunner/v4/hrp/internal/builtin"
"github.com/httprunner/httprunner/v4/hrp/internal/env"
)
type PingOptions struct {
@@ -46,9 +46,8 @@ func DoPing(pingOptions *PingOptions, args []string) (err error) {
var pingResult PingResult
defer func() {
if pingOptions.SaveTests {
dir, _ := os.Getwd()
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)
if err != nil {
log.Error().Err(err).Msg("save ping result failed")

View File

@@ -6,7 +6,6 @@ import (
"bufio"
"fmt"
"net/url"
"os"
"path/filepath"
"regexp"
"strconv"
@@ -17,6 +16,7 @@ import (
"github.com/rs/zerolog/log"
"github.com/httprunner/httprunner/v4/hrp/internal/builtin"
"github.com/httprunner/httprunner/v4/hrp/internal/env"
"github.com/httprunner/httprunner/v4/hrp/internal/myexec"
)
@@ -34,9 +34,8 @@ func DoTraceRoute(traceRouteOptions *TraceRouteOptions, args []string) (err erro
var traceRouteResult TraceRouteResult
defer func() {
if traceRouteOptions.SaveTests {
dir, _ := os.Getwd()
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)
if err != nil {
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
defer func() {
if traceRouteOptions.SaveTests {
dir, _ := os.Getwd()
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)
if err != nil {
log.Error().Err(err).Msg("save traceroute result failed")

View File

@@ -1,6 +1,9 @@
package env
import "os"
import (
"os"
"path/filepath"
)
var (
WDA_USB_DRIVER = os.Getenv("WDA_USB_DRIVER")
@@ -14,3 +17,24 @@ var (
PYPI_INDEX_URL = os.Getenv("PYPI_INDEX_URL")
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/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/sdk"
"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 {
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
}
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
}

View File

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

View File

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

View File

@@ -13,6 +13,7 @@ import (
"github.com/rs/zerolog/log"
"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/sdk"
)
@@ -163,5 +164,5 @@ func GetProjectRootDirPath(path string) (rootDir string, err error) {
// failed to locate project root dir
// maybe project plugin debugtalk.xx and proj.json are not exist
// 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/httprunner/httprunner/v4/hrp/internal/builtin"
"github.com/httprunner/httprunner/v4/hrp/internal/env"
"github.com/httprunner/httprunner/v4/hrp/internal/version"
)
@@ -61,12 +62,12 @@ func (s *Summary) appendCaseSummary(caseSummary *TestCaseSummary) {
s.rootDir = caseSummary.RootDir
} else if s.rootDir != caseSummary.RootDir {
// if multiple testcases have different root path, use current working dir
s.rootDir, _ = os.Getwd()
s.rootDir = env.RootDir
}
}
func (s *Summary) genHTMLReport() error {
reportsDir := filepath.Join(s.rootDir, resultsDir)
reportsDir := filepath.Join(s.rootDir, env.ResultsDir)
err := builtin.EnsureFolderExists(reportsDir)
if err != nil {
return err
@@ -96,7 +97,7 @@ func (s *Summary) genHTMLReport() error {
}
func (s *Summary) genSummary() error {
reportsDir := filepath.Join(s.rootDir, resultsDir)
reportsDir := filepath.Join(s.rootDir, env.ResultsDir)
err := builtin.EnsureFolderExists(reportsDir)
if err != nil {
return err
@@ -113,11 +114,9 @@ func (s *Summary) genSummary() error {
//go:embed internal/scaffold/templates/report/template.html
var reportTemplate string
const resultsDir = "reports"
type Stat struct {
TestCases TestCaseStat `json:"testcases" yaml:"test_cases"`
TestSteps TestStepStat `json:"teststeps" yaml:"test_steps"`
TestCases TestCaseStat `json:"testcases" yaml:"testcases"`
TestSteps TestStepStat `json:"teststeps" yaml:"teststeps"`
}
type TestCaseStat struct {