1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
import sys
from unittest.mock import patch
import pytest
from isort import io
from isort.exceptions import UnsupportedEncoding
class TestFile:
@pytest.mark.skipif(sys.platform == "win32", reason="Can't run file encoding test in AppVeyor")
def test_read(self, tmpdir):
test_file_content = """# -*- encoding: ascii -*-
import Ὡ
"""
test_file = tmpdir.join("file.py")
test_file.write(test_file_content)
with pytest.raises(UnicodeDecodeError):
with io.File.read(str(test_file)) as file_handler:
file_handler.stream.read()
def test_from_content(self, tmpdir):
test_file = tmpdir.join("file.py")
test_file.write_text("import os", "utf8")
file_obj = io.File.from_contents("import os", filename=str(test_file))
assert file_obj
assert file_obj.extension == "py"
def test_open(self, tmpdir):
with pytest.raises(FileNotFoundError):
io.File._open("THISCANTBEAREALFILEὩὩὩὩὩὩὩὩὩὩὩὩ.ὩὩὩὩὩ")
def raise_arbitrary_exception(*args, **kwargs):
raise RuntimeError("test")
test_file = tmpdir.join("file.py")
test_file.write("import os")
assert io.File._open(str(test_file))
# correctly responds to error determining encoding
with patch("tokenize.detect_encoding", raise_arbitrary_exception):
with pytest.raises(UnsupportedEncoding):
io.File._open(str(test_file))
|