summaryrefslogtreecommitdiffstats
path: root/library/python/runtime_py3/test/test_resources.py
blob: fda82527886ea1833ad21f54d5ac43ebce40a82c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
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",
    (
        ("resources", "foo.txt"),
        ("resources.submodule", "bar.txt"),
    ),
)
def test_is_resource_good_path(package, resource):
    assert ir.is_resource(package, resource)


@pytest.mark.parametrize(
    "package, resource",
    (
        ("resources", "111.txt"),
        ("resources.submodule", "222.txt"),
    ),
)
def test_is_resource_missing(package, resource):
    assert not ir.is_resource(package, resource)


@pytest.mark.parametrize(
    "directory",
    (
        "data",
        "submodule",
    ),
)
def test_is_resource_subresource_directory(directory):
    # Directories are not resources.
    assert not ir.is_resource("resources", directory)


@pytest.mark.parametrize(
    "package, resource, expected",
    (
        ("resources", "foo.txt", b"bar"),
        ("resources.submodule", "bar.txt", b"foo"),
    ),
)
def test_read_binary_good_path(package, resource, expected):
    assert ir.read_binary(package, resource) == expected


def test_read_binary_missing():
    with pytest.raises(TraversalError):
        ir.read_binary("resources", "111.txt")


@pytest.mark.parametrize(
    "package, resource, expected",
    (
        ("resources", "foo.txt", "bar"),
        ("resources.submodule", "bar.txt", "foo"),
    ),
)
def test_read_text_good_path(package, resource, expected):
    assert ir.read_text(package, resource) == expected


def test_read_text_missing():
    with pytest.raises(TraversalError):
        ir.read_text("resources", "111.txt")


@pytest.mark.parametrize(
    "package, expected",
    (
        ("resources", ["data", "submodule", "foo.txt"]),
        ("resources.submodule", ["bar.txt"]),
    ),
)
def test_contents_good_path(package, expected):
    assert sorted(ir.contents(package)) == sorted(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"


@pytest.mark.parametrize(
    "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"),
    ),
)
def test_files_read_bytes(package, resource, expected):
    assert (ir.files(package) / resource).read_bytes() == expected


@pytest.mark.parametrize(
    "package, resource, expected",
    (
        ("resources", "foo.txt", "bar"),
        ("resources", "data/my_data", "data"),
        ("resources", "submodule/bar.txt", "foo"),
        ("resources.submodule", "bar.txt", "foo"),
    ),
)
def test_files_read_text(package, resource, expected):
    assert (ir.files(package) / resource).read_text() == expected


@pytest.mark.parametrize(
    "package, expected",
    (
        ("resources", ("foo.txt", "data", "submodule")),
        ("resources.submodule", ("bar.txt",)),
    ),
)
def test_files_iterdir(package, expected):
    assert tuple(resource.name for resource in ir.files(package).iterdir()) == expected


@pytest.mark.parametrize(
    "package, expected",
    (
        ("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")