-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ca93844
commit cc95c26
Showing
2 changed files
with
71 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,72 @@ | ||
import os | ||
import shutil | ||
import tempfile | ||
import pytest | ||
import nonebot | ||
from nonebot.adapters.onebot.v11 import Adapter as ONEBOT_V11Adapter | ||
|
||
@pytest.fixture | ||
def temp_config_file(): | ||
with tempfile.TemporaryDirectory() as temp_dir: | ||
src_config_path = 'src/tools/config/_config.yml' | ||
temp_config_dir = os.path.join(temp_dir, 'src/tools/config') | ||
os.makedirs(temp_config_dir, exist_ok=True) | ||
|
||
temp_config_path = os.path.join(temp_config_dir, 'config.yml') | ||
shutil.copy(src_config_path, temp_config_path) | ||
|
||
original_config_path = os.getenv("CONFIG_PATH") | ||
os.environ["CONFIG_PATH"] = temp_config_path | ||
|
||
yield temp_config_path | ||
|
||
if original_config_path: | ||
os.environ["CONFIG_PATH"] = original_config_path | ||
else: | ||
del os.environ["CONFIG_PATH"] | ||
|
||
@pytest.mark.asyncio | ||
async def test_nonebot_initialization(temp_config_file): | ||
nonebot.init() | ||
|
||
app = nonebot.get_asgi() | ||
|
||
driver = nonebot.get_driver() | ||
driver.register_adapter(ONEBOT_V11Adapter) | ||
|
||
nonebot.load_from_toml("pyproject.toml") | ||
from unittest.mock import patch, MagicMock | ||
import bot | ||
|
||
@pytest.fixture(scope="module") | ||
def temp_config_file(tmpdir_factory): | ||
temp_dir = tmpdir_factory.mktemp("config") | ||
temp_config_path = os.path.join(temp_dir, "src/tools/config/config.yml") | ||
|
||
os.makedirs(os.path.dirname(temp_config_path), exist_ok=True) | ||
|
||
example_config_path = "src/tools/config/_config.yml" | ||
shutil.copy(example_config_path, temp_config_path) | ||
|
||
bot.config_path = temp_config_path | ||
|
||
yield temp_config_path | ||
|
||
shutil.rmtree(temp_dir) | ||
|
||
def test_config_file_exists(temp_config_file): | ||
assert os.path.exists(temp_config_file), "配置文件不存在" | ||
|
||
def test_ensure_folder_exists(): | ||
test_path = "test_folder" | ||
|
||
assert app is not None | ||
assert driver is not None | ||
if os.path.isdir(test_path): | ||
os.rmdir(test_path) | ||
|
||
assert not os.path.isdir(test_path), "测试文件夹已存在" | ||
|
||
bot.ensure_folder_exists(test_path) | ||
assert os.path.isdir(test_path), "测试文件夹未被创建" | ||
|
||
os.rmdir(test_path) | ||
|
||
def test_ensure_folders_exist(): | ||
test_structure = { | ||
"test_src": { | ||
"test_data": None, | ||
"test_cache": None | ||
} | ||
} | ||
|
||
if os.path.isdir("test_src"): | ||
for root, dirs, files in os.walk("test_src", topdown=False): | ||
for name in files: | ||
os.remove(os.path.join(root, name)) | ||
for name in dirs: | ||
os.rmdir(os.path.join(root, name)) | ||
os.rmdir("test_src") | ||
|
||
assert not os.path.isdir("test_src"), "测试文件夹结构已存在" | ||
|
||
bot.ensure_folders_exist(test_structure) | ||
assert os.path.isdir("test_src"), "根文件夹未被创建" | ||
assert os.path.isdir("test_src/test_data"), "子文件夹未被创建" | ||
assert os.path.isdir("test_src/test_cache"), "子文件夹未被创建" | ||
|
||
for root, dirs, files in os.walk("test_src", topdown=False): | ||
for name in files: | ||
os.remove(os.path.join(root, name)) | ||
for name in dirs: | ||
os.rmdir(os.path.join(root, name)) | ||
os.rmdir("test_src") | ||
|
||
assert nonebot.get_driver().adapters | ||
@patch("nonebot.init", MagicMock()) | ||
def test_nonebot_initialization(): | ||
bot.nonebot.init() | ||
assert bot.nonebot.init.called, "Nonebot 初始化未被调用" |