• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import io
2import unittest
3
4from importlib import resources
5
6from importlib.resources._adapters import (
7    CompatibilityFiles,
8    wrap_spec,
9)
10
11from .resources import util
12
13
14class CompatibilityFilesTests(unittest.TestCase):
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(), 'Hello, world!')
68
69    def test_child_path_open(self):
70        self.assertEqual((self.files / 'a').read_bytes(), b'Hello, world!')
71        self.assertEqual((self.files / 'a').read_text(), 'Hello, world!')
72
73    def test_orphan_path_open(self):
74        with self.assertRaises(FileNotFoundError):
75            (self.files / 'a' / 'b').read_bytes()
76        with self.assertRaises(FileNotFoundError):
77            (self.files / 'a' / 'b' / 'c').read_bytes()
78
79    def test_open_invalid_mode(self):
80        with self.assertRaises(ValueError):
81            self.files.open('0')
82
83    def test_orphan_path_invalid(self):
84        with self.assertRaises(ValueError):
85            CompatibilityFiles.OrphanPath()
86
87    def test_wrap_spec(self):
88        spec = wrap_spec(self.package)
89        self.assertIsInstance(spec.loader.get_resource_reader(None), CompatibilityFiles)
90
91
92class CompatibilityFilesNoReaderTests(unittest.TestCase):
93    @property
94    def package(self):
95        return util.create_package_from_loader(None)
96
97    @property
98    def files(self):
99        return resources.files(self.package)
100
101    def test_spec_path_joinpath(self):
102        self.assertIsInstance(self.files / 'a', CompatibilityFiles.OrphanPath)
103