11import dataclasses
22from copy import deepcopy
3+ from pathlib import Path
34from unittest .mock import patch
45
56import pytest
910from twyn .config .exceptions import (
1011 AllowlistPackageAlreadyExistsError ,
1112 AllowlistPackageDoesNotExistError ,
13+ InvalidSelectorMethodError ,
1214 TOMLError ,
1315)
1416from twyn .file_handler .exceptions import PathNotFoundError
@@ -46,7 +48,7 @@ def test_config_raises_for_unknown_file(self):
4648 def test_read_config_values (self , pyproject_toml_file ):
4749 config = ConfigHandler (file_handler = FileHandler (pyproject_toml_file )).resolve_config ()
4850 assert config .dependency_file == "my_file.txt"
49- assert config .selector_method == "my_selector "
51+ assert config .selector_method == "all "
5052 assert config .logging_level == AvailableLoggingLevels .debug
5153 assert config .allowlist == {"boto4" , "boto2" }
5254
@@ -57,7 +59,7 @@ def test_get_twyn_data_from_file(self, pyproject_toml_file):
5759 twyn_data = ConfigHandler (FileHandler (pyproject_toml_file ))._get_read_config (toml )
5860 assert twyn_data == ReadTwynConfiguration (
5961 dependency_file = "my_file.txt" ,
60- selector_method = "my_selector " ,
62+ selector_method = "all " ,
6163 logging_level = "debug" ,
6264 allowlist = {"boto4" , "boto2" },
6365 pypi_reference = None ,
@@ -95,7 +97,7 @@ def test_write_toml(self, pyproject_toml_file):
9597 },
9698 "twyn" : {
9799 "dependency_file" : "my_file.txt" ,
98- "selector_method" : "my_selector " ,
100+ "selector_method" : "all " ,
99101 "logging_level" : "debug" ,
100102 "allowlist" : {},
101103 "pypi_reference" : DEFAULT_TOP_PYPI_PACKAGES ,
@@ -166,3 +168,46 @@ def test_allowlist_remove_non_existent_package_error(self, mock_toml, mock_write
166168 config .remove_package_from_allowlist ("mypackage2" )
167169
168170 assert not mock_write_toml .called
171+
172+ @pytest .mark .parametrize ("valid_selector" , ["first-letter" , "nearby-letter" , "all" ])
173+ def test_valid_selector_methods_accepted (self , valid_selector : str , tmp_path : Path ):
174+ """Test that all valid selector methods are accepted."""
175+ pyproject_toml = tmp_path / "pyproject.toml"
176+ pyproject_toml .write_text ("" )
177+ config = ConfigHandler (FileHandler (str (pyproject_toml )), enforce_file = False )
178+
179+ # Should not raise any exception
180+ resolved_config = config .resolve_config (selector_method = valid_selector )
181+ assert resolved_config .selector_method == valid_selector
182+
183+ def test_invalid_selector_method_rejected (self , tmp_path : Path ):
184+ """Test that invalid selector methods are rejected with appropriate error."""
185+ pyproject_toml = tmp_path / "pyproject.toml"
186+ pyproject_toml .write_text ("" )
187+ config = ConfigHandler (FileHandler (str (pyproject_toml )), enforce_file = False )
188+
189+ with pytest .raises (InvalidSelectorMethodError ) as exc_info :
190+ config .resolve_config (selector_method = "random-selector" )
191+
192+ error_message = str (exc_info .value )
193+ assert "Invalid selector_method 'random-selector'" in error_message
194+ assert "Must be one of: all, first-letter, nearby-letter" in error_message
195+
196+ def test_invalid_selector_method_from_config_file (self , tmp_path : Path ):
197+ """Test that invalid selector method from config file is rejected."""
198+ # Create a config file with invalid selector method
199+ pyproject_toml = tmp_path / "pyproject.toml"
200+ data = """
201+ [tool.twyn]
202+ selector_method="invalid-selector"
203+ """
204+ pyproject_toml .write_text (data )
205+
206+ config = ConfigHandler (FileHandler (str (pyproject_toml )))
207+
208+ with pytest .raises (InvalidSelectorMethodError ) as exc_info :
209+ config .resolve_config ()
210+
211+ error_message = str (exc_info .value )
212+ assert "Invalid selector_method 'invalid-selector'" in error_message
213+ assert "Must be one of: all, first-letter, nearby-letter" in error_message
0 commit comments