-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathconftest.py
More file actions
43 lines (35 loc) · 1.32 KB
/
conftest.py
File metadata and controls
43 lines (35 loc) · 1.32 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
"""
Pytest configuration file for Backtrader tests.
Handles cleanup of temp directories to avoid permission issues on Windows.
"""
import os
import shutil
import tempfile
import stat
import glob
def remove_readonly(func, path, excinfo):
"""Error handler for shutil.rmtree to handle read-only files on Windows."""
try:
os.chmod(path, stat.S_IWRITE)
func(path)
except Exception:
pass
def pytest_configure(config):
"""Clean up old pytest temp directories before running tests.
This fixes the PermissionError on Windows when running with pytest-xdist (-n flag).
IMPORTANT: Skip cleanup when running as an xdist worker to avoid deleting
temp directories that other workers or the controller are actively using.
"""
# Skip cleanup on xdist workers — only the controller should clean up
if hasattr(config, 'workerinput'):
return
# Get the project root directory
root_dir = os.path.dirname(os.path.abspath(__file__))
# Clean up old timestamped pytest_tmp directories
for pattern in ['.pytest_tmp_*', '.pytest_tmp']:
for path in glob.glob(os.path.join(root_dir, pattern)):
try:
if os.path.isdir(path):
shutil.rmtree(path, onerror=remove_readonly)
except Exception:
pass