1  import io
       2  import unittest
       3  
       4  from importlib import resources
       5  
       6  from importlib.resources._adapters import (
       7      CompatibilityFiles,
       8      wrap_spec,
       9  )
      10  
      11  from . import util
      12  
      13  
      14  class ESC[4;38;5;81mCompatibilityFilesTests(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
      15      @property
      16      def package(self):
      17          bytes_data = io.BytesIO(b'Hello, world!')
      18          return util.create_package(
      19              file=bytes_data,
      20              path='some_path',
      21              contents=('a', 'b', 'c'),
      22          )
      23  
      24      @property
      25      def files(self):
      26          return resources.files(self.package)
      27  
      28      def test_spec_path_iter(self):
      29          self.assertEqual(
      30              sorted(path.name for path in self.files.iterdir()),
      31              ['a', 'b', 'c'],
      32          )
      33  
      34      def test_child_path_iter(self):
      35          self.assertEqual(list((self.files / 'a').iterdir()), [])
      36  
      37      def test_orphan_path_iter(self):
      38          self.assertEqual(list((self.files / 'a' / 'a').iterdir()), [])
      39          self.assertEqual(list((self.files / 'a' / 'a' / 'a').iterdir()), [])
      40  
      41      def test_spec_path_is(self):
      42          self.assertFalse(self.files.is_file())
      43          self.assertFalse(self.files.is_dir())
      44  
      45      def test_child_path_is(self):
      46          self.assertTrue((self.files / 'a').is_file())
      47          self.assertFalse((self.files / 'a').is_dir())
      48  
      49      def test_orphan_path_is(self):
      50          self.assertFalse((self.files / 'a' / 'a').is_file())
      51          self.assertFalse((self.files / 'a' / 'a').is_dir())
      52          self.assertFalse((self.files / 'a' / 'a' / 'a').is_file())
      53          self.assertFalse((self.files / 'a' / 'a' / 'a').is_dir())
      54  
      55      def test_spec_path_name(self):
      56          self.assertEqual(self.files.name, 'testingpackage')
      57  
      58      def test_child_path_name(self):
      59          self.assertEqual((self.files / 'a').name, 'a')
      60  
      61      def test_orphan_path_name(self):
      62          self.assertEqual((self.files / 'a' / 'b').name, 'b')
      63          self.assertEqual((self.files / 'a' / 'b' / 'c').name, 'c')
      64  
      65      def test_spec_path_open(self):
      66          self.assertEqual(self.files.read_bytes(), b'Hello, world!')
      67          self.assertEqual(self.files.read_text(encoding='utf-8'), 'Hello, world!')
      68  
      69      def test_child_path_open(self):
      70          self.assertEqual((self.files / 'a').read_bytes(), b'Hello, world!')
      71          self.assertEqual(
      72              (self.files / 'a').read_text(encoding='utf-8'), 'Hello, world!'
      73          )
      74  
      75      def test_orphan_path_open(self):
      76          with self.assertRaises(FileNotFoundError):
      77              (self.files / 'a' / 'b').read_bytes()
      78          with self.assertRaises(FileNotFoundError):
      79              (self.files / 'a' / 'b' / 'c').read_bytes()
      80  
      81      def test_open_invalid_mode(self):
      82          with self.assertRaises(ValueError):
      83              self.files.open('0')
      84  
      85      def test_orphan_path_invalid(self):
      86          with self.assertRaises(ValueError):
      87              CompatibilityFiles.OrphanPath()
      88  
      89      def test_wrap_spec(self):
      90          spec = wrap_spec(self.package)
      91          self.assertIsInstance(spec.loader.get_resource_reader(None), CompatibilityFiles)
      92  
      93  
      94  class ESC[4;38;5;81mCompatibilityFilesNoReaderTests(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
      95      @property
      96      def package(self):
      97          return util.create_package_from_loader(None)
      98  
      99      @property
     100      def files(self):
     101          return resources.files(self.package)
     102  
     103      def test_spec_path_joinpath(self):
     104          self.assertIsInstance(self.files / 'a', CompatibilityFiles.OrphanPath)