88from tomlkit import TOMLDocument , dumps , parse
99from twyn .base .constants import (
1010 DEFAULT_PROJECT_TOML_FILE ,
11+ DEFAULT_SELECTOR_METHOD ,
1112 DEFAULT_TOP_PYPI_PACKAGES ,
1213 DEFAULT_TWYN_TOML_FILE ,
1314 AvailableLoggingLevels ,
1617from twyn .config .exceptions import (
1718 AllowlistPackageAlreadyExistsError ,
1819 AllowlistPackageDoesNotExistError ,
20+ ConfigFileNotConfiguredError ,
1921 InvalidSelectorMethodError ,
2022 TOMLError ,
2123)
@@ -29,17 +31,11 @@ class TestConfigHandler:
2931 def throw_exception (self ) -> NoReturn :
3032 raise PathNotFoundError
3133
32- @patch ("twyn.file_handler.file_handler.FileHandler.read" )
33- def test_enforce_file_error (self , mock_is_file : Mock ) -> None :
34- mock_is_file .side_effect = self .throw_exception
35- with pytest .raises (TOMLError ):
36- ConfigHandler (FileHandler (DEFAULT_PROJECT_TOML_FILE ), enforce_file = True ).resolve_config ()
37-
3834 @patch ("twyn.file_handler.file_handler.FileHandler.read" )
3935 def test_no_enforce_file_on_non_existent_file (self , mock_is_file : Mock ) -> None :
4036 """Resolving the config without enforcing the file to be present gives you defaults."""
4137 mock_is_file .side_effect = self .throw_exception
42- config = ConfigHandler (FileHandler (DEFAULT_PROJECT_TOML_FILE ), enforce_file = False ).resolve_config ()
38+ config = ConfigHandler (FileHandler (DEFAULT_PROJECT_TOML_FILE )).resolve_config ()
4339
4440 assert config == TwynConfiguration (
4541 dependency_file = None ,
@@ -141,6 +137,28 @@ def test_get_default_config_file_path_twyn_file_does_not_exist(
141137 assert not twyn_path .exists ()
142138 assert ConfigHandler .get_default_config_file_path () == str (pyproject_toml_file )
143139
140+ def test_no_load_config_from_cache (self , pyproject_toml_file : Path ) -> None :
141+ """Check that in case of not loading the config from a file we use the default values."""
142+ # Config file exists
143+ assert pyproject_toml_file .exists ()
144+
145+ config = ConfigHandler ().resolve_config ()
146+
147+ assert config .allowlist == set ()
148+ assert config .dependency_file is None
149+ assert config .logging_level == AvailableLoggingLevels .warning
150+ assert config .use_cache is True
151+ assert config .selector_method == DEFAULT_SELECTOR_METHOD
152+ assert config .pypi_reference == DEFAULT_TOP_PYPI_PACKAGES
153+
154+ def test_cannot_write_if_file_not_configured (self ) -> None :
155+ with pytest .raises (ConfigFileNotConfiguredError , match = "write operation" ):
156+ ConfigHandler ()._write_toml (Mock ())
157+
158+ def test_cannot_read_if_file_not_configured (self ) -> None :
159+ with pytest .raises (ConfigFileNotConfiguredError , match = "read operation" ):
160+ ConfigHandler ()._read_toml ()
161+
144162
145163class TestAllowlistConfigHandler :
146164 @patch ("twyn.file_handler.file_handler.FileHandler.write" )
@@ -210,7 +228,7 @@ def test_valid_selector_methods_accepted(self, valid_selector: str, tmp_path: Pa
210228 """Test that all valid selector methods are accepted."""
211229 pyproject_toml = tmp_path / "pyproject.toml"
212230 pyproject_toml .write_text ("" )
213- config = ConfigHandler (FileHandler (str (pyproject_toml )), enforce_file = False )
231+ config = ConfigHandler (FileHandler (str (pyproject_toml )))
214232
215233 # Should not raise any exception
216234 resolved_config = config .resolve_config (selector_method = valid_selector )
@@ -220,7 +238,7 @@ def test_invalid_selector_method_rejected(self, tmp_path: Path) -> None:
220238 """Test that invalid selector methods are rejected with appropriate error."""
221239 pyproject_toml = tmp_path / "pyproject.toml"
222240 pyproject_toml .write_text ("" )
223- config = ConfigHandler (FileHandler (str (pyproject_toml )), enforce_file = False )
241+ config = ConfigHandler (FileHandler (str (pyproject_toml )))
224242
225243 with pytest .raises (InvalidSelectorMethodError ) as exc_info :
226244 config .resolve_config (selector_method = "random-selector" )
0 commit comments