Skip to content

Commit

Permalink
修复路径配置读取
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-clan committed Mar 10, 2024
1 parent 202db57 commit 324cc07
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 44 deletions.
1 change: 0 additions & 1 deletion httpfpt/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from httpfpt.core.path_conf import set_httpfpt_dir as set_httpfpt_dir

__version__ = 'v0.6.0'
24 changes: 6 additions & 18 deletions httpfpt/core/path_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,25 @@

import os

from typing_extensions import Self

from httpfpt.common.errors import ConfigInitError

__all__ = [
'set_httpfpt_dir',
'httpfpt_path',
]
__all__ = ['httpfpt_path']


class HttpFptPathConfig:
def __call__(self, base_dir: str) -> Self:
self._base_dir = base_dir
return self

@property
def project_dir(self) -> str:
"""项目根路径"""
_base_dir = self._base_dir if os.path.isdir(self._base_dir) else os.getenv('HTTPFPT_PROJECT_PATH')
_base_dir = os.getenv('HTTPFPT_PROJECT_PATH')
if not _base_dir:
raise ConfigInitError(
'运行失败:在访问 HTTPFPT API 前,请先通过 `httpfpt` 命令创建新项目;'
'如果已经创建,请确保配置了 `HTTPFPT_PROJECT_PATH` 环境变量为项目目录'
)
else:
if not os.path.exists(self._base_dir):
raise ConfigInitError(f'运行失败,项目路径 {self._base_dir} 不存在,请检查路径配置是否正确')
return self._base_dir
if not os.path.exists(_base_dir):
raise ConfigInitError(f'运行失败,项目路径 {_base_dir} 不存在,请检查环境变量配置是否正确')
return _base_dir

@property
def log_dir(self) -> str:
Expand Down Expand Up @@ -104,8 +95,5 @@ def settings_file_file(self) -> str:
return os.path.join(self.project_dir, 'core', 'conf.toml')


set_httpfpt_dir = HttpFptPathConfig()


# global path_config
httpfpt_path = set_httpfpt_dir('')
httpfpt_path = HttpFptPathConfig()
2 changes: 1 addition & 1 deletion httpfpt/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def startup(

run_args = [log_level]

default_case_path = os.sep.join([os.path.dirname(__file__), 'testcases', httpfpt_config.PROJECT_NAME])
default_case_path = os.sep.join([httpfpt_path.testcase_dir, httpfpt_config.PROJECT_NAME])
if case_path:
if '::' not in case_path:
raise ValueError(
Expand Down
33 changes: 9 additions & 24 deletions httpfpt/utils/cli/new_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,53 +25,38 @@ def create_new_project() -> None:
project_path = os.path.abspath(os.sep.join([path, name]))
core_path = os.path.join(project_path, 'core')
data_path = os.path.join(project_path, 'data')
init_file = os.path.join(project_path, '__init__.py')
conftest_file = os.path.join(project_path, 'conftest.py')
pytest_file = os.path.join(project_path, 'pytest.ini')
if os.path.exists(project_path):
raise cappa.Exit(f'\n❌ The "{name}" directory is not empty', code=1)
os.makedirs(project_path)
with import_path('httpfpt.core', '') as core_data:
shutil.copytree(core_data, core_path, ignore=shutil.ignore_patterns('get_conf.py', 'path_conf.py'))
patterns = ['__init__.py', 'get_conf.py', 'path_conf.py']
shutil.copytree(core_data, core_path, ignore=shutil.ignore_patterns(*patterns))
with import_path('httpfpt.data', '') as case_data:
shutil.copytree(case_data, data_path)
with import_path('httpfpt', '__init__.py') as pytest_init:
shutil.copyfile(pytest_init, init_file)
with import_path('httpfpt', 'conftest.py') as conftest:
shutil.copyfile(conftest, conftest_file)
with import_path('httpfpt', 'pytest.ini') as pytest_ini:
shutil.copyfile(pytest_ini, pytest_file)
init_tpl = f"""#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from functools import wraps
from typing import Any, Callable
from httpfpt import set_httpfpt_dir
# Init setup
set_httpfpt_dir('{project_path}')
def ensure_httpfpt_setup(func: Callable) -> Any:
@wraps(func)
def wrapper(*args, **kwargs) -> Callable:
set_httpfpt_dir('{project_path}')
return func(*args, **kwargs)
return wrapper
"""
run_tpl = """#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from httpfpt.run import run as httpfpt_run
httpfpt_run(testcase_generate=True)
"""
with open(os.path.join(project_path, '__init__.py'), 'w', encoding='utf-8') as f:
f.write(init_tpl)
with open(os.path.join(project_path, 'run.py'), 'w', encoding='utf-8') as f:
f.write(run_tpl)
console.print(
f'\n🎉 The project "{name}" has been created.'
f'\n🌴 The project is located in the directory: [cyan]{project_path}[/]'
f'\n⚠️ Before accessing HTTPFPT, be sure to set the environment variable '
f'HTTPFPT_PROJECT_PATH to the current project directory'
f'[yellow]HTTPFPT_PROJECT_PATH[/] to the current project directory'
f'\n Windows: setx HTTPFPT_PROJECT_PATH "{project_path}"'
f'\n Unix: vim ~/.bashrc'
f'\n\t export PATH=$PATH:{project_path}'
)

0 comments on commit 324cc07

Please sign in to comment.