Skip to content

Commit

Permalink
tests: add tests for CompatibiltyFiles
Browse files Browse the repository at this point in the history
Signed-off-by: Filipe Laíns <lains@riseup.net>
  • Loading branch information
FFY00 committed May 29, 2021
1 parent 5e8c1d2 commit 97083d2
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 2 deletions.
95 changes: 95 additions & 0 deletions importlib_resources/tests/test_compatibilty_files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import io
import unittest

import importlib_resources as resources

from importlib_resources._adapters import CompatibilityFiles

from . import util


class CompatibilityFilesTests(unittest.TestCase):
@property
def package(self):
bytes_data = io.BytesIO(b'Hello, world!')
return util.create_package(
file=bytes_data,
path='some_path',
contents=('a', 'b', 'c'),
)

@property
def files(self):
return resources.files(self.package)

def test_spec_path_iter(self):
self.assertEqual(
sorted(path.name for path in self.files.iterdir()),
['a', 'b', 'c'],
)

def test_child_path_iter(self):
self.assertEqual(list((self.files / 'a').iterdir()), [])

def test_orphan_path_iter(self):
self.assertEqual(list((self.files / 'a' / 'a').iterdir()), [])
self.assertEqual(list((self.files / 'a' / 'a' / 'a').iterdir()), [])

def test_spec_path_is(self):
self.assertFalse(self.files.is_file())
self.assertFalse(self.files.is_dir())

def test_child_path_is(self):
self.assertTrue((self.files / 'a').is_file())
self.assertFalse((self.files / 'a').is_dir())

def test_orphan_path_is(self):
self.assertFalse((self.files / 'a' / 'a').is_file())
self.assertFalse((self.files / 'a' / 'a').is_dir())
self.assertFalse((self.files / 'a' / 'a' / 'a').is_file())
self.assertFalse((self.files / 'a' / 'a' / 'a').is_dir())

def test_spec_path_name(self):
self.assertEqual(self.files.name, 'testingpackage')

def test_child_path_name(self):
self.assertEqual((self.files / 'a').name, 'a')

def test_orphan_path_name(self):
self.assertEqual((self.files / 'a' / 'b').name, 'b')
self.assertEqual((self.files / 'a' / 'b' / 'c').name, 'c')

def test_spec_path_open(self):
self.assertEqual(self.files.read_bytes(), b'Hello, world!')
self.assertEqual(self.files.read_text(), 'Hello, world!')

def test_child_path_open(self):
self.assertEqual((self.files / 'a').read_bytes(), b'Hello, world!')
self.assertEqual((self.files / 'a').read_text(), 'Hello, world!')

def test_orphan_path_open(self):
with self.assertRaises(FileNotFoundError):
(self.files / 'a' / 'b').read_bytes()
with self.assertRaises(FileNotFoundError):
(self.files / 'a' / 'b' / 'c').read_bytes()

def test_open_invalid_mode(self):
with self.assertRaises(ValueError):
self.files.open('0')

def test_orphan_path_invalid(self):
with self.assertRaises(ValueError):
CompatibilityFiles.OrphanPath()


class CompatibilityFilesNoReaderTests(unittest.TestCase):
@property
def package(self):
return util.create_package_from_loader(None)

@property
def files(self):
return resources.files(self.package)

def test_spec_path_joinpath(self):
self.assertIsInstance(self.files / 'a', CompatibilityFiles.OrphanPath)
10 changes: 8 additions & 2 deletions importlib_resources/tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,22 @@ def contents(self):
yield from self._contents


def create_package(file, path, is_package=True, contents=()):
def create_package_from_loader(loader, is_package=True):
name = 'testingpackage'
module = types.ModuleType(name)
loader = Reader(file=file, path=path, _contents=contents)
spec = ModuleSpec(name, loader, origin='does-not-exist', is_package=is_package)
module.__spec__ = spec
module.__loader__ = loader
return module


def create_package(file=None, path=None, is_package=True, contents=()):
return create_package_from_loader(
Reader(file=file, path=path, _contents=contents),
is_package,
)


class CommonTests(metaclass=abc.ABCMeta):
"""
Tests shared by test_open, test_path, and test_read.
Expand Down

0 comments on commit 97083d2

Please sign in to comment.