2017-12-30 16:18:06 -04:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
from importlib import resources
|
2022-01-22 22:38:26 -04:00
|
|
|
from . import data01
|
2022-07-24 21:53:10 -03:00
|
|
|
from . import util
|
2017-12-30 16:18:06 -04:00
|
|
|
|
|
|
|
|
2021-07-29 22:05:05 -03:00
|
|
|
class CommonBinaryTests(util.CommonTests, unittest.TestCase):
|
2017-12-30 16:18:06 -04:00
|
|
|
def execute(self, package, path):
|
2021-12-18 22:28:49 -04:00
|
|
|
target = resources.files(package).joinpath(path)
|
|
|
|
with target.open('rb'):
|
|
|
|
pass
|
2017-12-30 16:18:06 -04:00
|
|
|
|
|
|
|
|
2021-07-29 22:05:05 -03:00
|
|
|
class CommonTextTests(util.CommonTests, unittest.TestCase):
|
2017-12-30 16:18:06 -04:00
|
|
|
def execute(self, package, path):
|
2021-12-18 22:28:49 -04:00
|
|
|
target = resources.files(package).joinpath(path)
|
2023-02-18 17:29:22 -04:00
|
|
|
with target.open(encoding='utf-8'):
|
2021-12-18 22:28:49 -04:00
|
|
|
pass
|
2017-12-30 16:18:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
class OpenTests:
|
|
|
|
def test_open_binary(self):
|
2021-12-18 22:28:49 -04:00
|
|
|
target = resources.files(self.data) / 'binary.file'
|
|
|
|
with target.open('rb') as fp:
|
|
|
|
result = fp.read()
|
|
|
|
self.assertEqual(result, b'\x00\x01\x02\x03')
|
2017-12-30 16:18:06 -04:00
|
|
|
|
|
|
|
def test_open_text_default_encoding(self):
|
2021-12-18 22:28:49 -04:00
|
|
|
target = resources.files(self.data) / 'utf-8.file'
|
2023-02-18 17:29:22 -04:00
|
|
|
with target.open(encoding='utf-8') as fp:
|
2021-12-18 22:28:49 -04:00
|
|
|
result = fp.read()
|
2017-12-30 16:18:06 -04:00
|
|
|
self.assertEqual(result, 'Hello, UTF-8 world!\n')
|
|
|
|
|
|
|
|
def test_open_text_given_encoding(self):
|
2021-12-18 22:28:49 -04:00
|
|
|
target = resources.files(self.data) / 'utf-16.file'
|
|
|
|
with target.open(encoding='utf-16', errors='strict') as fp:
|
|
|
|
result = fp.read()
|
2017-12-30 16:18:06 -04:00
|
|
|
self.assertEqual(result, 'Hello, UTF-16 world!\n')
|
|
|
|
|
|
|
|
def test_open_text_with_errors(self):
|
2023-02-18 17:29:22 -04:00
|
|
|
"""
|
|
|
|
Raises UnicodeError without the 'errors' argument.
|
|
|
|
"""
|
2021-12-18 22:28:49 -04:00
|
|
|
target = resources.files(self.data) / 'utf-16.file'
|
|
|
|
with target.open(encoding='utf-8', errors='strict') as fp:
|
|
|
|
self.assertRaises(UnicodeError, fp.read)
|
|
|
|
with target.open(encoding='utf-8', errors='ignore') as fp:
|
|
|
|
result = fp.read()
|
2017-12-30 16:18:06 -04:00
|
|
|
self.assertEqual(
|
|
|
|
result,
|
|
|
|
'H\x00e\x00l\x00l\x00o\x00,\x00 '
|
|
|
|
'\x00U\x00T\x00F\x00-\x001\x006\x00 '
|
2021-03-04 14:43:00 -04:00
|
|
|
'\x00w\x00o\x00r\x00l\x00d\x00!\x00\n\x00',
|
|
|
|
)
|
2017-12-30 16:18:06 -04:00
|
|
|
|
|
|
|
def test_open_binary_FileNotFoundError(self):
|
2021-12-18 22:28:49 -04:00
|
|
|
target = resources.files(self.data) / 'does-not-exist'
|
2023-02-18 17:29:22 -04:00
|
|
|
with self.assertRaises(FileNotFoundError):
|
|
|
|
target.open('rb')
|
2017-12-30 16:18:06 -04:00
|
|
|
|
|
|
|
def test_open_text_FileNotFoundError(self):
|
2021-12-18 22:28:49 -04:00
|
|
|
target = resources.files(self.data) / 'does-not-exist'
|
2023-02-18 17:29:22 -04:00
|
|
|
with self.assertRaises(FileNotFoundError):
|
|
|
|
target.open(encoding='utf-8')
|
2017-12-30 16:18:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
class OpenDiskTests(OpenTests, unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.data = data01
|
|
|
|
|
|
|
|
|
2021-03-04 14:43:00 -04:00
|
|
|
class OpenDiskNamespaceTests(OpenTests, unittest.TestCase):
|
|
|
|
def setUp(self):
|
2022-01-22 22:38:26 -04:00
|
|
|
from . import namespacedata01
|
2021-03-04 14:43:00 -04:00
|
|
|
|
|
|
|
self.data = namespacedata01
|
|
|
|
|
|
|
|
|
2017-12-30 16:18:06 -04:00
|
|
|
class OpenZipTests(OpenTests, util.ZipSetup, unittest.TestCase):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|