Skip to content

Commit

Permalink
<pytest>[feat]only install chromium
Browse files Browse the repository at this point in the history
  • Loading branch information
HornCopper committed Aug 16, 2024
1 parent ca93844 commit cc95c26
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 60 deletions.
27 changes: 4 additions & 23 deletions .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ on:

jobs:
build:
runs-on: ${{ matrix.os }}
runs-on: ubuntu-latest
strategy:
matrix:
os: [ubuntu-latest]
python-version: [ '3.9.13' ]
steps:
- uses: actions/checkout@v4
Expand All @@ -23,30 +22,12 @@ jobs:
- name: Display Python version
run: python --version

- name: Update and install system dependencies
run: |
sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get install -y \
libvpx-dev \
libevent-dev \
libopus-dev \
libgstreamer1.0-dev \
libgstreamer-plugins-base1.0-dev \
libharfbuzz-dev \
libhyphen-dev \
libflite1 \
libgles2-mesa-dev \
libx264-dev \
libunwind-dev
- name: Install Python dependencies
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install pytest pytest-asyncio pytest-timeout
python -m pip install pytest pytest-asyncio
python -m pip install -r requirements.txt
playwright install chromium
- name: Run tests
run: |
pytest -v --full-trace --durations 10 --timeout=1200 test.py
pytest -v --full-trace --durations 10 test.py
104 changes: 67 additions & 37 deletions test.py
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 初始化未被调用"

0 comments on commit cc95c26

Please sign in to comment.