(root)/
Python-3.12.0/
Lib/
test/
test_importlib/
resources/
test_files.py
       1  import typing
       2  import textwrap
       3  import unittest
       4  import warnings
       5  import importlib
       6  import contextlib
       7  
       8  from importlib import resources
       9  from importlib.resources.abc import Traversable
      10  from . import data01
      11  from . import util
      12  from . import _path
      13  from test.support import os_helper
      14  from test.support import import_helper
      15  
      16  
      17  @contextlib.contextmanager
      18  def suppress_known_deprecation():
      19      with warnings.catch_warnings(record=True) as ctx:
      20          warnings.simplefilter('default', category=DeprecationWarning)
      21          yield ctx
      22  
      23  
      24  class ESC[4;38;5;81mFilesTests:
      25      def test_read_bytes(self):
      26          files = resources.files(self.data)
      27          actual = files.joinpath('utf-8.file').read_bytes()
      28          assert actual == b'Hello, UTF-8 world!\n'
      29  
      30      def test_read_text(self):
      31          files = resources.files(self.data)
      32          actual = files.joinpath('utf-8.file').read_text(encoding='utf-8')
      33          assert actual == 'Hello, UTF-8 world!\n'
      34  
      35      @unittest.skipUnless(
      36          hasattr(typing, 'runtime_checkable'),
      37          "Only suitable when typing supports runtime_checkable",
      38      )
      39      def test_traversable(self):
      40          assert isinstance(resources.files(self.data), Traversable)
      41  
      42      def test_old_parameter(self):
      43          """
      44          Files used to take a 'package' parameter. Make sure anyone
      45          passing by name is still supported.
      46          """
      47          with suppress_known_deprecation():
      48              resources.files(package=self.data)
      49  
      50  
      51  class ESC[4;38;5;81mOpenDiskTests(ESC[4;38;5;149mFilesTests, ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
      52      def setUp(self):
      53          self.data = data01
      54  
      55  
      56  class ESC[4;38;5;81mOpenZipTests(ESC[4;38;5;149mFilesTests, 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):
      57      pass
      58  
      59  
      60  class ESC[4;38;5;81mOpenNamespaceTests(ESC[4;38;5;149mFilesTests, ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
      61      def setUp(self):
      62          from . import namespacedata01
      63  
      64          self.data = namespacedata01
      65  
      66  
      67  class ESC[4;38;5;81mSiteDir:
      68      def setUp(self):
      69          self.fixtures = contextlib.ExitStack()
      70          self.addCleanup(self.fixtures.close)
      71          self.site_dir = self.fixtures.enter_context(os_helper.temp_dir())
      72          self.fixtures.enter_context(import_helper.DirsOnSysPath(self.site_dir))
      73          self.fixtures.enter_context(import_helper.CleanImport())
      74  
      75  
      76  class ESC[4;38;5;81mModulesFilesTests(ESC[4;38;5;149mSiteDir, ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
      77      def test_module_resources(self):
      78          """
      79          A module can have resources found adjacent to the module.
      80          """
      81          spec = {
      82              'mod.py': '',
      83              'res.txt': 'resources are the best',
      84          }
      85          _path.build(spec, self.site_dir)
      86          import mod
      87  
      88          actual = resources.files(mod).joinpath('res.txt').read_text(encoding='utf-8')
      89          assert actual == spec['res.txt']
      90  
      91  
      92  class ESC[4;38;5;81mImplicitContextFilesTests(ESC[4;38;5;149mSiteDir, ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
      93      def test_implicit_files(self):
      94          """
      95          Without any parameter, files() will infer the location as the caller.
      96          """
      97          spec = {
      98              'somepkg': {
      99                  '__init__.py': textwrap.dedent(
     100                      """
     101                      import importlib.resources as res
     102                      val = res.files().joinpath('res.txt').read_text(encoding='utf-8')
     103                      """
     104                  ),
     105                  'res.txt': 'resources are the best',
     106              },
     107          }
     108          _path.build(spec, self.site_dir)
     109          assert importlib.import_module('somepkg').val == 'resources are the best'
     110  
     111  
     112  if __name__ == '__main__':
     113      unittest.main()