mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-05 15:37:35 +08:00
feat: print output with colors
This commit is contained in:
+1
-15
@@ -1,9 +1,6 @@
|
|||||||
package hrp
|
package hrp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os/exec"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
|
||||||
@@ -25,18 +22,7 @@ func convert2PyTestScripts(paths ...string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
args := append([]string{"-m", "httprunner", "make"}, paths...)
|
args := append([]string{"-m", "httprunner", "make"}, paths...)
|
||||||
cmd := exec.Command(python3, args...)
|
return builtin.ExecCommand(python3, args...)
|
||||||
log.Info().Str("cmd", cmd.String()).Msg("convert to pytest scripts")
|
|
||||||
|
|
||||||
output, err := cmd.CombinedOutput()
|
|
||||||
if err != nil {
|
|
||||||
return errors.Wrap(err, "pytest running failed")
|
|
||||||
}
|
|
||||||
out := strings.TrimSpace(string(output))
|
|
||||||
println(out)
|
|
||||||
|
|
||||||
log.Info().Msg("convert to pytest scripts successfully")
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func convert2GoTestScripts(paths ...string) error {
|
func convert2GoTestScripts(paths ...string) error {
|
||||||
|
|||||||
@@ -92,16 +92,37 @@ func EnsurePython3Venv(packages ...string) (string, error) {
|
|||||||
return python3, nil
|
return python3, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExecCommand(cmd *exec.Cmd, cwd string) error {
|
func ExecCommandInDir(cmd *exec.Cmd, dir string) error {
|
||||||
log.Info().Str("cmd", cmd.String()).Str("cwd", cwd).Msg("exec command")
|
log.Info().Str("cmd", cmd.String()).Str("dir", dir).Msg("exec command")
|
||||||
cmd.Dir = cwd
|
cmd.Dir = dir
|
||||||
output, err := cmd.CombinedOutput()
|
|
||||||
out := strings.TrimSpace(string(output))
|
// print output with colors
|
||||||
|
cmd.Stdout = os.Stdout
|
||||||
|
cmd.Stderr = os.Stderr
|
||||||
|
|
||||||
|
err := cmd.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Str("output", out).Msg("exec command failed")
|
log.Error().Err(err).Msg("exec command failed")
|
||||||
} else if len(out) != 0 {
|
return err
|
||||||
log.Info().Str("output", out).Msg("exec command success")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExecCommand(cmdName string, args ...string) error {
|
||||||
|
cmd := exec.Command(cmdName, args...)
|
||||||
|
log.Info().Str("cmd", cmd.String()).Msg("exec command")
|
||||||
|
|
||||||
|
// print output with colors
|
||||||
|
cmd.Stdout = os.Stdout
|
||||||
|
cmd.Stderr = os.Stderr
|
||||||
|
|
||||||
|
err := cmd.Run()
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err).Msg("exec command failed")
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,7 @@
|
|||||||
package pytest
|
package pytest
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os/exec"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/rs/zerolog/log"
|
|
||||||
|
|
||||||
"github.com/httprunner/httprunner/hrp/internal/builtin"
|
"github.com/httprunner/httprunner/hrp/internal/builtin"
|
||||||
)
|
)
|
||||||
@@ -17,15 +13,5 @@ func RunPytest(args []string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
args = append([]string{"-m", "httprunner", "run"}, args...)
|
args = append([]string{"-m", "httprunner", "run"}, args...)
|
||||||
cmd := exec.Command(python3, args...)
|
return builtin.ExecCommand(python3, args...)
|
||||||
log.Info().Str("cmd", cmd.String()).Msg("run pytest")
|
|
||||||
|
|
||||||
output, err := cmd.CombinedOutput()
|
|
||||||
if err != nil {
|
|
||||||
return errors.Wrap(err, "pytest running failed")
|
|
||||||
}
|
|
||||||
out := strings.TrimSpace(string(output))
|
|
||||||
println(out)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -133,7 +133,7 @@ func CreateScaffold(projectName string, pluginType PluginType) error {
|
|||||||
func createGoPlugin(projectName string) error {
|
func createGoPlugin(projectName string) error {
|
||||||
log.Info().Msg("start to create hashicorp go plugin")
|
log.Info().Msg("start to create hashicorp go plugin")
|
||||||
// check go sdk
|
// check go sdk
|
||||||
if err := builtin.ExecCommand(exec.Command("go", "version"), projectName); err != nil {
|
if err := builtin.ExecCommandInDir(exec.Command("go", "version"), projectName); err != nil {
|
||||||
return errors.Wrap(err, "go sdk not installed")
|
return errors.Wrap(err, "go sdk not installed")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -149,19 +149,19 @@ func createGoPlugin(projectName string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// create go mod
|
// create go mod
|
||||||
if err := builtin.ExecCommand(exec.Command("go", "mod", "init", "plugin"), pluginDir); err != nil {
|
if err := builtin.ExecCommandInDir(exec.Command("go", "mod", "init", "plugin"), pluginDir); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// download plugin dependency
|
// download plugin dependency
|
||||||
// funplugin version should be locked
|
// funplugin version should be locked
|
||||||
funplugin := fmt.Sprintf("github.com/httprunner/funplugin@%s", shared.Version)
|
funplugin := fmt.Sprintf("github.com/httprunner/funplugin@%s", shared.Version)
|
||||||
if err := builtin.ExecCommand(exec.Command("go", "get", funplugin), pluginDir); err != nil {
|
if err := builtin.ExecCommandInDir(exec.Command("go", "get", funplugin), pluginDir); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// build plugin debugtalk.bin
|
// build plugin debugtalk.bin
|
||||||
if err := builtin.ExecCommand(exec.Command("go", "build", "-o", filepath.Join("..", "debugtalk.bin"), "debugtalk.go"), pluginDir); err != nil {
|
if err := builtin.ExecCommandInDir(exec.Command("go", "build", "-o", filepath.Join("..", "debugtalk.bin"), "debugtalk.go"), pluginDir); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+5
-4
@@ -2,20 +2,21 @@ package hrp
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/httprunner/httprunner/hrp/internal/scaffold"
|
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
|
"github.com/httprunner/httprunner/hrp/internal/builtin"
|
||||||
|
"github.com/httprunner/httprunner/hrp/internal/scaffold"
|
||||||
)
|
)
|
||||||
|
|
||||||
func buildHashicorpGoPlugin() {
|
func buildHashicorpGoPlugin() {
|
||||||
log.Info().Msg("[init] build hashicorp go plugin")
|
log.Info().Msg("[init] build hashicorp go plugin")
|
||||||
cmd := exec.Command("go", "build",
|
err := builtin.ExecCommand("go", "build",
|
||||||
"-o", templatesDir+"debugtalk.bin", templatesDir+"plugin/debugtalk.go")
|
"-o", templatesDir+"debugtalk.bin", templatesDir+"plugin/debugtalk.go")
|
||||||
if err := cmd.Run(); err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msg("build hashicorp go plugin failed")
|
log.Error().Err(err).Msg("build hashicorp go plugin failed")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user