mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-09 06:23:52 +08:00
feat: assert python3 package is ready with specified version
This commit is contained in:
@@ -6,12 +6,13 @@
|
|||||||
|
|
||||||
- feat #1342: support specify custom python3 venv
|
- feat #1342: support specify custom python3 venv
|
||||||
- feat: support python3 venv priority, specified > projectDir/.venv > $HOME/.hrp/venv
|
- feat: support python3 venv priority, specified > projectDir/.venv > $HOME/.hrp/venv
|
||||||
- refactor: build plugin mechanism
|
- feat: assert python3 package is ready with specified version
|
||||||
|
- refactor: build plugin mechanism, cancel automatic installation of dependencies
|
||||||
- fix: pip upgrade httprunner when installing hrp
|
- fix: pip upgrade httprunner when installing hrp
|
||||||
|
|
||||||
**python version**
|
**python version**
|
||||||
|
|
||||||
fix: unexpected changes in step variables
|
- fix: unexpected changes in step variables
|
||||||
|
|
||||||
## v4.1.2 (2022-06-09)
|
## v4.1.2 (2022-06-09)
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"project_name": "demo-with-go-plugin",
|
"project_name": "demo-with-go-plugin",
|
||||||
"create_time": "2022-06-13T23:28:24.920066+08:00",
|
"create_time": "2022-06-13T23:42:06.381463+08:00",
|
||||||
"hrp_version": "v4.1.2"
|
"hrp_version": "v4.1.3"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
# NOTE: Generated By hrp v4.1.2, DO NOT EDIT!
|
# NOTE: Generated By hrp v4.1.3, DO NOT EDIT!
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"project_name": "demo-with-py-plugin",
|
"project_name": "demo-with-py-plugin",
|
||||||
"create_time": "2022-06-13T23:28:25.254595+08:00",
|
"create_time": "2022-06-13T23:42:06.751991+08:00",
|
||||||
"hrp_version": "v4.1.2"
|
"hrp_version": "v4.1.3"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -104,7 +104,17 @@ func PrepareVenv(venv string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// not specify python3 venv, create default venv
|
// not specify python3 venv, create default venv
|
||||||
python3, err := createDefaultPython3Venv()
|
home, err := os.UserHomeDir()
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "get user home dir failed")
|
||||||
|
}
|
||||||
|
venv = filepath.Join(home, ".hrp", "venv")
|
||||||
|
log.Info().Str("venv", venv).Msg("create default python3 venv")
|
||||||
|
packages := []string{
|
||||||
|
fmt.Sprintf("funppy==%s", fungo.Version),
|
||||||
|
fmt.Sprintf("httprunner==%s", version.VERSION),
|
||||||
|
}
|
||||||
|
python3, err := EnsurePython3Venv(venv, packages...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "create default python3 venv failed")
|
return errors.Wrap(err, "create default python3 venv failed")
|
||||||
}
|
}
|
||||||
@@ -113,56 +123,50 @@ func PrepareVenv(venv string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// create default python3 venv located in $HOME/.hrp/venv
|
|
||||||
func createDefaultPython3Venv() (string, error) {
|
|
||||||
log.Info().Msg("create default python3 venv at $HOME/.hrp/venv")
|
|
||||||
home, err := os.UserHomeDir()
|
|
||||||
if err != nil {
|
|
||||||
return "", errors.Wrap(err, "get user home dir failed")
|
|
||||||
}
|
|
||||||
venv := filepath.Join(home, ".hrp", "venv")
|
|
||||||
packages := []string{
|
|
||||||
fmt.Sprintf("funppy==%s", fungo.Version),
|
|
||||||
fmt.Sprintf("httprunner==%s", version.VERSION),
|
|
||||||
}
|
|
||||||
return EnsurePython3Venv(venv, packages...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func ExecPython3Command(cmdName string, args ...string) error {
|
func ExecPython3Command(cmdName string, args ...string) error {
|
||||||
args = append([]string{"-m", cmdName}, args...)
|
args = append([]string{"-m", cmdName}, args...)
|
||||||
return ExecCommand(Python3Executable, args...)
|
return ExecCommand(Python3Executable, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func AssertPythonPackage(python3 string, pkgName, pkgVersion string) error {
|
||||||
|
out, err := exec.Command(
|
||||||
|
python3, "-c", fmt.Sprintf("import %s; print(%s.__version__)", pkgName, pkgName),
|
||||||
|
).Output()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("python package %s not found", pkgName)
|
||||||
|
}
|
||||||
|
|
||||||
|
// do not check version if pkgVersion is empty
|
||||||
|
if pkgVersion == "" {
|
||||||
|
log.Info().Str("name", pkgName).Msg("python package is ready")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// check package version equality
|
||||||
|
version := strings.TrimSpace(string(out))
|
||||||
|
if fmt.Sprintf("v%s", version) != pkgVersion {
|
||||||
|
return fmt.Errorf("python package %s version %s not matched, please upgrade to %s",
|
||||||
|
pkgName, version, pkgVersion)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Info().Str("name", pkgName).Str("version", pkgVersion).Msg("python package is ready")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func InstallPythonPackage(python3 string, pkg string) (err error) {
|
func InstallPythonPackage(python3 string, pkg string) (err error) {
|
||||||
var pkgName string
|
var pkgName, pkgVersion string
|
||||||
if strings.Contains(pkg, "==") {
|
if strings.Contains(pkg, "==") {
|
||||||
// funppy==0.4.2
|
// funppy==0.5.0
|
||||||
pkgInfo := strings.Split(pkg, "==")
|
pkgInfo := strings.Split(pkg, "==")
|
||||||
pkgName = pkgInfo[0]
|
pkgName = pkgInfo[0]
|
||||||
} else if strings.Contains(pkg, ">=") {
|
pkgVersion = pkgInfo[1]
|
||||||
// httprunner>=4.0.0-beta
|
|
||||||
pkgInfo := strings.Split(pkg, ">=")
|
|
||||||
pkgName = pkgInfo[0]
|
|
||||||
} else {
|
} else {
|
||||||
|
// funppy
|
||||||
pkgName = pkg
|
pkgName = pkg
|
||||||
}
|
}
|
||||||
|
|
||||||
defer func() {
|
// check if package installed and version matched
|
||||||
if err == nil {
|
err = AssertPythonPackage(python3, pkgName, pkgVersion)
|
||||||
// check package version
|
|
||||||
if out, err := exec.Command(
|
|
||||||
python3, "-c", fmt.Sprintf("import %s; print(%s.__version__)", pkgName, pkgName),
|
|
||||||
).Output(); err == nil {
|
|
||||||
log.Info().
|
|
||||||
Str("name", pkgName).
|
|
||||||
Str("version", strings.TrimSpace(string(out))).
|
|
||||||
Msg("python package is ready")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
// check if package installed
|
|
||||||
err = exec.Command(python3, "-c", fmt.Sprintf("import %s", pkgName)).Run()
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -183,7 +187,7 @@ func InstallPythonPackage(python3 string, pkg string) (err error) {
|
|||||||
return errors.Wrap(err, "pip install package failed")
|
return errors.Wrap(err, "pip install package failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return AssertPythonPackage(python3, pkgName, pkgVersion)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExecCommandInDir(cmd *exec.Cmd, dir string) error {
|
func ExecCommandInDir(cmd *exec.Cmd, dir string) error {
|
||||||
|
|||||||
@@ -8,10 +8,10 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/httprunner/funplugin/fungo"
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
|
||||||
|
"github.com/httprunner/funplugin/fungo"
|
||||||
"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/sdk"
|
"github.com/httprunner/httprunner/v4/hrp/internal/sdk"
|
||||||
@@ -208,6 +208,7 @@ func createPythonPlugin(projectName, venv string) error {
|
|||||||
if venv == "" {
|
if venv == "" {
|
||||||
venv = filepath.Join(projectName, ".venv")
|
venv = filepath.Join(projectName, ".venv")
|
||||||
}
|
}
|
||||||
|
log.Info().Str("venv", venv).Msg("create python3 venv")
|
||||||
packages := []string{
|
packages := []string{
|
||||||
fmt.Sprintf("funppy==%s", fungo.Version),
|
fmt.Sprintf("funppy==%s", fungo.Version),
|
||||||
fmt.Sprintf("httprunner==%s", version.VERSION),
|
fmt.Sprintf("httprunner==%s", version.VERSION),
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
# NOTE: Generated By hrp v4.1.2, DO NOT EDIT!
|
# NOTE: Generated By hrp v4.1.3, DO NOT EDIT!
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
// NOTE: Generated By hrp v4.1.2, DO NOT EDIT!
|
// NOTE: Generated By hrp v4.1.3, DO NOT EDIT!
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"github.com/httprunner/funplugin"
|
"github.com/httprunner/funplugin"
|
||||||
|
"github.com/httprunner/funplugin/fungo"
|
||||||
"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"
|
||||||
@@ -57,6 +58,13 @@ func initPlugin(path, venv string, logOn bool) (plugin funplugin.IPlugin, err er
|
|||||||
log.Error().Err(err).Msg("prepare python3 venv failed")
|
log.Error().Err(err).Msg("prepare python3 venv failed")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
// check if funppy is ready in python3 venv
|
||||||
|
err = builtin.AssertPythonPackage(builtin.Python3Executable, "funppy", fungo.Version)
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err).Str("venv", venv).Msg("python package funppy is not ready")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
pluginOptions = append(pluginOptions, funplugin.WithPython3(builtin.Python3Executable))
|
pluginOptions = append(pluginOptions, funplugin.WithPython3(builtin.Python3Executable))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user