1
0
mirror of https://github.com/240596448/onec_codetemplate_parser.git synced 2025-11-23 21:34:39 +02:00
Files
onec_codetemplate_parser/tests/conftest.py

86 lines
3.2 KiB
Python
Raw Normal View History

2025-10-31 15:23:24 +03:00
"""fixtures for tests"""
import os
2025-10-21 22:33:34 +03:00
from pathlib import Path
2025-11-04 20:34:03 +03:00
from types import SimpleNamespace
2025-10-31 15:23:24 +03:00
import pytest
2025-10-21 22:33:34 +03:00
def get_all_fixtures():
2025-11-04 16:08:26 +03:00
"""Автоматически находим все файлы в директории тестовых данных."""
2025-11-04 20:34:03 +03:00
def from_dir():
st_files = Path(__file__).parent.glob("fixtures/*.st")
list_st_files = [f for f in st_files if f.is_file()]
return list_st_files
def from_env():
# Добавляем файлы из внешнего списка, если он задан
list_st_files = []
file_list = os.getenv("TEMPLATES_LIST")
if file_list:
if not Path(file_list).is_file():
raise FileNotFoundError(f"Файл списка дополнительных шаблонов не найден: {file_list}")
file_lines = Path(file_list).read_text(encoding='utf-8-sig').splitlines()
for item in file_lines:
item_path = Path(item).expanduser() if item.startswith("~") else Path(item)
if item_path.is_file():
list_st_files.append(item_path)
else:
raise FileNotFoundError(
f"Файл шаблона из списка дополнительных файлов "
f"({Path(file_list).name}) не найден: {item_path}")
return list_st_files
list_st_files = from_dir()
list_st_files.extend(from_env())
result = []
for f in list_st_files:
if f.name.startswith("00-"):
spec = {"level": 0, "objects": 0}
elif f.name.startswith("01-"):
spec = {"level": 1, "objects": 1}
elif f.name.startswith("02-"):
spec = {"level": 1, "objects": 2}
else:
spec = {"level": None, "objects": None}
spec["name"] = f.name
spec["path"] = f
result.append(SimpleNamespace(**spec))
return [pytest.param(r, id=r.name) for r in result]
@pytest.fixture(scope="class", params=get_all_fixtures())
def file_path_spec(request):
return request.param
@pytest.fixture(scope="class")
def file_path(file_path_spec):
2025-11-04 16:08:26 +03:00
"""Путь к каждому тестовому файлу."""
2025-11-04 20:34:03 +03:00
return file_path_spec.path
@pytest.fixture(scope="class")
def file_data_spec(file_path_spec):
"""Данные каждого тестового файла."""
file_path_spec.data = file_path_spec.path.read_text(encoding='utf-8-sig')
return file_path_spec
2025-10-21 22:33:34 +03:00
@pytest.fixture(scope="class")
2025-11-04 20:34:03 +03:00
def file_data(file_data_spec):
2025-11-04 16:08:26 +03:00
"""Данные каждого тестового файла."""
2025-11-04 20:34:03 +03:00
return file_data_spec.data
2025-10-21 22:33:34 +03:00
2025-11-01 02:18:51 +03:00
@pytest.fixture()
def temp_src(tmp_path):
2025-11-04 16:08:26 +03:00
"""Создаёт временную папку 'src' для каждого теста."""
src_path = tmp_path / "src"
src_path.mkdir()
return src_path
@pytest.fixture()
def temp_output_st(tmp_path):
2025-11-04 16:08:26 +03:00
"""Создаёт временный файл для вывода каждого теста."""
output_path = tmp_path / "output.st"
output_path.touch()
return output_path