File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed
Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change 1+ import pathlib
2+ import pytest
3+
4+
5+ class TestRelativeImports :
6+ """Тест на отсутствие абсолютных путей в импортах проекта."""
7+
8+ def test_relative_imports (self ):
9+ """Проверяет отсутствие в проекте импортов с 'src'."""
10+
11+ src_dir = pathlib .Path ("src" )
12+
13+ files_with_src_imports = []
14+
15+ for py_file in src_dir .rglob ("*.py" ):
16+ content = py_file .read_text (encoding = "utf-8" )
17+
18+ for line_num , line in enumerate (content .splitlines (), 1 ):
19+ if line .strip ().startswith ('from src.' ) or line .strip ().startswith ('import src.' ):
20+ relative_path = py_file .relative_to (src_dir )
21+ files_with_src_imports .append (f"{ relative_path } :{ line_num } " )
22+
23+ if files_with_src_imports :
24+ error_msg = (
25+ "Найдены файлы с некорректными импортами:\n " +
26+ "\n " .join (f" - { file } " for file in files_with_src_imports )
27+ )
28+ pytest .fail (error_msg )
You can’t perform that action at this time.
0 commit comments