Skip to content

Commit

Permalink
🎨 Replace deprecated open_binary method with files
Browse files Browse the repository at this point in the history
Signed-off-by: ff137 <[email protected]>
  • Loading branch information
ff137 committed Nov 14, 2024
1 parent d8632ee commit 36e47ce
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 15 deletions.
2 changes: 1 addition & 1 deletion acapy_agent/config/logging/configurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def load_resource(path: str, encoding: Optional[str] = None):
else:
# Package resource
package, resource = components
bstream = resources.open_binary(package, resource)
bstream = resources.files(package).joinpath(resource).open("rb")
if encoding:
return io.TextIOWrapper(bstream, encoding=encoding)
return bstream
Expand Down
59 changes: 45 additions & 14 deletions acapy_agent/config/tests/test_logging.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import contextlib
from io import StringIO
from io import BufferedReader, StringIO, TextIOWrapper
from tempfile import NamedTemporaryFile
from unittest import IsolatedAsyncioTestCase, mock

Expand Down Expand Up @@ -110,24 +110,55 @@ def test_banner_did(self):
def test_load_resource(self):
# Testing local file access
with mock.patch("builtins.open", mock.MagicMock()) as mock_open:
test_module.load_resource("abc", encoding="utf-8")
# First call succeeds
file_handle = mock.MagicMock(spec=TextIOWrapper)
mock_open.return_value = file_handle
result = test_module.load_resource("abc", encoding="utf-8")
mock_open.assert_called_once_with("abc", encoding="utf-8")
assert result == file_handle # Verify the returned file handle

mock_open.reset_mock()
# Simulate IOError on second call
mock_open.side_effect = IOError("insufficient privilege")
# load_resource should absorb IOError
test_module.load_resource("abc", encoding="utf-8")
result = test_module.load_resource("abc", encoding="utf-8")
mock_open.assert_called_once_with("abc", encoding="utf-8")
assert result is None

# Testing package resource access with encoding (text mode)
with mock.patch(
"importlib.resources.open_binary", mock.MagicMock()
) as mock_open_binary, mock.patch(
with mock.patch("importlib.resources.files") as mock_files, mock.patch(
"io.TextIOWrapper", mock.MagicMock()
) as mock_text_io_wrapper:
test_module.load_resource("abc:def", encoding="utf-8")
mock_open_binary.assert_called_once_with("abc", "def")
mock_text_io_wrapper.assert_called_once()
# Setup the mocks
mock_resource_path = mock.MagicMock()
mock_files.return_value.joinpath.return_value = mock_resource_path
mock_resource_handle = mock.MagicMock(spec=BufferedReader)
mock_resource_path.open.return_value = mock_resource_handle
mock_text_io_wrapper.return_value = mock.MagicMock(spec=TextIOWrapper)

result = test_module.load_resource("abc:def", encoding="utf-8")

# Assertions
mock_files.assert_called_once_with("abc")
mock_files.return_value.joinpath.assert_called_once_with("def")
mock_resource_path.open.assert_called_once_with("rb")
mock_text_io_wrapper.assert_called_once_with(
mock_resource_handle, encoding="utf-8"
)
assert result is mock_text_io_wrapper.return_value

# Testing package resource access without encoding (binary mode)
with mock.patch(
"importlib.resources.open_binary", mock.MagicMock()
) as mock_open_binary:
test_module.load_resource("abc:def", encoding=None)
mock_open_binary.assert_called_once_with("abc", "def")
with mock.patch("importlib.resources.files") as mock_files:
# Setup the mocks
mock_resource_path = mock.MagicMock()
mock_files.return_value.joinpath.return_value = mock_resource_path
mock_resource_handle = mock.MagicMock(spec=BufferedReader)
mock_resource_path.open.return_value = mock_resource_handle

result = test_module.load_resource("abc:def", encoding=None)

# Assertions
mock_files.assert_called_once_with("abc")
mock_files.return_value.joinpath.assert_called_once_with("def")
mock_resource_path.open.assert_called_once_with("rb")
assert result == mock_resource_handle # Verify the returned binary stream

0 comments on commit 36e47ce

Please sign in to comment.