Pillow/Tests/test_util.py
Will Badart adae7ecc6a
_util.isPath returns True for pathlib.Path objects
Now, for functions which accept either a path or file object, the
predicate will pass on Paths and not attempt to call .read on them
before opening.

The pathlib module was added in 3.4 but os.path functions did not start
accepting path-like objects until 3.6, so that is the version after
which this implementation is defined.

Added a unit test to make sure isPath accepts Path objects. The unit
test is skipped if python version is not 3.6 or later.
2019-01-28 12:19:21 -05:00

97 lines
1.9 KiB
Python

import sys
from helper import unittest, PillowTestCase
from PIL import _util
py36 = sys.version_info.major >= 3 and sys.version_info.minor >= 6
class TestUtil(PillowTestCase):
def test_is_string_type(self):
# Arrange
color = "red"
# Act
it_is = _util.isStringType(color)
# Assert
self.assertTrue(it_is)
def test_is_not_string_type(self):
# Arrange
color = (255, 0, 0)
# Act
it_is_not = _util.isStringType(color)
# Assert
self.assertFalse(it_is_not)
def test_is_path(self):
# Arrange
fp = "filename.ext"
# Act
it_is = _util.isStringType(fp)
# Assert
self.assertTrue(it_is)
@unittest.skipIf(not py36, 'os.path support for Paths added in 3.6')
def test_path_obj_is_path(self):
# Arrange
from pathlib import Path
fp = Path('filename.ext')
# Act
it_is = _util.isPath(fp)
# Assert
self.assertTrue(it_is)
def test_is_not_path(self):
# Arrange
filename = self.tempfile("temp.ext")
fp = open(filename, 'w').close()
# Act
it_is_not = _util.isPath(fp)
# Assert
self.assertFalse(it_is_not)
def test_is_directory(self):
# Arrange
directory = "Tests"
# Act
it_is = _util.isDirectory(directory)
# Assert
self.assertTrue(it_is)
def test_is_not_directory(self):
# Arrange
text = "abc"
# Act
it_is_not = _util.isDirectory(text)
# Assert
self.assertFalse(it_is_not)
def test_deferred_error(self):
# Arrange
# Act
thing = _util.deferred_error(ValueError("Some error text"))
# Assert
self.assertRaises(ValueError, lambda: thing.some_attr)
if __name__ == '__main__':
unittest.main()