Skip to content

Commit ffcd5f9

Browse files
committed
[test] Add test for Distribution.update_dist_android_api
1 parent 30ab490 commit ffcd5f9

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

tests/test_distribution.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@
2121
"python_version": "3.7",
2222
}
2323

24+
gradle_file_partial_content = f"""
25+
android {{
26+
compileSdkVersion {dist_info_data["android_api"]}
27+
buildToolsVersion '29.0.1'
28+
defaultConfig {{
29+
minSdkVersion {dist_info_data["ndk_api"]}
30+
targetSdkVersion {dist_info_data["android_api"]}
31+
versionCode 821101
32+
versionName '1.1'
33+
}}
34+
}}
35+
"""
36+
2437

2538
class TestDistribution(unittest.TestCase):
2639
"""
@@ -158,6 +171,45 @@ def test_update_dist_project_properties_android_api(self, mock_open):
158171
)
159172
self.assertEqual(mock_open.call_args[0][1], 'w')
160173

174+
@mock.patch("pythonforandroid.distribution.open")
175+
def test_update_dist_android_api(self, mock_open):
176+
"""Test that method
177+
:meth:`~pythonforandroid.distribution.Distribution.update_dist_android_api`
178+
calls the proper methods with the right arguments."""
179+
self.setUp_distribution_with_bootstrap(
180+
Bootstrap().get_bootstrap("sdl2", self.ctx)
181+
)
182+
183+
new_android_api = 28
184+
new_gradle_content = gradle_file_partial_content.replace(
185+
f"compileSdkVersion {dist_info_data['android_api']}",
186+
f"compileSdkVersion {new_android_api}"
187+
).replace(
188+
f"targetSdkVersion {dist_info_data['android_api']}",
189+
f"targetSdkVersion {new_android_api}"
190+
)
191+
mock_open.side_effect = [
192+
mock.mock_open(read_data=gradle_file_partial_content).return_value,
193+
mock.mock_open(read_data=new_gradle_content).return_value,
194+
mock.mock_open(read_data="target=android-27").return_value,
195+
mock.mock_open(read_data=json.dumps(dist_info_data)).return_value,
196+
mock.mock_open(read_data=json.dumps(dist_info_data)).return_value,
197+
]
198+
199+
dist = self.ctx.bootstrap.distribution
200+
dist.update_dist_android_api(new_android_api)
201+
mock_open.assert_has_calls(
202+
[
203+
mock.call(os.path.join(dist.dist_dir, "build.gradle"), "r"),
204+
mock.call(os.path.join(dist.dist_dir, "build.gradle"), "w"),
205+
mock.call(
206+
os.path.join(dist.dist_dir, "project.properties"), "w"
207+
),
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"),
210+
]
211+
)
212+
161213
@mock.patch("pythonforandroid.distribution.exists")
162214
def test_folder_exist(self, mock_exists):
163215
"""Test that method

0 commit comments

Comments
 (0)