summaryrefslogtreecommitdiffstats
path: root/library/python/runtime_py3/test
diff options
context:
space:
mode:
authorshadchin <[email protected]>2026-01-20 07:14:57 +0300
committershadchin <[email protected]>2026-01-20 07:32:54 +0300
commitceac195a892eec61403ec3d2f3a4e7f4550c90e2 (patch)
treed30c6cda1f39bf50c7aab62426b9691cefef551b /library/python/runtime_py3/test
parentbd2b219f7ac04bfa6912239ed6b89c287e8cd2d8 (diff)
Rework our machinery for `importlib.resources`
commit_hash:952ba013b771d9c6cb949cf43125956ad5cdfd58
Diffstat (limited to 'library/python/runtime_py3/test')
-rw-r--r--library/python/runtime_py3/test/resources/data/my_data1
-rw-r--r--library/python/runtime_py3/test/test_resources.py55
-rw-r--r--library/python/runtime_py3/test/ya.make5
3 files changed, 53 insertions, 8 deletions
diff --git a/library/python/runtime_py3/test/resources/data/my_data b/library/python/runtime_py3/test/resources/data/my_data
new file mode 100644
index 00000000000..6320cd248dd
--- /dev/null
+++ b/library/python/runtime_py3/test/resources/data/my_data
@@ -0,0 +1 @@
+data \ No newline at end of file
diff --git a/library/python/runtime_py3/test/test_resources.py b/library/python/runtime_py3/test/test_resources.py
index 0bf70f8c54b..fda82527886 100644
--- a/library/python/runtime_py3/test/test_resources.py
+++ b/library/python/runtime_py3/test/test_resources.py
@@ -1,7 +1,12 @@
import importlib.resources as ir
+from importlib.resources._common import get_resource_reader
+from importlib.resources.abc import TraversalError
+
import pytest
+from sitecustomize import ResfsTraversableResources
+
@pytest.mark.parametrize(
"package, resource",
@@ -25,9 +30,16 @@ def test_is_resource_missing(package, resource):
assert not ir.is_resource(package, resource)
-def test_is_resource_subresource_directory():
+ "directory",
+ (
+ "data",
+ "submodule",
+ ),
+)
+def test_is_resource_subresource_directory(directory):
# Directories are not resources.
- assert not ir.is_resource("resources", "submodule")
+ assert not ir.is_resource("resources", directory)
@pytest.mark.parametrize(
@@ -42,7 +54,7 @@ def test_read_binary_good_path(package, resource, expected):
def test_read_binary_missing():
- with pytest.raises(FileNotFoundError):
+ with pytest.raises(TraversalError):
ir.read_binary("resources", "111.txt")
@@ -58,14 +70,14 @@ def test_read_text_good_path(package, resource, expected):
def test_read_text_missing():
- with pytest.raises(FileNotFoundError):
+ with pytest.raises(TraversalError):
ir.read_text("resources", "111.txt")
@pytest.mark.parametrize(
"package, expected",
(
- ("resources", ["submodule", "foo.txt"]),
+ ("resources", ["data", "submodule", "foo.txt"]),
("resources.submodule", ["bar.txt"]),
),
)
@@ -76,6 +88,7 @@ def test_contents_good_path(package, expected):
def test_files_joinpath():
assert ir.files("resources") / "submodule"
assert ir.files("resources") / "foo.txt"
+ assert ir.files("resources") / "data" / "my_data"
assert ir.files("resources") / "submodule" / "bar.txt"
assert ir.files("resources.submodule") / "bar.txt"
@@ -84,6 +97,8 @@ def test_files_joinpath():
"package, resource, expected",
(
("resources", "foo.txt", b"bar"),
+ ("resources", "data/my_data", b"data"),
+ ("resources", "submodule/bar.txt", b"foo"),
("resources.submodule", "bar.txt", b"foo"),
),
)
@@ -95,6 +110,8 @@ def test_files_read_bytes(package, resource, expected):
"package, resource, expected",
(
("resources", "foo.txt", "bar"),
+ ("resources", "data/my_data", "data"),
+ ("resources", "submodule/bar.txt", "foo"),
("resources.submodule", "bar.txt", "foo"),
),
)
@@ -105,7 +122,7 @@ def test_files_read_text(package, resource, expected):
@pytest.mark.parametrize(
"package, expected",
(
- ("resources", ("foo.txt", "submodule")),
+ ("resources", ("foo.txt", "data", "submodule")),
("resources.submodule", ("bar.txt",)),
),
)
@@ -116,9 +133,33 @@ def test_files_iterdir(package, expected):
@pytest.mark.parametrize(
"package, expected",
(
- ("resources", ("foo.txt", "submodule")),
+ ("resources", ("data", "foo.txt", "submodule")),
("resources.submodule", ("bar.txt",)),
),
)
def test_files_iterdir_with_sort(package, expected):
assert tuple(resource.name for resource in sorted(ir.files(package).iterdir())) == expected
+
+
+def test_get_resource_reader():
+ import resources
+
+ reader = get_resource_reader(resources)
+ assert isinstance(reader, ResfsTraversableResources)
+
+ assert reader.is_resource("foo.txt") is True
+ assert reader.is_resource("submodule/bar.txt") is True
+ assert reader.is_resource("notfound.txt") is False
+ assert reader.is_resource("submodule") is False
+
+ with pytest.raises(FileNotFoundError) as ex:
+ reader.resource_path("foo.txt")
+ assert str(ex.value) == "foo.txt"
+
+ with reader.open_resource("foo.txt") as f:
+ assert f.read() == b"bar"
+ with pytest.raises(FileNotFoundError) as ex:
+ reader.open_resource("notfound.txt")
+ assert str(ex.value) == "resfs/file/library/python/runtime_py3/test/resources/notfound.txt"
+
+ assert tuple(reader.contents()) == ("foo.txt", "data", "submodule")
diff --git a/library/python/runtime_py3/test/ya.make b/library/python/runtime_py3/test/ya.make
index fde64236dca..7e466d0a708 100644
--- a/library/python/runtime_py3/test/ya.make
+++ b/library/python/runtime_py3/test/ya.make
@@ -2,7 +2,9 @@ PY3TEST()
STYLE_PYTHON()
-DEPENDS(library/python/runtime_py3/test/traceback)
+DEPENDS(
+ library/python/runtime_py3/test/traceback
+)
PEERDIR(
contrib/python/parameterized
@@ -29,6 +31,7 @@ RESOURCE_FILES(
.dist-info/entry_points.txt
.dist-info/top_level.txt
resources/foo.txt
+ resources/data/my_data
resources/submodule/bar.txt
)