-
Notifications
You must be signed in to change notification settings - Fork 375
Expand file tree
/
Copy pathconftest.py
More file actions
180 lines (134 loc) · 4.23 KB
/
conftest.py
File metadata and controls
180 lines (134 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
"""Shared pytest fixtures and configuration for all tests."""
import os
import shutil
import tempfile
from pathlib import Path
from typing import Generator
import pytest
@pytest.fixture
def temp_dir() -> Generator[Path, None, None]:
"""Create a temporary directory for test files.
Yields:
Path: Path to the temporary directory
"""
temp_path = Path(tempfile.mkdtemp())
yield temp_path
shutil.rmtree(temp_path)
@pytest.fixture
def sample_markdown_content() -> str:
"""Provide sample markdown content for testing.
Returns:
str: Sample markdown text
"""
return """# Sample Chapter
This is a **sample** chapter with:
- Bullet points
- [Links](https://example.com)
- `Code snippets`
## Subsection
More content here.
"""
@pytest.fixture
def sample_chapter_file(temp_dir: Path) -> Path:
"""Create a sample chapter file for testing.
Args:
temp_dir: Temporary directory fixture
Returns:
Path: Path to the created chapter file
"""
chapter_path = temp_dir / "ch01.md"
chapter_path.write_text("""# Chapter 01 - Introduction
This is the first chapter of the book.
## Key Concepts
- Machine Learning
- Deep Learning
- Neural Networks
""")
return chapter_path
@pytest.fixture
def mock_chapters_dir(temp_dir: Path) -> Path:
"""Create a mock chapters directory with sample files.
Args:
temp_dir: Temporary directory fixture
Returns:
Path: Path to the chapters directory
"""
chapters_dir = temp_dir / "chapters"
chapters_dir.mkdir()
# Create sample chapter files
for i in range(1, 4):
chapter_file = chapters_dir / f"ch{i:02d}.md"
chapter_file.write_text(f"# Chapter {i:02d}\n\nContent for chapter {i}.")
return chapters_dir
@pytest.fixture
def mock_imgs_dir(temp_dir: Path) -> Path:
"""Create a mock images directory.
Args:
temp_dir: Temporary directory fixture
Returns:
Path: Path to the images directory
"""
imgs_dir = temp_dir / "imgs"
imgs_dir.mkdir()
# Create a placeholder image file
(imgs_dir / "sample.png").write_text("placeholder")
return imgs_dir
@pytest.fixture
def mock_project_structure(temp_dir: Path, mock_chapters_dir: Path, mock_imgs_dir: Path) -> Path:
"""Create a complete mock project structure.
Args:
temp_dir: Temporary directory fixture
mock_chapters_dir: Mock chapters directory
mock_imgs_dir: Mock images directory
Returns:
Path: Path to the project root
"""
# Create additional files
(temp_dir / "README.md").write_text("# Machine Learning Yearning")
(temp_dir / "acknowledgement.md").write_text("# Acknowledgements\n\nThanks to everyone.")
(temp_dir / "glossary_vi.md").write_text("# Glossary\n\nML terms in Vietnamese.")
return temp_dir
@pytest.fixture
def env_vars() -> dict:
"""Provide test environment variables.
Returns:
dict: Dictionary of environment variables
"""
return {
"TEST_MODE": "true",
"PYTHONPATH": str(Path.cwd()),
}
@pytest.fixture
def mock_pdf_config() -> dict:
"""Provide mock PDF configuration.
Returns:
dict: PDF configuration options
"""
return {
"page-size": "A4",
"margin-top": "1in",
"margin-right": "1in",
"margin-bottom": "1in",
"margin-left": "1in",
"encoding": "UTF-8",
"enable-local-file-access": None,
}
@pytest.fixture(autouse=True)
def cleanup_test_files():
"""Automatically clean up any test files created during tests."""
yield
# Clean up any files created in the current directory during tests
test_patterns = ["test_*.md", "test_*.pdf", "test_*.html", "combined_*.md"]
for pattern in test_patterns:
for file in Path.cwd().glob(pattern):
if file.exists():
file.unlink()
@pytest.fixture
def monkeypatch_env(monkeypatch, env_vars):
"""Automatically set environment variables for tests.
Args:
monkeypatch: pytest monkeypatch fixture
env_vars: Environment variables fixture
"""
for key, value in env_vars.items():
monkeypatch.setenv(key, value)