Skip to content

Commit

Permalink
更新项目配置和路径配置 (#168)
Browse files Browse the repository at this point in the history
* 更新项目配置全局变量名

* 更新路径配置全局变量名

* 更新项目配置
  • Loading branch information
wu-clan authored Mar 18, 2024
1 parent 2f92510 commit 8247f89
Show file tree
Hide file tree
Showing 25 changed files with 276 additions and 224 deletions.
4 changes: 2 additions & 2 deletions httpfpt/common/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from loguru import logger

from httpfpt.core.path_conf import LOG_PATH
from httpfpt.core.path_conf import httpfpt_path

if TYPE_CHECKING:
import loguru
Expand All @@ -28,7 +28,7 @@ def log() -> loguru.Logger:
:return:
"""
log_path = LOG_PATH
log_path = httpfpt_path.log_dir

if not os.path.join(log_path):
os.makedirs(log_path)
Expand Down
22 changes: 11 additions & 11 deletions httpfpt/common/send_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from httpfpt.common.errors import AssertError, SendRequestError
from httpfpt.common.log import log
from httpfpt.core.get_conf import config
from httpfpt.core.get_conf import httpfpt_config
from httpfpt.db.mysql_db import mysql_client
from httpfpt.enums.request.body import BodyType
from httpfpt.enums.request.engin import EnginType
Expand Down Expand Up @@ -63,11 +63,11 @@ def _requests_engin(**kwargs) -> RequestsResponse:
:param kwargs:
:return:
"""
kwargs['timeout'] = kwargs['timeout'] or config.REQUEST_TIMEOUT
kwargs['verify'] = kwargs['verify'] or config.REQUEST_VERIFY
kwargs['proxies'] = kwargs['proxies'] or config.REQUEST_PROXIES_REQUESTS
kwargs['allow_redirects'] = kwargs['allow_redirects'] or config.REQUEST_REDIRECTS
request_retry = kwargs['retry'] or config.REQUEST_RETRY
kwargs['timeout'] = kwargs['timeout'] or httpfpt_config.REQUEST_TIMEOUT
kwargs['verify'] = kwargs['verify'] or httpfpt_config.REQUEST_VERIFY
kwargs['proxies'] = kwargs['proxies'] or httpfpt_config.REQUEST_PROXIES_REQUESTS
kwargs['allow_redirects'] = kwargs['allow_redirects'] or httpfpt_config.REQUEST_REDIRECTS
request_retry = kwargs['retry'] or httpfpt_config.REQUEST_RETRY
del kwargs['retry']
# 消除安全警告
requests.packages.urllib3.disable_warnings() # type: ignore
Expand All @@ -93,11 +93,11 @@ def _httpx_engin(**kwargs) -> HttpxResponse:
:param kwargs:
:return:
"""
kwargs['timeout'] = kwargs['timeout'] or config.REQUEST_TIMEOUT
verify = kwargs['verify'] or config.REQUEST_VERIFY
proxies = kwargs['proxies'] or config.REQUEST_PROXIES_HTTPX
redirects = kwargs['allow_redirects'] or config.REQUEST_REDIRECTS
request_retry = kwargs['retry'] or config.REQUEST_RETRY
kwargs['timeout'] = kwargs['timeout'] or httpfpt_config.REQUEST_TIMEOUT
verify = kwargs['verify'] or httpfpt_config.REQUEST_VERIFY
proxies = kwargs['proxies'] or httpfpt_config.REQUEST_PROXIES_HTTPX
redirects = kwargs['allow_redirects'] or httpfpt_config.REQUEST_REDIRECTS
request_retry = kwargs['retry'] or httpfpt_config.REQUEST_RETRY
del kwargs['verify']
del kwargs['proxies']
del kwargs['allow_redirects']
Expand Down
8 changes: 4 additions & 4 deletions httpfpt/common/yaml_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import yaml

from httpfpt.common.log import log
from httpfpt.core.path_conf import TEST_DATA_PATH, YAML_REPORT_PATH
from httpfpt.core.path_conf import httpfpt_path
from httpfpt.utils.time_control import get_current_time


Expand Down Expand Up @@ -78,7 +78,7 @@ def write_yaml_report(
:param mode: 文件写入模式
:return
"""
_yaml_report_path = YAML_REPORT_PATH
_yaml_report_path = httpfpt_path.yaml_report_dir
if not os.path.exists(_yaml_report_path):
os.makedirs(_yaml_report_path)
_file = os.path.join(_yaml_report_path, filename)
Expand All @@ -99,9 +99,9 @@ def write_yaml_vars(data: dict) -> None:
:param data:
:return:
"""
_file = os.path.join(TEST_DATA_PATH, 'global_vars.yaml')
_file = os.path.join(httpfpt_path.data_dir, 'global_vars.yaml')
try:
_vars = read_yaml(TEST_DATA_PATH, filename='global_vars.yaml')
_vars = read_yaml(httpfpt_path.data_dir, filename='global_vars.yaml')
_vars.update(data)
with open(_file, encoding='utf-8', mode='w') as f:
yaml.dump(_vars, f, allow_unicode=True)
Expand Down
8 changes: 4 additions & 4 deletions httpfpt/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from httpfpt.common.log import log
from httpfpt.common.variable_cache import variable_cache
from httpfpt.common.yaml_handler import write_yaml_report
from httpfpt.core.get_conf import config as sys_config
from httpfpt.core.get_conf import httpfpt_config


@pytest.fixture(scope='session', autouse=True)
Expand Down Expand Up @@ -62,7 +62,7 @@ def pytest_configure(config):
if metadata:
from pytest_metadata.plugin import metadata_key

config.stash[metadata_key]['Project Name'] = sys_config.PROJECT_NAME
config.stash[metadata_key]['Project Name'] = httpfpt_config.PROJECT_NAME
del config.stash[metadata_key]['Packages']
del config.stash[metadata_key]['Platform']
del config.stash[metadata_key]['Plugins']
Expand All @@ -76,7 +76,7 @@ def pytest_html_results_summary(prefix):
:return:
"""
# 向 html 报告中的 summary 添加额外信息
prefix.extend([html.p(f'Tester: {sys_config.TESTER_NAME}')])
prefix.extend([html.p(f'Tester: {httpfpt_config.TESTER_NAME}')])


@pytest.mark.optionalhook
Expand All @@ -87,7 +87,7 @@ def pytest_html_report_title(report):
:param report:
:return:
"""
report.title = f'{sys_config.TEST_REPORT_TITLE}'
report.title = f'{httpfpt_config.TEST_REPORT_TITLE}'


@pytest.mark.optionalhook
Expand Down
112 changes: 56 additions & 56 deletions httpfpt/core/get_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,103 +8,103 @@
from httpfpt.common.toml_handler import read_toml


class Config:
class HttpFptConfig:
def __init__(self) -> None:
self.__config = read_toml(str(Path(__file__).resolve().parent), 'conf.toml')
self.settings = read_toml(str(Path(__file__).resolve().parent), 'conf.toml')

# 项目目录名
self.PROJECT_NAME = glom(self.__config, 'project.name')
self.PROJECT_NAME = glom(self.settings, 'project.name')

# 测试报告
self.TEST_REPORT_TITLE = glom(self.__config, 'report.title')
self.TESTER_NAME = glom(self.__config, 'report.tester_name')
self.TEST_REPORT_TITLE = glom(self.settings, 'report.title')
self.TESTER_NAME = glom(self.settings, 'report.tester_name')

# mysql 数据库
self.MYSQL_HOST = glom(self.__config, 'mysql.host')
self.MYSQL_PORT = glom(self.__config, 'mysql.port')
self.MYSQL_USER = glom(self.__config, 'mysql.user')
self.MYSQL_PASSWORD = glom(self.__config, 'mysql.password')
self.MYSQL_DATABASE = glom(self.__config, 'mysql.database')
self.MYSQL_CHARSET = glom(self.__config, 'mysql.charset')
self.MYSQL_HOST = glom(self.settings, 'mysql.host')
self.MYSQL_PORT = glom(self.settings, 'mysql.port')
self.MYSQL_USER = glom(self.settings, 'mysql.user')
self.MYSQL_PASSWORD = glom(self.settings, 'mysql.password')
self.MYSQL_DATABASE = glom(self.settings, 'mysql.database')
self.MYSQL_CHARSET = glom(self.settings, 'mysql.charset')

# redis 数据库
self.REDIS_HOST = glom(self.__config, 'redis.host')
self.REDIS_PORT = glom(self.__config, 'redis.port')
self.REDIS_PASSWORD = glom(self.__config, 'redis.password')
self.REDIS_DATABASE = glom(self.__config, 'redis.database')
self.REDIS_TIMEOUT = glom(self.__config, 'redis.timeout')
self.REDIS_HOST = glom(self.settings, 'redis.host')
self.REDIS_PORT = glom(self.settings, 'redis.port')
self.REDIS_PASSWORD = glom(self.settings, 'redis.password')
self.REDIS_DATABASE = glom(self.settings, 'redis.database')
self.REDIS_TIMEOUT = glom(self.settings, 'redis.timeout')

# 邮件
self.EMAIL_SERVER = glom(self.__config, 'email.host')
self.EMAIL_PORT = glom(self.__config, 'email.port')
self.EMAIL_USER = glom(self.__config, 'email.user')
self.EMAIL_PASSWORD = glom(self.__config, 'email.password')
self.EMAIL_SEND_TO = glom(self.__config, 'email.receiver')
self.EMAIL_SSL = glom(self.__config, 'email.ssl')
self.EMAIL_SEND = glom(self.__config, 'email.send')
self.EMAIL_SERVER = glom(self.settings, 'email.host')
self.EMAIL_PORT = glom(self.settings, 'email.port')
self.EMAIL_USER = glom(self.settings, 'email.user')
self.EMAIL_PASSWORD = glom(self.settings, 'email.password')
self.EMAIL_SEND_TO = glom(self.settings, 'email.receiver')
self.EMAIL_SSL = glom(self.settings, 'email.ssl')
self.EMAIL_SEND = glom(self.settings, 'email.send')

# 钉钉
self.DINGDING_WEBHOOK = glom(self.__config, 'dingding.webhook')
self.DINGDING_WEBHOOK = glom(self.settings, 'dingding.webhook')
self.DINGDING_PROXY = {
'http': glom(self.__config, 'dingding.proxies.http')
if glom(self.__config, 'dingding.proxies.http') != ''
'http': glom(self.settings, 'dingding.proxies.http')
if glom(self.settings, 'dingding.proxies.http') != ''
else None,
'https': glom(self.__config, 'dingding.proxies.https')
if glom(self.__config, 'dingding.proxies.https') != ''
'https': glom(self.settings, 'dingding.proxies.https')
if glom(self.settings, 'dingding.proxies.https') != ''
else None,
}
self.DINGDING_SEND = glom(self.__config, 'dingding.send')
self.DINGDING_SEND = glom(self.settings, 'dingding.send')

# 飞书
self.FEISHU_WEBHOOK = glom(self.__config, 'feishu.webhook')
self.FEISHU_WEBHOOK = glom(self.settings, 'feishu.webhook')
self.FEISHU_PROXY = {
'http': glom(self.__config, 'feishu.proxies.http')
if glom(self.__config, 'feishu.proxies.http') != ''
'http': glom(self.settings, 'feishu.proxies.http')
if glom(self.settings, 'feishu.proxies.http') != ''
else None,
'https': glom(self.__config, 'feishu.proxies.https')
if glom(self.__config, 'feishu.proxies.https') != ''
'https': glom(self.settings, 'feishu.proxies.https')
if glom(self.settings, 'feishu.proxies.https') != ''
else None,
}
self.FEISHU_SEND = glom(self.__config, 'feishu.send')
self.FEISHU_SEND = glom(self.settings, 'feishu.send')

# 企业微信
self.WECHAT_WEBHOOK = glom(self.__config, 'wechat.webhook')
self.WECHAT_WEBHOOK = glom(self.settings, 'wechat.webhook')
self.WECHAT_PROXY = {
'http': glom(self.__config, 'wechat.proxies.http')
if glom(self.__config, 'wechat.proxies.http') != ''
'http': glom(self.settings, 'wechat.proxies.http')
if glom(self.settings, 'wechat.proxies.http') != ''
else None,
'https': glom(self.__config, 'wechat.proxies.https')
if glom(self.__config, 'wechat.proxies.https') != ''
'https': glom(self.settings, 'wechat.proxies.https')
if glom(self.settings, 'wechat.proxies.https') != ''
else None,
}
self.WECHAT_SEND = glom(self.__config, 'wechat.send')
self.WECHAT_SEND = glom(self.settings, 'wechat.send')

# 请求发送
self.REQUEST_TIMEOUT = glom(self.__config, 'request.timeout')
self.REQUEST_VERIFY = glom(self.__config, 'request.verify')
self.REQUEST_REDIRECTS = glom(self.__config, 'request.redirects')
self.REQUEST_TIMEOUT = glom(self.settings, 'request.timeout')
self.REQUEST_VERIFY = glom(self.settings, 'request.verify')
self.REQUEST_REDIRECTS = glom(self.settings, 'request.redirects')
self.REQUEST_PROXIES_REQUESTS = {
'http': glom(self.__config, 'request.proxies.http')
if glom(self.__config, 'request.proxies.http') != ''
'http': glom(self.settings, 'request.proxies.http')
if glom(self.settings, 'request.proxies.http') != ''
else None,
'https': glom(self.__config, 'request.proxies.https')
if glom(self.__config, 'request.proxies.https') != ''
'https': glom(self.settings, 'request.proxies.https')
if glom(self.settings, 'request.proxies.https') != ''
else None,
}
self.REQUEST_PROXIES_HTTPX = {
'http://': glom(self.__config, 'request.proxies.http')
if glom(self.__config, 'request.proxies.http') != ''
'http://': glom(self.settings, 'request.proxies.http')
if glom(self.settings, 'request.proxies.http') != ''
else None,
'https://': glom(self.__config, 'request.proxies.https')
if glom(self.__config, 'request.proxies.https') != ''
'https://': glom(self.settings, 'request.proxies.https')
if glom(self.settings, 'request.proxies.https') != ''
else None,
}
self.REQUEST_RETRY = glom(self.__config, 'request.retry')
self.REQUEST_RETRY = glom(self.settings, 'request.retry')


@lru_cache(maxsize=None)
def cache_config() -> Config:
return Config()
def cache_httpfpt_config() -> HttpFptConfig:
return HttpFptConfig()


config = cache_config()
httpfpt_config = cache_httpfpt_config()
Loading

0 comments on commit 8247f89

Please sign in to comment.