Files
RuoYi-Vue3-FastAPI/ruoyi-fastapi-backend/tests/cli/runtime/test_app_runtime.py
T
insistence 2a055ba648 feat: 新增插件系统 (#112)
* feat: 初始化插件系统

* refactor: 收口插件系统运行时重构

* perf: 优化插件系统类型提示

* fix&perf: 修复和优化插件系统

* fix: 修复gitignore规则误忽略插件文件的问题

* fix: 修复运行时插件根路径算错的问题

* fix: 加强插件发现和路由注册的防护措施

* revert: 回滚定时任务白名单

* fix: 移除未使用的应用路由注册探测

* revert: 恢复部分代码

* perf: 优化插件系统

* docs: 新增插件开发文档

* perf: 优化插件管理模块

* perf: 提升插件系统核心能力

* refactor: 重构生命周期 step runner

* fix: 修复lint错误

* test: 清理测试用例

* test: 调整测试目录名称

* fix: 修复前后端目录硬编码的问题

* fix: 修复插件系统安全性缺口

* refactor: 重新设计插件生命周期 Migration 事务与回滚

* perf: 优化插件系统边界问题

* refactor: 重构当前插件系统的依赖体系设计

* perf: 优化代码

* perf: 优化代码

* fix: 修复代码合并问题

* fix: 修复bug

* perf: 优化代码

* perf&fix: 优化代码和修复bug

* docs: 优化文档格式

* feat: 适配Vue2版本

* docs: 更新README文档

* fix: 修复ruff lint错误

* chore: 更新后端依赖文件
2026-07-28 20:35:18 +08:00

162 lines
4.4 KiB
Python

from types import SimpleNamespace
from pytest import MonkeyPatch
from cli.runtime.app import AppRuntimeService
from cli.runtime.app.gateway import AppInfrastructureGateway
from cli.runtime.app.support import AppSnapshotSupport
from cli.runtime.base import RuntimeEnvironmentService
REDIS_PORT = 6379
class FakeRuntimeEnvironment(RuntimeEnvironmentService):
"""
模拟运行时环境服务。
"""
@staticmethod
def get_backend_dir() -> str:
"""
返回固定后端目录。
:return: 固定目录
"""
return '/tmp/ruoyi-backend'
@staticmethod
def get_python_executable() -> str:
"""
返回固定 Python 可执行文件。
:return: Python 可执行文件路径
"""
return '/usr/bin/python3'
def test_app_snapshot_support_builds_config_snapshot() -> None:
"""
校验应用快照支持对象会构建应用配置快照。
:return: None
"""
gateway = AppInfrastructureGateway()
support = AppSnapshotSupport(gateway, FakeRuntimeEnvironment())
fake_env_module = SimpleNamespace(
AppConfig=SimpleNamespace(
app_env='dev',
app_name='ruoyi',
app_host='127.0.0.1',
app_port=8080,
app_root_path='/api',
app_reload=True,
app_workers=1,
app_disable_swagger=False,
app_disable_redoc=False,
),
DataBaseConfig=SimpleNamespace(
db_type='mysql',
db_host='127.0.0.1',
db_port=3306,
db_database='ruoyi',
),
RedisConfig=SimpleNamespace(redis_host='127.0.0.1', redis_port=REDIS_PORT),
LogConfig=SimpleNamespace(loguru_level='INFO'),
TransportCryptoConfig=SimpleNamespace(
transport_crypto_enabled=True,
transport_crypto_mode='strict',
),
)
def _fake_get_env_module() -> SimpleNamespace:
return fake_env_module
object.__setattr__(gateway, 'get_env_module', _fake_get_env_module)
payload = support.build_app_config_snapshot()
assert payload['env'] == 'dev'
assert payload['dbType'] == 'mysql'
assert payload['redisPort'] == REDIS_PORT
assert payload['transportCryptoMode'] == 'strict'
def test_app_snapshot_support_builds_env_snapshot(monkeypatch: MonkeyPatch) -> None:
"""
校验应用快照支持对象会构建环境解析快照。
:param monkeypatch: pytest monkeypatch 工具
:return: None
"""
gateway = AppInfrastructureGateway()
support = AppSnapshotSupport(gateway, FakeRuntimeEnvironment())
fake_env_module = SimpleNamespace(
AppConfig=SimpleNamespace(app_env='prod'),
)
def _fake_get_env_module() -> SimpleNamespace:
return fake_env_module
object.__setattr__(gateway, 'get_env_module', _fake_get_env_module)
monkeypatch.setenv('APP_ENV', 'test')
payload = support.build_app_env_snapshot()
assert payload == {
'cliEnv': 'test',
'configEnv': 'prod',
'appEnv': 'test',
'envFile': '.env.test',
'envFilePath': '/tmp/ruoyi-backend/.env.test',
'envFileExists': False,
'backendDir': '/tmp/ruoyi-backend',
'pythonExecutable': '/usr/bin/python3',
}
def test_app_runtime_service_builds_app_instance() -> None:
"""
校验应用运行时 facade 会通过基础设施网关构建应用实例。
:return: None
"""
gateway = AppInfrastructureGateway()
service = AppRuntimeService(
runtime_environment=FakeRuntimeEnvironment(),
infrastructure_gateway=gateway,
)
class FakeServerModule:
"""
模拟 server 模块。
"""
@staticmethod
def create_app() -> dict[str, str]:
"""
返回模拟应用实例。
:return: 模拟应用实例
"""
return {'app': 'ok'}
@staticmethod
def _register_application_routers(app: dict[str, str]) -> None:
"""
模拟不存在于当前 server 模块的旧私有扩展点。
:param app: 模拟应用实例
:return: None
"""
raise AssertionError('不应调用私有路由注册扩展点')
def _fake_get_server_module() -> FakeServerModule:
return FakeServerModule()
object.__setattr__(gateway, 'get_server_module', _fake_get_server_module)
payload = service.build_app_instance()
assert payload == {'app': 'ok'}