|
1 | 1 | import io
|
| 2 | +import os |
| 3 | +import pathlib |
2 | 4 | import unittest
|
3 | 5 |
|
4 | 6 | import importlib_resources as resources
|
| 7 | +from importlib_resources.abc import Traversable, TraversableResources |
5 | 8 | from . import data01
|
6 | 9 | from . import util
|
7 | 10 |
|
@@ -57,5 +60,42 @@ def test_remove_in_context_manager(self):
|
57 | 60 | path.unlink()
|
58 | 61 |
|
59 | 62 |
|
| 63 | +class PathLikeTests(PathTests, unittest.TestCase): |
| 64 | + class PathLikeTraversable: |
| 65 | + """pathlib.Path proxy, is os.PathLike but is not pathlib.Path""" |
| 66 | + |
| 67 | + def __init__(self, *args, **kwargs): |
| 68 | + self._path = pathlib.Path(*args, **kwargs) |
| 69 | + |
| 70 | + def __fspath__(self): |
| 71 | + return os.fspath(self._path) |
| 72 | + |
| 73 | + def joinpath(self, other): |
| 74 | + return self.__class__(self, other) |
| 75 | + |
| 76 | + __truediv__ = joinpath |
| 77 | + |
| 78 | + @property |
| 79 | + def parent(self): |
| 80 | + return self.__class__(self._path.parent) |
| 81 | + |
| 82 | + def __getattr__(self, name): |
| 83 | + return getattr(self._path, name) |
| 84 | + |
| 85 | + class PathLikeResources(TraversableResources): |
| 86 | + def __init__(self, loader): |
| 87 | + self.path = PathLikeTests.PathLikeTraversable(loader.path).parent |
| 88 | + |
| 89 | + def get_resource_reader(self, package): |
| 90 | + return self |
| 91 | + |
| 92 | + def files(self): |
| 93 | + return self.path |
| 94 | + |
| 95 | + def setUp(self): |
| 96 | + reader = self.PathLikeResources(data01.__loader__) |
| 97 | + self.data = util.create_package_from_loader(reader) |
| 98 | + |
| 99 | + |
60 | 100 | if __name__ == '__main__':
|
61 | 101 | unittest.main()
|
0 commit comments