Skip to content

Commit 1614bec

Browse files
authored
修复命令行传递额外参数中有空格问题 (#75)
应该使用shlex.split而不是字符串的split
1 parent 15d31f3 commit 1614bec

4 files changed

Lines changed: 30 additions & 6 deletions

File tree

‎pytest/pyproject.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "testsolar-pytestx"
3-
version = "0.1.26"
3+
version = "0.1.27"
44
description = "Pytest tool for TestSolar"
55
authors = [
66
{name = "asiazhang2002", email = "asiazhang2002@gmail.com"},

‎pytest/src/testsolar_pytestx/executor.py‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from typing import BinaryIO, Optional, Dict, Any, List, Callable
55

66
import pytest
7+
import shlex
78
from loguru import logger
89
from pytest import Item, Session
910

@@ -66,9 +67,7 @@ def run_testcases(
6667
args.append("--alluredir={}".format(allure_dir))
6768
initialization_allure_dir(allure_dir)
6869

69-
extra_args = os.environ.get("TESTSOLAR_TTP_EXTRAARGS", "")
70-
if extra_args:
71-
args.extend(extra_args.split())
70+
append_extra_args(args)
7271

7372
reporter: Reporter = Reporter(pipe_io=pipe_io)
7473

@@ -107,6 +106,12 @@ def run_testcases(
107106
logger.info("pytest process exit")
108107

109108

109+
def append_extra_args(args: List[str]) -> None:
110+
extra_args = os.environ.get("TESTSOLAR_TTP_EXTRAARGS", "")
111+
if extra_args:
112+
args.extend(shlex.split(extra_args))
113+
114+
110115
class PytestExecutor:
111116
def __init__(
112117
self,

‎pytest/tests/test_executor.py‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
import unittest
33
from datetime import datetime, timedelta
44
from pathlib import Path
5+
from unittest import mock
56

67
from testsolar_testtool_sdk.model.param import EntryParam
78
from testsolar_testtool_sdk.model.testresult import ResultType, LogLevel
89
from testsolar_testtool_sdk.pipe_reader import read_test_result
910

10-
from testsolar_pytestx.executor import run_testcases
11+
from testsolar_pytestx.executor import run_testcases, append_extra_args
1112

1213

1314
def convert_to_datetime(raw: str) -> datetime:
@@ -243,3 +244,21 @@ def test_run_datadrive_with_utf8_str(self):
243244
end = read_test_result(pipe_io)
244245
self.assertEqual(end.ResultType, ResultType.SUCCEED)
245246
self.assertEqual(len(end.Steps), 3)
247+
248+
def test_split_args_with_space(self):
249+
args = []
250+
251+
with mock.patch.dict(
252+
"os.environ",
253+
{"TESTSOLAR_TTP_EXTRAARGS": '-m "not p3" -t timeout -l "fast iu897 nuh"'},
254+
clear=True,
255+
):
256+
append_extra_args(args)
257+
258+
self.assertEqual(len(args), 6)
259+
self.assertEqual(args[0], "-m")
260+
self.assertEqual(args[1], "not p3")
261+
self.assertEqual(args[2], "-t")
262+
self.assertEqual(args[3], "timeout")
263+
self.assertEqual(args[4], "-l")
264+
self.assertEqual(args[5], "fast iu897 nuh")

‎pytest/testtool.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ schemaVersion: 1.0
22
name: pytest
33
nameZh: pytest自动化测试
44
lang: python
5-
version: '0.1.26'
5+
version: '0.1.27'
66
langType: INTERPRETED
77
description: |-
88
pytest是一个成熟的全功能Python测试工具,可以帮助您编写更好的程序。此测试工具允许您在TestSolar上运行pytest。

0 commit comments

Comments
 (0)