forked from cleveragents/cleveragents-core
91 lines
2.5 KiB
Python
91 lines
2.5 KiB
Python
"""
|
|
Pytest/Behave configuration for feature tests
|
|
|
|
Provides cleanup for temporary files created during BDD tests.
|
|
"""
|
|
|
|
import pytest
|
|
import os
|
|
import tempfile
|
|
|
|
|
|
@pytest.fixture
|
|
def temp_file_tracker():
|
|
"""
|
|
Track temporary files created during a test and clean them up.
|
|
|
|
Usage in step definitions:
|
|
def step_impl(context):
|
|
if not hasattr(context, 'temp_tracker'):
|
|
context.temp_tracker = TempFileTracker()
|
|
temp_file = context.temp_tracker.create('.yaml')
|
|
# ... use temp_file ...
|
|
"""
|
|
class TempFileTracker:
|
|
def __init__(self):
|
|
self.files = []
|
|
|
|
def create(self, suffix='', mode='w', **kwargs):
|
|
"""Create a tracked temporary file."""
|
|
temp_file = tempfile.NamedTemporaryFile(
|
|
mode=mode,
|
|
prefix='cleveragent_',
|
|
suffix=suffix,
|
|
delete=False,
|
|
**kwargs
|
|
)
|
|
temp_file.close()
|
|
self.files.append(temp_file.name)
|
|
return temp_file.name
|
|
|
|
def cleanup(self):
|
|
"""Clean up all tracked files."""
|
|
for filepath in self.files:
|
|
try:
|
|
os.unlink(filepath)
|
|
except (FileNotFoundError, PermissionError):
|
|
pass
|
|
self.files = []
|
|
|
|
tracker = TempFileTracker()
|
|
yield tracker
|
|
tracker.cleanup()
|
|
|
|
|
|
@pytest.fixture(scope="session", autouse=True)
|
|
def cleanup_cleveragent_temp_files():
|
|
"""
|
|
Auto-cleanup fixture that runs after entire test session.
|
|
|
|
Cleans up CleverAgents-specific temporary files to prevent resource leaks.
|
|
Uses 'cleveragent_' prefix to avoid deleting other programs' files.
|
|
"""
|
|
yield # Tests run
|
|
|
|
# After all tests complete, clean up temp files with our prefix
|
|
import glob
|
|
|
|
# Only match files with our specific prefix
|
|
patterns = [
|
|
"/tmp/cleveragent_*.yaml",
|
|
"/tmp/cleveragent_*.json",
|
|
"/tmp/cleveragent_*.txt",
|
|
"/tmp/cleveragent_*.j2",
|
|
"/var/tmp/cleveragent_*.yaml",
|
|
"/var/tmp/cleveragent_*.json"
|
|
]
|
|
|
|
cleaned = 0
|
|
for pattern in patterns:
|
|
for temp_file in glob.glob(pattern):
|
|
try:
|
|
if os.path.exists(temp_file):
|
|
os.unlink(temp_file)
|
|
cleaned += 1
|
|
except Exception:
|
|
pass # Ignore cleanup errors
|
|
|
|
if cleaned > 0:
|
|
print(f"\n✅ Cleaned up {cleaned} CleverAgents feature test temporary files")
|
|
|