Skip to content

Commit b5c46ce

Browse files
committed
[test] Add test for Distribution.test_update_dist_info
1 parent 3d7ff31 commit b5c46ce

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

tests/test_distribution.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"bootstrap": "sdl2",
1515
"archs": ["armeabi", "armeabi-v7a", "x86", "x86_64", "arm64-v8a"],
1616
"ndk_api": 21,
17+
"android_api": 27,
1718
"use_setup_py": False,
1819
"recipes": ["hostpython3", "python3", "sdl2", "kivy", "requests"],
1920
"hostpython": "/some/fake/hostpython3",
@@ -33,8 +34,8 @@ def setUp(self):
3334
"""Configure a :class:`~pythonforandroid.build.Context` so we can
3435
perform our unittests"""
3536
self.ctx = Context()
36-
self.ctx.ndk_api = 21
37-
self.ctx.android_api = 27
37+
self.ctx.ndk_api = dist_info_data['ndk_api']
38+
self.ctx.android_api = dist_info_data['android_api']
3839
self.ctx._sdk_dir = "/opt/android/android-sdk"
3940
self.ctx._ndk_dir = "/opt/android/android-ndk"
4041
self.ctx.setup_dirs(os.getcwd())
@@ -104,6 +105,40 @@ def test_get_dist_info(self, mock_open):
104105
mock_open.assert_called_once_with("/fake_dir/dist_info.json", "r")
105106
self.assertIsInstance(dist_info, dict)
106107

108+
@mock.patch("pythonforandroid.distribution.json.dump")
109+
@mock.patch("pythonforandroid.distribution.open", create=True)
110+
def test_update_dist_info(self, mock_open, mock_json):
111+
"""Test that method
112+
:meth:`~pythonforandroid.distribution.Distribution.update_dist_info`
113+
calls the proper methods with the right arguments."""
114+
self.setUp_distribution_with_bootstrap(
115+
Bootstrap().get_bootstrap("sdl2", self.ctx)
116+
)
117+
new_info_data = dist_info_data
118+
new_info_data['android_api'] = 28
119+
expected_json_file = mock.mock_open(
120+
read_data=json.dumps(new_info_data)
121+
).return_value
122+
mock_open.side_effect = [
123+
# first call to open, when we read the file
124+
mock.mock_open(read_data=json.dumps(dist_info_data)).return_value,
125+
# second call to open, when we update the file
126+
expected_json_file,
127+
]
128+
129+
self.ctx.bootstrap.distribution.update_dist_info(
130+
"android_api", new_info_data['android_api']
131+
)
132+
# Note: call_args only contemplates the last mocked call, see also:
133+
# https://docs.python.org/3/library/unittest.mock.html#unittest.mock.Mock.call_args # noqa
134+
self.assertTrue(mock_open.call_args[0][0].endswith(
135+
'dists/test_prj__armeabi-v7a/dist_info.json')
136+
)
137+
self.assertEqual(mock_open.call_args[0][1], 'w')
138+
mock_json.assert_called_once_with(
139+
new_info_data, expected_json_file, indent=4, sort_keys=True,
140+
)
141+
107142
@mock.patch("pythonforandroid.distribution.exists")
108143
def test_folder_exist(self, mock_exists):
109144
"""Test that method

0 commit comments

Comments
 (0)