test_compatibilty_files.py 3.2 KB

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