1515
1616
1717def yaml_dump_to_string (data ):
18- """Helper function to dump YAML data to string using ruamel.yaml"""
1918 stream = io .StringIO ()
2019 yaml .dump (data , stream )
2120 return stream .getvalue ()
2221
2322
24- @pytest .fixture
25- def dummy_module_factory ():
26- """Factory fixture for creating DummyModule instances"""
23+ class DummyModule (NFCoreComponent ):
24+ def __init__ (self , path ):
25+ self .environment_yml = path
26+ self .component_dir = path .parent
27+ self .component_name = "dummy"
28+ self .passed = []
29+ self .failed = []
30+ self .warned = []
2731
28- def _create_dummy_module (path ):
29- class DummyModule (NFCoreComponent ):
30- def __init__ (self , path ):
31- self .environment_yml = path
32- self .component_dir = path .parent
33- self .component_name = "dummy"
34- self .passed = []
35- self .failed = []
36- self .warned = []
3732
38- return DummyModule (path )
33+ class DummyLint (ComponentLint ):
34+ def __init__ (self , tmp_path ):
35+ self .modules_repo = type ("repo" , (), {"local_repo_dir" : tmp_path })
36+ self .passed = []
37+ self .failed = []
3938
40- return _create_dummy_module
4139
40+ def setup_test_environment (tmp_path , content , filename = "environment.yml" ):
41+ test_file = tmp_path / filename
42+ test_file .write_text (content )
4243
43- @pytest .fixture
44- def dummy_lint_factory ():
45- """Factory fixture for creating DummyLint instances"""
44+ (tmp_path / "modules" ).mkdir (exist_ok = True )
45+ (tmp_path / "modules" / "environment-schema.json" ).write_text ("{}" )
4646
47- def _create_dummy_lint (tmp_path ):
48- class DummyLint (ComponentLint ):
49- def __init__ (self ):
50- self .modules_repo = type ("repo" , (), {"local_repo_dir" : tmp_path })
51- self .passed = []
52- self .failed = []
47+ module = DummyModule (test_file )
48+ lint = DummyLint (tmp_path )
5349
54- return DummyLint ()
50+ return test_file , module , lint
5551
56- return _create_dummy_lint
5752
58-
59- @pytest .fixture
60- def setup_lint_environment (tmp_path , dummy_module_factory , dummy_lint_factory ):
61- """Setup function that creates the necessary directory structure and dummy objects for linting"""
62-
63- def _setup (test_file_content , filename = "environment.yml" ):
64- test_file = tmp_path / filename
65- test_file .write_text (test_file_content )
66-
67- # Create required directory structure
68- (tmp_path / "modules" ).mkdir (exist_ok = True )
69- (tmp_path / "modules" / "environment-schema.json" ).write_text ("{}" )
70-
71- module = dummy_module_factory (test_file )
72- lint = dummy_lint_factory (tmp_path )
73-
74- return test_file , module , lint
75-
76- return _setup
77-
78-
79- def assert_yaml_result (test_file , expected , check_sorting = True ):
80- """Helper function to assert YAML parsing results"""
53+ def assert_yaml_result (test_file , expected ):
8154 result = test_file .read_text ()
8255 lines = result .splitlines (True )
8356
84- # Handle YAML with schema headers
8557 if lines [:2 ] == [
8658 "---\n " ,
8759 "# yaml-language-server: $schema=https://raw.githubusercontent.com/nf-core/modules/master/modules/environment-schema.json\n " ,
@@ -90,7 +62,6 @@ def assert_yaml_result(test_file, expected, check_sorting=True):
9062 else :
9163 parsed = yaml .load (result )
9264
93- # Assert expected content
9465 if isinstance (expected , list ):
9566 assert parsed ["dependencies" ] == expected
9667 else :
@@ -186,25 +157,14 @@ def assert_yaml_result(test_file, expected, check_sorting=True):
186157 },
187158 ),
188159 ],
189- ids = [
190- "basic_dependency_sorting" ,
191- "dict_dependency_sorting" ,
192- "existing_headers" ,
193- "channel_preservation" ,
194- "channel_preservation_with_additional_channels" ,
195- "namespaced_dependencies" ,
196- "mixed_dependencies" ,
197- "full_environment" ,
198- ],
199160)
200- def test_environment_yml_sorting (setup_lint_environment , input_content , expected ):
161+ def test_environment_yml_sorting (tmp_path , input_content , expected ):
201162 """Test that environment.yml files are sorted correctly"""
202- test_file , module , lint = setup_lint_environment ( input_content )
163+ test_file , module , lint = setup_test_environment ( tmp_path , input_content )
203164
204165 environment_yml (lint , module )
205166
206167 assert_yaml_result (test_file , expected )
207- # Check linter passed for sorting
208168 assert any ("environment_yml_sorted" in x for x in [p .lint_test for p in lint .passed ])
209169
210170
@@ -214,25 +174,24 @@ def test_environment_yml_sorting(setup_lint_environment, input_content, expected
214174 ("invalid: yaml: here" , "bad.yml" ),
215175 ("" , "empty.yml" ),
216176 ],
217- ids = ["invalid_yaml" , "empty_file" ],
218177)
219- def test_environment_yml_invalid_files (setup_lint_environment , invalid_content , filename ):
178+ def test_environment_yml_invalid_files (tmp_path , invalid_content , filename ):
220179 """Test that invalid YAML files raise exceptions"""
221- test_file , module , lint = setup_lint_environment ( invalid_content , filename )
180+ test_file , module , lint = setup_test_environment ( tmp_path , invalid_content , filename )
222181
223182 with pytest .raises (Exception ):
224183 environment_yml (lint , module )
225184
226185
227- def test_environment_yml_missing_dependencies (setup_lint_environment ):
186+ def test_environment_yml_missing_dependencies (tmp_path ):
228187 """Test handling of environment.yml without dependencies section"""
229188 content = "channels:\n - conda-forge\n "
230- test_file , module , lint = setup_lint_environment ( content )
189+ test_file , module , lint = setup_test_environment ( tmp_path , content )
231190
232191 environment_yml (lint , module )
233192
234193 expected = {"channels" : ["conda-forge" ]}
235- assert_yaml_result (test_file , expected , check_sorting = False )
194+ assert_yaml_result (test_file , expected )
236195
237196
238197# Integration tests using the full ModuleLint class
0 commit comments