11import dataclasses
22from copy import deepcopy
33from pathlib import Path
4- from unittest .mock import patch
4+ from typing import NoReturn
5+ from unittest .mock import Mock , patch
56
67import pytest
78from tomlkit import TOMLDocument , dumps , parse
8- from twyn .base .constants import DEFAULT_PROJECT_TOML_FILE , DEFAULT_TOP_PYPI_PACKAGES , AvailableLoggingLevels
9+ from twyn .base .constants import (
10+ DEFAULT_PROJECT_TOML_FILE ,
11+ DEFAULT_TOP_PYPI_PACKAGES ,
12+ DEFAULT_TWYN_TOML_FILE ,
13+ AvailableLoggingLevels ,
14+ )
915from twyn .config .config_handler import ConfigHandler , ReadTwynConfiguration , TwynConfiguration
1016from twyn .config .exceptions import (
1117 AllowlistPackageAlreadyExistsError ,
1622from twyn .file_handler .exceptions import PathNotFoundError
1723from twyn .file_handler .file_handler import FileHandler
1824
25+ from tests .conftest import create_tmp_file
26+
1927
20- class TestConfig :
21- def throw_exception (self ):
28+ class TestConfigHandler :
29+ def throw_exception (self ) -> NoReturn :
2230 raise PathNotFoundError
2331
2432 @patch ("twyn.file_handler.file_handler.FileHandler.read" )
25- def test_enforce_file_error (self , mock_is_file ) :
33+ def test_enforce_file_error (self , mock_is_file : Mock ) -> None :
2634 mock_is_file .side_effect = self .throw_exception
2735 with pytest .raises (TOMLError ):
2836 ConfigHandler (FileHandler (DEFAULT_PROJECT_TOML_FILE ), enforce_file = True ).resolve_config ()
2937
3038 @patch ("twyn.file_handler.file_handler.FileHandler.read" )
31- def test_no_enforce_file_on_non_existent_file (self , mock_is_file ) :
39+ def test_no_enforce_file_on_non_existent_file (self , mock_is_file : Mock ) -> None :
3240 """Resolving the config without enforcing the file to be present gives you defaults."""
3341 mock_is_file .side_effect = self .throw_exception
3442 config = ConfigHandler (FileHandler (DEFAULT_PROJECT_TOML_FILE ), enforce_file = False ).resolve_config ()
@@ -41,18 +49,18 @@ def test_no_enforce_file_on_non_existent_file(self, mock_is_file):
4149 pypi_reference = DEFAULT_TOP_PYPI_PACKAGES ,
4250 )
4351
44- def test_config_raises_for_unknown_file (self ):
52+ def test_config_raises_for_unknown_file (self ) -> None :
4553 with pytest .raises (TOMLError ):
4654 ConfigHandler (FileHandler ("non-existent-file.toml" )).resolve_config ()
4755
48- def test_read_config_values (self , pyproject_toml_file ) :
56+ def test_read_config_values (self , pyproject_toml_file : str ) -> None :
4957 config = ConfigHandler (file_handler = FileHandler (pyproject_toml_file )).resolve_config ()
5058 assert config .dependency_file == "my_file.txt"
5159 assert config .selector_method == "all"
5260 assert config .logging_level == AvailableLoggingLevels .debug
5361 assert config .allowlist == {"boto4" , "boto2" }
5462
55- def test_get_twyn_data_from_file (self , pyproject_toml_file ) :
63+ def test_get_twyn_data_from_file (self , pyproject_toml_file : str ) -> None :
5664 handler = ConfigHandler (FileHandler (str (pyproject_toml_file )))
5765
5866 toml = handler ._read_toml ()
@@ -65,7 +73,7 @@ def test_get_twyn_data_from_file(self, pyproject_toml_file):
6573 pypi_reference = None ,
6674 )
6775
68- def test_write_toml (self , pyproject_toml_file ) :
76+ def test_write_toml (self , pyproject_toml_file : str ) -> None :
6977 handler = ConfigHandler (FileHandler (pyproject_toml_file ))
7078 toml = handler ._read_toml ()
7179
@@ -105,11 +113,20 @@ def test_write_toml(self, pyproject_toml_file):
105113 }
106114 }
107115
116+ def test_get_default_config_file_path_twyn_file_exists (self , pyproject_toml_file : Path ) -> None :
117+ assert pyproject_toml_file .exists ()
118+ with create_tmp_file (Path ("twyn.toml" ), "" ):
119+ assert ConfigHandler .get_default_config_file_path () == DEFAULT_TWYN_TOML_FILE
120+
121+ def test_get_default_config_file_path_twyn_file_does_not_exist (self , pyproject_toml_file : Path ) -> None :
122+ assert pyproject_toml_file .exists ()
123+ assert ConfigHandler .get_default_config_file_path () == DEFAULT_TWYN_TOML_FILE
124+
108125
109126class TestAllowlistConfigHandler :
110127 @patch ("twyn.file_handler.file_handler.FileHandler.write" )
111128 @patch ("twyn.config.config_handler.ConfigHandler._read_toml" )
112- def test_allowlist_add (self , mock_toml , mock_write_toml ) :
129+ def test_allowlist_add (self , mock_toml : Mock , mock_write_toml : Mock ) -> None :
113130 mock_toml .return_value = TOMLDocument ()
114131
115132 config = ConfigHandler (FileHandler ("some-file" ))
@@ -123,7 +140,7 @@ def test_allowlist_add(self, mock_toml, mock_write_toml):
123140
124141 @patch ("twyn.config.config_handler.ConfigHandler._write_toml" )
125142 @patch ("twyn.config.config_handler.ConfigHandler._read_toml" )
126- def test_allowlist_add_duplicate_error (self , mock_toml , mock_write_toml ) :
143+ def test_allowlist_add_duplicate_error (self , mock_toml : Mock , mock_write_toml : Mock ) -> None :
127144 mock_toml .return_value = parse (dumps ({"tool" : {"twyn" : {"allowlist" : ["mypackage" ]}}}))
128145
129146 config = ConfigHandler (FileHandler ("some-file" ))
@@ -135,19 +152,17 @@ def test_allowlist_add_duplicate_error(self, mock_toml, mock_write_toml):
135152
136153 assert not mock_write_toml .called
137154
138- @patch ("twyn.config.config_handler.ConfigHandler._write_toml" )
139155 @patch ("twyn.config.config_handler.ConfigHandler._read_toml" )
140- def test_allowlist_remove_completely (self , mock_toml , mock_write_toml ) :
156+ def test_allowlist_remove_completely (self , mock_toml : Mock ) -> None :
141157 mock_toml .return_value = parse (dumps ({"tool" : {"twyn" : {"allowlist" : ["mypackage" ]}}}))
142158
143159 config = ConfigHandler (FileHandler ("some-file" ))
144160
145161 config .remove_package_from_allowlist ("mypackage" )
146162 assert config ._read_toml () == {"tool" : {"twyn" : {}}}
147163
148- @patch ("twyn.config.config_handler.ConfigHandler._write_toml" )
149164 @patch ("twyn.config.config_handler.ConfigHandler._read_toml" )
150- def test_allowlist_remove (self , mock_toml , mock_write_toml ) :
165+ def test_allowlist_remove (self , mock_toml : Mock ) -> None :
151166 mock_toml .return_value = parse (dumps ({"tool" : {"twyn" : {"allowlist" : ["mypackage" , "another-package" ]}}}))
152167
153168 config = ConfigHandler (FileHandler ("some-file" ))
@@ -157,7 +172,7 @@ def test_allowlist_remove(self, mock_toml, mock_write_toml):
157172
158173 @patch ("twyn.config.config_handler.ConfigHandler._write_toml" )
159174 @patch ("twyn.config.config_handler.ConfigHandler._read_toml" )
160- def test_allowlist_remove_non_existent_package_error (self , mock_toml , mock_write_toml ) :
175+ def test_allowlist_remove_non_existent_package_error (self , mock_toml : Mock , mock_write_toml : Mock ) -> None :
161176 mock_toml .return_value = parse (dumps ({"tool" : {"twyn" : {"allowlist" : ["mypackage" ]}}}))
162177
163178 config = ConfigHandler (FileHandler ("some-file" ))
@@ -170,7 +185,7 @@ def test_allowlist_remove_non_existent_package_error(self, mock_toml, mock_write
170185 assert not mock_write_toml .called
171186
172187 @pytest .mark .parametrize ("valid_selector" , ["first-letter" , "nearby-letter" , "all" ])
173- def test_valid_selector_methods_accepted (self , valid_selector : str , tmp_path : Path ):
188+ def test_valid_selector_methods_accepted (self , valid_selector : str , tmp_path : Path ) -> None :
174189 """Test that all valid selector methods are accepted."""
175190 pyproject_toml = tmp_path / "pyproject.toml"
176191 pyproject_toml .write_text ("" )
@@ -180,7 +195,7 @@ def test_valid_selector_methods_accepted(self, valid_selector: str, tmp_path: Pa
180195 resolved_config = config .resolve_config (selector_method = valid_selector )
181196 assert resolved_config .selector_method == valid_selector
182197
183- def test_invalid_selector_method_rejected (self , tmp_path : Path ):
198+ def test_invalid_selector_method_rejected (self , tmp_path : Path ) -> None :
184199 """Test that invalid selector methods are rejected with appropriate error."""
185200 pyproject_toml = tmp_path / "pyproject.toml"
186201 pyproject_toml .write_text ("" )
@@ -193,7 +208,7 @@ def test_invalid_selector_method_rejected(self, tmp_path: Path):
193208 assert "Invalid selector_method 'random-selector'" in error_message
194209 assert "Must be one of: all, first-letter, nearby-letter" in error_message
195210
196- def test_invalid_selector_method_from_config_file (self , tmp_path : Path ):
211+ def test_invalid_selector_method_from_config_file (self , tmp_path : Path ) -> None :
197212 """Test that invalid selector method from config file is rejected."""
198213 # Create a config file with invalid selector method
199214 pyproject_toml = tmp_path / "pyproject.toml"
0 commit comments