Skip to content

Commit 36e47ce

Browse files
committed
🎨 Replace deprecated open_binary method with files
Signed-off-by: ff137 <[email protected]>
1 parent d8632ee commit 36e47ce

File tree

2 files changed

+46
-15
lines changed

2 files changed

+46
-15
lines changed

acapy_agent/config/logging/configurator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def load_resource(path: str, encoding: Optional[str] = None):
5252
else:
5353
# Package resource
5454
package, resource = components
55-
bstream = resources.open_binary(package, resource)
55+
bstream = resources.files(package).joinpath(resource).open("rb")
5656
if encoding:
5757
return io.TextIOWrapper(bstream, encoding=encoding)
5858
return bstream

acapy_agent/config/tests/test_logging.py

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import contextlib
2-
from io import StringIO
2+
from io import BufferedReader, StringIO, TextIOWrapper
33
from tempfile import NamedTemporaryFile
44
from unittest import IsolatedAsyncioTestCase, mock
55

@@ -110,24 +110,55 @@ def test_banner_did(self):
110110
def test_load_resource(self):
111111
# Testing local file access
112112
with mock.patch("builtins.open", mock.MagicMock()) as mock_open:
113-
test_module.load_resource("abc", encoding="utf-8")
113+
# First call succeeds
114+
file_handle = mock.MagicMock(spec=TextIOWrapper)
115+
mock_open.return_value = file_handle
116+
result = test_module.load_resource("abc", encoding="utf-8")
117+
mock_open.assert_called_once_with("abc", encoding="utf-8")
118+
assert result == file_handle # Verify the returned file handle
119+
120+
mock_open.reset_mock()
121+
# Simulate IOError on second call
114122
mock_open.side_effect = IOError("insufficient privilege")
115123
# load_resource should absorb IOError
116-
test_module.load_resource("abc", encoding="utf-8")
124+
result = test_module.load_resource("abc", encoding="utf-8")
125+
mock_open.assert_called_once_with("abc", encoding="utf-8")
126+
assert result is None
117127

118128
# Testing package resource access with encoding (text mode)
119-
with mock.patch(
120-
"importlib.resources.open_binary", mock.MagicMock()
121-
) as mock_open_binary, mock.patch(
129+
with mock.patch("importlib.resources.files") as mock_files, mock.patch(
122130
"io.TextIOWrapper", mock.MagicMock()
123131
) as mock_text_io_wrapper:
124-
test_module.load_resource("abc:def", encoding="utf-8")
125-
mock_open_binary.assert_called_once_with("abc", "def")
126-
mock_text_io_wrapper.assert_called_once()
132+
# Setup the mocks
133+
mock_resource_path = mock.MagicMock()
134+
mock_files.return_value.joinpath.return_value = mock_resource_path
135+
mock_resource_handle = mock.MagicMock(spec=BufferedReader)
136+
mock_resource_path.open.return_value = mock_resource_handle
137+
mock_text_io_wrapper.return_value = mock.MagicMock(spec=TextIOWrapper)
138+
139+
result = test_module.load_resource("abc:def", encoding="utf-8")
140+
141+
# Assertions
142+
mock_files.assert_called_once_with("abc")
143+
mock_files.return_value.joinpath.assert_called_once_with("def")
144+
mock_resource_path.open.assert_called_once_with("rb")
145+
mock_text_io_wrapper.assert_called_once_with(
146+
mock_resource_handle, encoding="utf-8"
147+
)
148+
assert result is mock_text_io_wrapper.return_value
127149

128150
# Testing package resource access without encoding (binary mode)
129-
with mock.patch(
130-
"importlib.resources.open_binary", mock.MagicMock()
131-
) as mock_open_binary:
132-
test_module.load_resource("abc:def", encoding=None)
133-
mock_open_binary.assert_called_once_with("abc", "def")
151+
with mock.patch("importlib.resources.files") as mock_files:
152+
# Setup the mocks
153+
mock_resource_path = mock.MagicMock()
154+
mock_files.return_value.joinpath.return_value = mock_resource_path
155+
mock_resource_handle = mock.MagicMock(spec=BufferedReader)
156+
mock_resource_path.open.return_value = mock_resource_handle
157+
158+
result = test_module.load_resource("abc:def", encoding=None)
159+
160+
# Assertions
161+
mock_files.assert_called_once_with("abc")
162+
mock_files.return_value.joinpath.assert_called_once_with("def")
163+
mock_resource_path.open.assert_called_once_with("rb")
164+
assert result == mock_resource_handle # Verify the returned binary stream

0 commit comments

Comments
 (0)