11"""Unit tests for extraction utilities."""
22
3- import tempfile
4- from contextlib import contextmanager
53from pathlib import Path
64
75import pytest
1614)
1715
1816
19- @contextmanager
20- def temp_yaml_file (yaml_content : str ):
21- """Context manager for creating and cleaning up temporary YAML files."""
22- with tempfile .NamedTemporaryFile (mode = "w" , suffix = ".yaml" , delete = False ) as f :
23- f .write (yaml_content )
24- yaml_path = Path (f .name )
25-
26- try :
27- yield yaml_path
28- finally :
29- yaml_path .unlink ()
17+ @pytest .fixture
18+ def temp_yaml_file (tmp_path ):
19+ """Fixture for creating temporary YAML files."""
20+ def _create_yaml (yaml_content : str ) -> Path :
21+ yaml_path = tmp_path / "config.yaml"
22+ yaml_path .write_text (yaml_content )
23+ return yaml_path
24+ return _create_yaml
3025
3126
3227class TestCallPattern :
@@ -231,7 +226,7 @@ def test_extract_metrics_missing_patterns(self, sample_stats_file):
231226class TestYAMLParsing :
232227 """Tests for YAML configuration parsing."""
233228
234- def test_from_yaml_mixed_patterns (self ):
229+ def test_from_yaml_mixed_patterns (self , temp_yaml_file ):
235230 """Test parsing YAML with various pattern configurations and templates."""
236231 yaml_content = """
237232patterns:
@@ -253,8 +248,8 @@ def test_from_yaml_mixed_patterns(self):
253248 extract_percall: true
254249 cumtime_template: "custom_{name}_time"
255250"""
256- with temp_yaml_file (yaml_content ) as yaml_path :
257- config = ExtractionConfig .from_yaml (yaml_path )
251+ yaml_path = temp_yaml_file (yaml_content )
252+ config = ExtractionConfig .from_yaml (yaml_path )
258253
259254 assert len (config .patterns ) == 3
260255
@@ -283,46 +278,46 @@ def test_from_yaml_file_not_found(self):
283278 with pytest .raises (FileNotFoundError , match = "YAML config file not found" ):
284279 ExtractionConfig .from_yaml ("/nonexistent/file.yaml" )
285280
286- def test_from_yaml_missing_patterns_key (self ):
281+ def test_from_yaml_missing_patterns_key (self , temp_yaml_file ):
287282 """Test error when YAML is missing 'patterns' key."""
288283 yaml_content = """
289284some_other_key:
290285 - value: 1
291286"""
292- with temp_yaml_file (yaml_content ) as yaml_path :
293- with pytest .raises (ValueError , match = "must contain a 'patterns' key" ):
294- ExtractionConfig .from_yaml (yaml_path )
287+ yaml_path = temp_yaml_file (yaml_content )
288+ with pytest .raises (ValueError , match = "must contain a 'patterns' key" ):
289+ ExtractionConfig .from_yaml (yaml_path )
295290
296- def test_from_yaml_missing_name_field (self ):
291+ def test_from_yaml_missing_name_field (self , temp_yaml_file ):
297292 """Test error when pattern is missing 'name' field."""
298293 yaml_content = """
299294patterns:
300295 - filename: test.py
301296 function_name: test
302297"""
303- with temp_yaml_file (yaml_content ) as yaml_path :
304- with pytest .raises (ValueError , match = "missing required field 'name'" ):
305- ExtractionConfig .from_yaml (yaml_path )
298+ yaml_path = temp_yaml_file (yaml_content )
299+ with pytest .raises (ValueError , match = "missing required field 'name'" ):
300+ ExtractionConfig .from_yaml (yaml_path )
306301
307- def test_from_yaml_missing_required_fields (self ):
302+ def test_from_yaml_missing_required_fields (self , temp_yaml_file ):
308303 """Test error when pattern is missing required fields."""
309304 yaml_content = """
310305patterns:
311306 - name: my_func
312307 filename: test.py
313308"""
314- with temp_yaml_file (yaml_content ) as yaml_path :
315- with pytest .raises (
316- ValueError , match = "requires 'filename' and 'function_name' fields"
317- ):
318- ExtractionConfig .from_yaml (yaml_path )
309+ yaml_path = temp_yaml_file (yaml_content )
310+ with pytest .raises (
311+ ValueError , match = "requires 'filename' and 'function_name' fields"
312+ ):
313+ ExtractionConfig .from_yaml (yaml_path )
319314
320- def test_from_yaml_pattern_not_dict (self ):
315+ def test_from_yaml_pattern_not_dict (self , temp_yaml_file ):
321316 """Test error when pattern is not a dictionary."""
322317 yaml_content = """
323318patterns:
324319 - not_a_dict
325320"""
326- with temp_yaml_file (yaml_content ) as yaml_path :
327- with pytest .raises (ValueError , match = "must be a dictionary" ):
328- ExtractionConfig .from_yaml (yaml_path )
321+ yaml_path = temp_yaml_file (yaml_content )
322+ with pytest .raises (ValueError , match = "must be a dictionary" ):
323+ ExtractionConfig .from_yaml (yaml_path )
0 commit comments