(root)/
Python-3.11.7/
Lib/
test/
test_importlib/
test_open.py
       1  import unittest
       2  
       3  from importlib import resources
       4  from . import data01
       5  from .resources import util
       6  
       7  
       8  class ESC[4;38;5;81mCommonBinaryTests(ESC[4;38;5;149mutilESC[4;38;5;149m.ESC[4;38;5;149mCommonTests, ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
       9      def execute(self, package, path):
      10          target = resources.files(package).joinpath(path)
      11          with target.open('rb'):
      12              pass
      13  
      14  
      15  class ESC[4;38;5;81mCommonTextTests(ESC[4;38;5;149mutilESC[4;38;5;149m.ESC[4;38;5;149mCommonTests, ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
      16      def execute(self, package, path):
      17          target = resources.files(package).joinpath(path)
      18          with target.open():
      19              pass
      20  
      21  
      22  class ESC[4;38;5;81mOpenTests:
      23      def test_open_binary(self):
      24          target = resources.files(self.data) / 'binary.file'
      25          with target.open('rb') as fp:
      26              result = fp.read()
      27              self.assertEqual(result, b'\x00\x01\x02\x03')
      28  
      29      def test_open_text_default_encoding(self):
      30          target = resources.files(self.data) / 'utf-8.file'
      31          with target.open() as fp:
      32              result = fp.read()
      33              self.assertEqual(result, 'Hello, UTF-8 world!\n')
      34  
      35      def test_open_text_given_encoding(self):
      36          target = resources.files(self.data) / 'utf-16.file'
      37          with target.open(encoding='utf-16', errors='strict') as fp:
      38              result = fp.read()
      39          self.assertEqual(result, 'Hello, UTF-16 world!\n')
      40  
      41      def test_open_text_with_errors(self):
      42          # Raises UnicodeError without the 'errors' argument.
      43          target = resources.files(self.data) / 'utf-16.file'
      44          with target.open(encoding='utf-8', errors='strict') as fp:
      45              self.assertRaises(UnicodeError, fp.read)
      46          with target.open(encoding='utf-8', errors='ignore') as fp:
      47              result = fp.read()
      48          self.assertEqual(
      49              result,
      50              'H\x00e\x00l\x00l\x00o\x00,\x00 '
      51              '\x00U\x00T\x00F\x00-\x001\x006\x00 '
      52              '\x00w\x00o\x00r\x00l\x00d\x00!\x00\n\x00',
      53          )
      54  
      55      def test_open_binary_FileNotFoundError(self):
      56          target = resources.files(self.data) / 'does-not-exist'
      57          self.assertRaises(FileNotFoundError, target.open, 'rb')
      58  
      59      def test_open_text_FileNotFoundError(self):
      60          target = resources.files(self.data) / 'does-not-exist'
      61          self.assertRaises(FileNotFoundError, target.open)
      62  
      63  
      64  class ESC[4;38;5;81mOpenDiskTests(ESC[4;38;5;149mOpenTests, ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
      65      def setUp(self):
      66          self.data = data01
      67  
      68  
      69  class ESC[4;38;5;81mOpenDiskNamespaceTests(ESC[4;38;5;149mOpenTests, ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
      70      def setUp(self):
      71          from . import namespacedata01
      72  
      73          self.data = namespacedata01
      74  
      75  
      76  class ESC[4;38;5;81mOpenZipTests(ESC[4;38;5;149mOpenTests, ESC[4;38;5;149mutilESC[4;38;5;149m.ESC[4;38;5;149mZipSetup, ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
      77      pass
      78  
      79  
      80  if __name__ == '__main__':
      81      unittest.main()