Skip to content

Commit 948b83b

Browse files
committed
[dist] Convert get_dist_info into the cls property dist_info
So we can reduce the a little more the complexity of cls `Distribution`
1 parent 9942f09 commit 948b83b

File tree

2 files changed

+42
-38
lines changed

2 files changed

+42
-38
lines changed

pythonforandroid/distribution.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,10 @@ def dist_info_file(self):
251251
"""Return the path of the distribution `dist_info.json` file."""
252252
return str(Path(self.dist_dir, 'dist_info.json'))
253253

254-
@staticmethod
255-
def get_dist_info(dist_dir):
256-
"""Load `dist_info.json` file given a dist path."""
257-
dist_json_file = str(Path(dist_dir, 'dist_info.json'))
258-
with open(dist_json_file, 'r') as file_opened:
254+
@property
255+
def dist_info(self):
256+
"""Load the Distribution's `dist_info.json` file."""
257+
with open(self.dist_info_file, 'r') as file_opened:
259258
dist_info = json.load(file_opened)
260259
return dist_info
261260

@@ -285,12 +284,12 @@ def save_info(self, dirn):
285284
)
286285

287286
def update_dist_info(self, key, value):
288-
"""Update `dist_info.json` values."""
289-
dist_info = self.get_dist_info(self.dist_dir)
287+
"""Update `dist_info.json` file values."""
288+
new_dist_info = self.dist_info.copy()
290289

291-
dist_info[key] = value
290+
new_dist_info[key] = value
292291
with open(self.dist_info_file, 'w') as file_opened:
293-
json.dump(dist_info, file_opened, indent=4, sort_keys=True)
292+
json.dump(new_dist_info, file_opened, indent=4, sort_keys=True)
294293

295294
def update_dist_project_properties_android_api(self, new_android_api):
296295
"""Update `project.properties` with a new android api."""

tests/test_distribution.py

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -103,20 +103,21 @@ def test_dist_info_file(self):
103103
os.path.join(distribution.dist_dir, "dist_info.json")
104104
)
105105

106-
@mock.patch("pythonforandroid.distribution.open", create=True)
107-
def test_get_dist_info(self, mock_open):
106+
def test_dist_info(self):
108107
"""Test that method
109-
:meth:`~pythonforandroid.distribution.Distribution.get_dist_info`
108+
:meth:`~pythonforandroid.distribution.Distribution.dist_info`
110109
calls the proper methods and returns the proper value."""
111110
self.setUp_distribution_with_bootstrap(
112111
Bootstrap().get_bootstrap("sdl2", self.ctx)
113112
)
114-
mock_open.side_effect = [
115-
mock.mock_open(read_data=json.dumps(dist_info_data)).return_value
116-
]
117-
dist_info = self.ctx.bootstrap.distribution.get_dist_info("/fake_dir")
118-
mock_open.assert_called_once_with("/fake_dir/dist_info.json", "r")
119-
self.assertIsInstance(dist_info, dict)
113+
114+
mock_open = mock.mock_open(read_data=json.dumps(dist_info_data))
115+
with mock.patch("pythonforandroid.distribution.open", mock_open):
116+
dist_info = self.ctx.bootstrap.distribution.dist_info
117+
mock_open.assert_called_once_with(
118+
self.ctx.bootstrap.distribution.dist_info_file, "r",
119+
)
120+
self.assertEqual(dist_info, dist_info_data)
120121

121122
@mock.patch("pythonforandroid.distribution.json.dump")
122123
@mock.patch("pythonforandroid.distribution.open", create=True)
@@ -132,24 +133,22 @@ def test_update_dist_info(self, mock_open, mock_json):
132133
expected_json_file = mock.mock_open(
133134
read_data=json.dumps(new_info_data)
134135
).return_value
135-
mock_open.side_effect = [
136-
# first call to open, when we read the file
137-
mock.mock_open(read_data=json.dumps(dist_info_data)).return_value,
138-
# second call to open, when we update the file
139-
expected_json_file,
140-
]
136+
mock_open.side_effect = [expected_json_file]
137+
138+
with mock.patch(
139+
'pythonforandroid.distribution.Distribution.dist_info',
140+
new_callable=mock.PropertyMock,
141+
return_value=dist_info_data,
142+
) as mock_dist_info:
143+
self.ctx.bootstrap.distribution.update_dist_info(
144+
"android_api", new_info_data['android_api']
145+
)
146+
# We expect two calls to property `dist_info` (read and write)
147+
self.assertEqual(len(mock_dist_info.call_args), 2)
141148

142-
self.ctx.bootstrap.distribution.update_dist_info(
143-
"android_api", new_info_data['android_api']
144-
)
145-
# Note: call_args only contemplates the last mocked call, see also:
146-
# https://docs.python.org/3/library/unittest.mock.html#unittest.mock.Mock.call_args # noqa
147-
self.assertTrue(mock_open.call_args[0][0].endswith(
148-
'dists/test_prj__armeabi-v7a/dist_info.json')
149-
)
150-
self.assertEqual(mock_open.call_args[0][1], 'w')
151-
mock_json.assert_called_once_with(
152-
new_info_data, expected_json_file, indent=4, sort_keys=True,
149+
mock_open.assert_called_once_with(
150+
self.ctx.bootstrap.distribution.dist_info_file,
151+
"w",
153152
)
154153

155154
@mock.patch("pythonforandroid.distribution.open", create=True)
@@ -197,16 +196,22 @@ def test_update_dist_android_api(self, mock_open):
197196
]
198197

199198
dist = self.ctx.bootstrap.distribution
200-
dist.update_dist_android_api(new_android_api)
199+
with mock.patch(
200+
'pythonforandroid.distribution.Distribution.dist_info',
201+
new_callable=mock.PropertyMock,
202+
return_value=dist_info_data,
203+
) as mock_dist_info:
204+
dist.update_dist_android_api(new_android_api)
205+
# We expect two calls to property `dist_info` (read and write)
206+
self.assertEqual(len(mock_dist_info.call_args), 2)
207+
201208
mock_open.assert_has_calls(
202209
[
203210
mock.call(os.path.join(dist.dist_dir, "build.gradle"), "r"),
204211
mock.call(os.path.join(dist.dist_dir, "build.gradle"), "w"),
205212
mock.call(
206213
os.path.join(dist.dist_dir, "project.properties"), "w"
207214
),
208-
mock.call(os.path.join(dist.dist_dir, "dist_info.json"), "r"),
209-
mock.call(os.path.join(dist.dist_dir, "dist_info.json"), "w"),
210215
]
211216
)
212217

0 commit comments

Comments
 (0)