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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
import os
import sys
import urllib
if sys.version_info[0] < 3:
import urllib2
else:
import urllib.request as urllib2
import pytest
from sdl2.ext import resources
class TestSDL2ExtResources(object):
__tags__ = ["sdl2ext"]
def test_open_zipfile(self):
fpath = os.path.join(os.path.dirname(__file__), "resources")
zfile = os.path.join(fpath, "resources.zip")
# resources.zip is a packed version of resources/, which at
# least contains
#
# resources/rwopstest.txt
# resources/surfacetest.bmp
resfile = resources.open_zipfile(zfile, "rwopstest.txt", "resources")
assert resfile is not None
resfile = resources.open_zipfile(zfile, "resources/rwopstest.txt")
assert resfile is not None
with pytest.raises(KeyError):
resources.open_zipfile(zfile, "invalid")
with pytest.raises(KeyError):
resources.open_zipfile(zfile, None)
with pytest.raises(KeyError):
resources.open_zipfile(zfile,
"rwopstest.txt", "data")
with pytest.raises(KeyError):
resources.open_zipfile(zfile,
"rwopstest.txt", 1234)
with pytest.raises(KeyError):
resources.open_zipfile(zfile,
None, None)
with pytest.raises(TypeError):
resources.open_zipfile(None,
"rwopstest.txt")
with pytest.raises(TypeError):
resources.open_zipfile(None, None)
with pytest.raises(TypeError):
resources.open_zipfile(None,
"rwopstest.txt", "resources")
def test_open_tarfile(self):
fpath = os.path.join(os.path.dirname(__file__), "resources")
tfile = os.path.join(fpath, "resources.tar.gz")
# resources.tar.gz is a packed version of resources/, which at
# least contains
#
# resources/rwopstest.txt
# resources/surfacetest.bmp
resfile = resources.open_tarfile(tfile, "rwopstest.txt", "resources")
assert resfile is not None
resfile = resources.open_tarfile(tfile, "resources/rwopstest.txt")
assert resfile is not None
# TODO: refine the error handling in open_tarfile()
with pytest.raises(KeyError):
resources.open_tarfile(tfile, "invalid")
with pytest.raises(AttributeError):
resources.open_tarfile(tfile, None)
with pytest.raises(KeyError):
resources.open_tarfile(tfile,
"rwopstest.txt", "data")
with pytest.raises(KeyError):
resources.open_tarfile(tfile,
"rwopstest.txt", 1234)
with pytest.raises(AttributeError):
resources.open_tarfile(tfile,
None, None)
with pytest.raises(ValueError):
resources.open_tarfile(None,
"rwopstest.txt")
with pytest.raises(ValueError):
resources.open_tarfile(None, None)
with pytest.raises(ValueError):
resources.open_tarfile(None,
"rwopstest.txt", "resources")
def test_open_url(self):
if sys.version_info[0] < 3:
p2url = urllib.pathname2url
else:
p2url = urllib2.pathname2url
fpath = os.path.join(os.path.dirname(__file__), "resources")
fpath = os.path.abspath(fpath)
tfile = os.path.join(fpath, "rwopstest.txt")
urlpath = "file:%s" % p2url(tfile)
resfile = resources.open_url(urlpath)
assert resfile is not None
tfile = os.path.join(fpath, "invalid")
urlpath = "file:%s" % p2url(tfile)
with pytest.raises(urllib2.URLError):
resources.open_url(urlpath)
def test_Resources(self):
with pytest.raises(ValueError):
resources.Resources("invalid")
res = resources.Resources()
assert isinstance(res, resources.Resources)
with pytest.raises(KeyError):
res.get("surfacetest.bmp")
fpath = os.path.join(os.path.dirname(__file__), "resources")
res = resources.Resources(fpath)
assert res.get("rwopstest.txt") is not None
assert res.get("surfacetest.bmp") is not None
res2 = resources.Resources(__file__)
assert res2.get("rwopstest.txt") is not None
assert res2.get("surfacetest.bmp") is not None
res3 = resources.Resources(__file__, "resources")
assert res3.get("rwopstest.txt") is not None
assert res3.get("surfacetest.bmp") is not None
def test_Resources_add(self):
fpath = os.path.join(os.path.dirname(__file__), "resources")
sfile = os.path.join(fpath, "surfacetest.bmp")
zfile = os.path.join(fpath, "resources.zip")
res = resources.Resources()
res.add(sfile)
with pytest.raises(KeyError):
res.get("rwopstest.txt")
assert res.get("surfacetest.bmp") is not None
res.add(zfile)
assert res.get("rwopstest.txt") is not None
assert res.get("surfacetest.bmp") is not None
with pytest.raises(TypeError):
res.add(None)
with pytest.raises(ValueError):
res.add("invalid_name.txt")
def test_Resources_add_file(self):
fpath = os.path.join(os.path.dirname(__file__), "resources")
sfile = os.path.join(fpath, "surfacetest.bmp")
zfile = os.path.join(fpath, "resources.zip")
res = resources.Resources()
res.add_file(sfile)
res.add_file(zfile)
with pytest.raises(KeyError):
res.get("rwopstest.txt")
assert res.get("surfacetest.bmp") is not None
assert res.get("resources.zip") is not None
with pytest.raises(TypeError):
res.add_file(None)
with pytest.raises(ValueError):
res.add_file("invalid_name.txt")
def test_Resources_add_archive(self):
fpath = os.path.join(os.path.dirname(__file__), "resources")
zfile = os.path.join(fpath, "resources.zip")
tfile = os.path.join(fpath, "resources.tar.gz")
res = resources.Resources()
res.add_archive(zfile)
assert res.get("surfacetest.bmp") is not None
assert res.get("rwopstest.txt") is not None
with pytest.raises(KeyError):
res.get("resources.zip")
with pytest.raises(TypeError):
res.add_archive(None)
with pytest.raises(ValueError):
res.add_archive("invalid_name.txt")
res = resources.Resources()
res.add_archive(tfile, typehint="targz")
assert res.get("surfacetest.bmp") is not None
assert res.get("rwopstest.txt") is not None
with pytest.raises(KeyError):
res.get("resources.tar.gz")
def test_Resources_get(self):
fpath = os.path.join(os.path.dirname(__file__), "resources")
for path in (fpath, None):
res = resources.Resources(path)
with pytest.raises(KeyError):
res.get("invalid_file.txt")
with pytest.raises(KeyError):
res.get(None)
with pytest.raises(KeyError):
res.get(123456)
if path is None:
with pytest.raises(KeyError):
res.get("surfacetest.bmp")
with pytest.raises(KeyError):
res.get("rwopstest.txt")
else:
assert res.get("surfacetest.bmp") is not None
assert res.get("rwopstest.txt") is not None
def test_Resources_get_filelike(self):
fpath = os.path.join(os.path.dirname(__file__), "resources")
zfile = os.path.join(fpath, "resources.zip")
pfile = os.path.join(fpath, "rwopstest.txt")
res = resources.Resources()
res.add(zfile)
v1 = res.get_filelike("rwopstest.txt")
v2 = res.get_filelike("surfacetest.bmp")
assert type(v1) == type(v2)
res.add(pfile)
v1 = res.get_filelike("rwopstest.txt")
v2 = res.get_filelike("surfacetest.bmp")
assert type(v1) != type(v2)
with pytest.raises(KeyError):
res.get_filelike(None)
with pytest.raises(KeyError):
res.get_filelike("invalid")
with pytest.raises(KeyError):
res.get_filelike(1234)
def test_Resources_get_path(self):
fpath = os.path.join(os.path.dirname(__file__), "resources")
zfile = os.path.join(fpath, "resources.zip")
pfile = os.path.join(fpath, "rwopstest.txt")
res = resources.Resources()
res.add(zfile)
res.add(pfile)
zpath = res.get_path("surfacetest.bmp")
assert zpath.find("surfacetest.bmp@") != -1
assert zpath != zfile
ppath = res.get_path("rwopstest.txt")
assert ppath.find("rwopstest.txt") != -1
with pytest.raises(KeyError):
res.get_path(None)
with pytest.raises(KeyError):
res.get_path("invalid")
with pytest.raises(KeyError):
res.get_path(1234)
def test_Resources_scan(self):
fpath = os.path.join(os.path.dirname(__file__))
res = resources.Resources()
res.scan(fpath)
assert res.get("rwopstest.txt") is not None
assert res.get("surfacetest.bmp") is not None
with pytest.raises(ValueError):
res.scan("invalid")
with pytest.raises(ValueError):
res.scan(fpath, "invalid")
with pytest.raises(Exception):
res.scan(12345)
res = resources.Resources()
res.scan(fpath, "resources")
assert res.get("rwopstest.txt") is not None
assert res.get("surfacetest.bmp") is not None
|