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