forked from python/cpython
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request python#9 from brettcannon/implement-read
Implement read()
- Loading branch information
Showing
5 changed files
with
90 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
Hello, world! | ||
Hello, UTF-8 world! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import io | ||
import os.path | ||
import pathlib | ||
import sys | ||
import unittest | ||
|
||
import importlib_resources as resources | ||
from importlib_resources.tests import data | ||
|
||
|
||
class CommonTests(unittest.TestCase): | ||
|
||
def test_package_name(self): | ||
# Passing in the package name should succeed. | ||
resources.read(data.__name__, 'utf-8.file') | ||
|
||
def test_package_object(self): | ||
# Passing in the package itself should succeed. | ||
resources.read(data, 'utf-8.file') | ||
|
||
def test_string_path(self): | ||
path = 'utf-8.file' | ||
# Passing in a string for the path should succeed. | ||
resources.read(data, path) | ||
|
||
@unittest.skipIf(sys.version_info < (3, 6), 'requires os.PathLike support') | ||
def test_pathlib_path(self): | ||
# Passing in a pathlib.PurePath object for the path should succeed. | ||
path = pathlib.PurePath('utf-8.file') | ||
resources.read(data, path) | ||
|
||
def test_absolute_path(self): | ||
# An absolute path is a ValueError. | ||
path = pathlib.Path(__file__) | ||
full_path = path.parent/'utf-8.file' | ||
with self.assertRaises(ValueError): | ||
resources.read(data, str(full_path)) | ||
|
||
def test_relative_path(self): | ||
# A reative path is a ValueError. | ||
with self.assertRaises(ValueError): | ||
resources.read(data, '../data/utf-8.file') | ||
|
||
def test_importing_module_as_side_effect(self): | ||
# The anchor package can already be imported. | ||
del sys.modules[data.__name__] | ||
resources.read(data.__name__, 'utf-8.file') | ||
|
||
def test_non_package(self): | ||
# The anchor package cannot be a module. | ||
with self.assertRaises(TypeError): | ||
resources.read(__spec__.name, 'utf-8.file') | ||
|
||
|
||
class ReadTests(unittest.TestCase): | ||
|
||
def test_default_encoding(self): | ||
result = resources.read(data, 'utf-8.file') | ||
self.assertEqual("Hello, UTF-8 world!\n", result) | ||
|
||
def test_encoding(self): | ||
result = resources.read(data, 'utf-16.file', encoding='utf-16') | ||
self.assertEqual("Hello, UTF-16 world!\n", result) | ||
|
||
def test_errors(self): | ||
# Raises UnicodeError without the 'errors' argument. | ||
result = resources.read(data, 'utf-16.file', encoding='utf-8', | ||
errors='ignore') |