Skip to content

Commit 24bcc32

Browse files
committed
Add a test for encoding parameter
1 parent 31bce1d commit 24bcc32

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

tests/test_common.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,32 @@ def ignore_reqs(self, modname):
196196
requirements_filename=str(fake_requirements_file),
197197
)
198198
assert not reqs
199+
200+
201+
def test_find_imported_modules_sets_encoding_to_utf8_when_reading(tmp_path):
202+
(tmp_path / 'module.py').touch()
203+
204+
class options:
205+
paths = [tmp_path]
206+
207+
def ignore_files(*_):
208+
return False
209+
210+
expected_encoding = 'utf-8'
211+
used_encoding = None
212+
213+
original_open = common.__builtins__['open']
214+
215+
def mocked_open(*args, **kwargs):
216+
# As of Python 3.9, the args to open() are as follows:
217+
# file, mode, buffering, encoding, erorrs, newline, closedf, opener
218+
nonlocal used_encoding
219+
if 'encoding' in kwargs:
220+
used_encoding = kwargs['encoding']
221+
return original_open(*args, **kwargs)
222+
223+
common.__builtins__['open'] = mocked_open
224+
common.find_imported_modules(options)
225+
common.__builtins__['open'] = original_open
226+
227+
assert used_encoding == expected_encoding

0 commit comments

Comments
 (0)