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

@@ -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
}