feat(urllib.request): add importlib resource handler
This commit is contained in:
parent
24806959bb
commit
59713aefb8
2 changed files with 90 additions and 11 deletions
|
|
@ -1,13 +1,90 @@
|
|||
import os.path
|
||||
import sys
|
||||
import urllib.request
|
||||
|
||||
import pytest
|
||||
|
||||
from byteb4rb1e.utils.testing.pytest.decorators import run_in_subprocess_once
|
||||
from byteb4rb1e.utils.testing.pytest.fixtures import mock_pkg
|
||||
from byteb4rb1e.utils.urllib.request import PkgHandler
|
||||
|
||||
|
||||
class TestPkgHandler:
|
||||
"""
|
||||
"""
|
||||
def test_default(self):
|
||||
@run_in_subprocess_once()
|
||||
def test_text(self, mock_pkg):
|
||||
"""
|
||||
"""
|
||||
pass
|
||||
_opener: urllib.request.OpenerDirector = urllib.request.build_opener(
|
||||
PkgHandler()
|
||||
)
|
||||
|
||||
dummy_data = 'Hello'
|
||||
|
||||
mock_pkg('foobarpkg', {
|
||||
'data.txt': dummy_data
|
||||
})
|
||||
|
||||
result = _opener.open('pkg://foobarpkg/data.txt').readline()
|
||||
|
||||
assert isinstance(result, str)
|
||||
assert result == dummy_data
|
||||
|
||||
|
||||
@run_in_subprocess_once()
|
||||
def test_bytes(self, mock_pkg):
|
||||
"""
|
||||
"""
|
||||
_opener: urllib.request.OpenerDirector = urllib.request.build_opener(
|
||||
PkgHandler()
|
||||
)
|
||||
|
||||
dummy_data = b'foobar123'
|
||||
|
||||
mock_pkg('foobarpkg', {
|
||||
'data.bin': dummy_data
|
||||
})
|
||||
|
||||
result = _opener.open('pkg://foobarpkg/data.bin').readline()
|
||||
|
||||
assert isinstance(result, bytes)
|
||||
assert result == dummy_data
|
||||
|
||||
|
||||
@run_in_subprocess_once()
|
||||
def test_subdir(self, mock_pkg):
|
||||
"""
|
||||
"""
|
||||
_opener: urllib.request.OpenerDirector = urllib.request.build_opener(
|
||||
PkgHandler()
|
||||
)
|
||||
|
||||
dummy_data = 'foobar123'
|
||||
|
||||
mock_pkg('foobarpkg', {
|
||||
'foo/bar/data.txt': dummy_data
|
||||
})
|
||||
|
||||
result = _opener.open('pkg://foobarpkg/foo/bar/data.txt').readline()
|
||||
|
||||
assert result == dummy_data
|
||||
|
||||
|
||||
@run_in_subprocess_once()
|
||||
def test_nested_module(self, mock_pkg):
|
||||
"""
|
||||
"""
|
||||
_opener: urllib.request.OpenerDirector = urllib.request.build_opener(
|
||||
PkgHandler()
|
||||
)
|
||||
|
||||
dummy_data = 'foobar123'
|
||||
|
||||
mock_pkg('foo.bar.pkg', {
|
||||
'dummy/data.txt': dummy_data
|
||||
})
|
||||
|
||||
result = _opener.open('pkg://foo.bar.pkg/dummy/data.txt').readline()
|
||||
|
||||
assert result == dummy_data
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue