|
| 1 | +import os |
| 2 | +import json |
| 3 | +import unittest |
| 4 | + |
| 5 | +try: |
| 6 | + from unittest import mock |
| 7 | +except ImportError: |
| 8 | + # `Python 2` or lower than `Python 3.3` does not |
| 9 | + # have the `unittest.mock` module built-in |
| 10 | + import mock |
| 11 | +from pythonforandroid.bootstrap import Bootstrap |
| 12 | +from pythonforandroid.distribution import Distribution |
| 13 | +from pythonforandroid.recipe import Recipe |
| 14 | +from pythonforandroid.util import BuildInterruptingException |
| 15 | +from pythonforandroid.build import Context |
| 16 | + |
| 17 | +dist_info_data = { |
| 18 | + "dist_name": None, |
| 19 | + "bootstrap": "sdl2", |
| 20 | + "archs": ["armeabi", "armeabi-v7a", "x86", "x86_64", "arm64-v8a"], |
| 21 | + "ndk_api": 21, |
| 22 | + "use_setup_py": False, |
| 23 | + "recipes": ["hostpython3", "python3", "sdl2", "kivy", "requests"], |
| 24 | + "hostpython": "/some/fake/hostpython3", |
| 25 | + "python_version": "3.7", |
| 26 | +} |
| 27 | + |
| 28 | + |
| 29 | +class TestDistribution(unittest.TestCase): |
| 30 | + def setUp(self): |
| 31 | + self.ctx = Context() |
| 32 | + self.ctx.ndk_api = 21 |
| 33 | + self.ctx.android_api = 27 |
| 34 | + self.ctx._sdk_dir = "/opt/android/android-sdk" |
| 35 | + self.ctx._ndk_dir = "/opt/android/android-ndk" |
| 36 | + self.ctx.setup_dirs(os.getcwd()) |
| 37 | + self.ctx.recipe_build_order = [ |
| 38 | + "hostpython3", |
| 39 | + "python3", |
| 40 | + "sdl2", |
| 41 | + "kivy", |
| 42 | + ] |
| 43 | + |
| 44 | + def setUp_distribution_with_bootstrap(self, bs, **kwargs): |
| 45 | + # extend the setUp by configuring a distribution, because some test |
| 46 | + # needs a distribution to be set to be properly tested |
| 47 | + self.ctx.bootstrap = bs |
| 48 | + self.ctx.bootstrap.distribution = Distribution.get_distribution( |
| 49 | + self.ctx, |
| 50 | + name=kwargs.pop("name", "test_prj"), |
| 51 | + recipes=kwargs.pop("recipes", ["python3", "kivy"]), |
| 52 | + **kwargs |
| 53 | + ) |
| 54 | + |
| 55 | + def tearDown(self): |
| 56 | + if self.ctx.bootstrap: |
| 57 | + self.ctx.bootstrap.distribution = None |
| 58 | + self.ctx.bootstrap = None |
| 59 | + |
| 60 | + def test_properties(self): |
| 61 | + self.setUp_distribution_with_bootstrap( |
| 62 | + Bootstrap().get_bootstrap("sdl2", self.ctx) |
| 63 | + ) |
| 64 | + distribution = self.ctx.bootstrap.distribution |
| 65 | + self.assertEqual(self.ctx, distribution.ctx) |
| 66 | + expected_repr = ( |
| 67 | + "<Distribution: name test_prj with recipes (python3, kivy)>" |
| 68 | + ) |
| 69 | + self.assertEqual(distribution.__str__(), expected_repr) |
| 70 | + self.assertEqual(distribution.__repr__(), expected_repr) |
| 71 | + |
| 72 | + @mock.patch("pythonforandroid.distribution.exists") |
| 73 | + def test_folder_exist(self, mock_exists): |
| 74 | + |
| 75 | + self.setUp_distribution_with_bootstrap( |
| 76 | + Bootstrap().get_bootstrap("sdl2", self.ctx) |
| 77 | + ) |
| 78 | + self.ctx.bootstrap.distribution.folder_exists() |
| 79 | + mock_exists.assert_called_with( |
| 80 | + self.ctx.bootstrap.distribution.dist_dir |
| 81 | + ) |
| 82 | + |
| 83 | + @mock.patch("pythonforandroid.distribution.rmtree") |
| 84 | + def test_delete(self, mock_rmtree): |
| 85 | + |
| 86 | + self.setUp_distribution_with_bootstrap( |
| 87 | + Bootstrap().get_bootstrap("sdl2", self.ctx) |
| 88 | + ) |
| 89 | + self.ctx.bootstrap.distribution.delete() |
| 90 | + mock_rmtree.assert_called_with( |
| 91 | + self.ctx.bootstrap.distribution.dist_dir |
| 92 | + ) |
| 93 | + |
| 94 | + @mock.patch("pythonforandroid.distribution.exists") |
| 95 | + def test_get_distribution_no_name(self, mock_exists): |
| 96 | + |
| 97 | + mock_exists.return_value = False |
| 98 | + self.ctx.bootstrap = Bootstrap().get_bootstrap("sdl2", self.ctx) |
| 99 | + dist = Distribution.get_distribution(self.ctx) |
| 100 | + self.assertEqual(dist.name, "unnamed_dist_1") |
| 101 | + |
| 102 | + @mock.patch("pythonforandroid.util.chdir") |
| 103 | + @mock.patch("pythonforandroid.distribution.open", create=True) |
| 104 | + def test_save_info(self, mock_open_dist_info, mock_chdir): |
| 105 | + self.setUp_distribution_with_bootstrap( |
| 106 | + Bootstrap().get_bootstrap("sdl2", self.ctx) |
| 107 | + ) |
| 108 | + self.ctx.hostpython = "/some/fake/hostpython3" |
| 109 | + self.ctx.python_recipe = Recipe.get_recipe("python3", self.ctx) |
| 110 | + self.ctx.python_modules = ["requests"] |
| 111 | + mock_open_dist_info.side_effect = [ |
| 112 | + mock.mock_open(read_data=json.dumps(dist_info_data)).return_value |
| 113 | + ] |
| 114 | + self.ctx.bootstrap.distribution.save_info("/fake_dir") |
| 115 | + mock_open_dist_info.assert_called_once_with("dist_info.json", "w") |
| 116 | + mock_open_dist_info.reset_mock() |
| 117 | + |
| 118 | + @mock.patch("pythonforandroid.distribution.open", create=True) |
| 119 | + @mock.patch("pythonforandroid.distribution.exists") |
| 120 | + @mock.patch("pythonforandroid.distribution.glob.glob") |
| 121 | + def test_get_distributions( |
| 122 | + self, mock_glob, mock_exists, mock_open_dist_info |
| 123 | + ): |
| 124 | + self.setUp_distribution_with_bootstrap( |
| 125 | + Bootstrap().get_bootstrap("sdl2", self.ctx) |
| 126 | + ) |
| 127 | + mock_glob.return_value = ["sdl2-python3"] |
| 128 | + mock_open_dist_info.side_effect = [ |
| 129 | + mock.mock_open(read_data=json.dumps(dist_info_data)).return_value |
| 130 | + ] |
| 131 | + |
| 132 | + dists = self.ctx.bootstrap.distribution.get_distributions(self.ctx) |
| 133 | + self.assertIsInstance(dists, list) |
| 134 | + self.assertEqual(len(dists), 1) |
| 135 | + self.assertIsInstance(dists[0], Distribution) |
| 136 | + self.assertEqual(dists[0].name, "sdl2-python3") |
| 137 | + self.assertEqual(dists[0].ndk_api, 21) |
| 138 | + self.assertEqual( |
| 139 | + dists[0].recipes, |
| 140 | + ["hostpython3", "python3", "sdl2", "kivy", "requests"], |
| 141 | + ) |
| 142 | + mock_open_dist_info.assert_called_with("sdl2-python3/dist_info.json") |
| 143 | + mock_open_dist_info.reset_mock() |
| 144 | + |
| 145 | + @mock.patch("pythonforandroid.distribution.open", create=True) |
| 146 | + @mock.patch("pythonforandroid.distribution.exists") |
| 147 | + @mock.patch("pythonforandroid.distribution.glob.glob") |
| 148 | + def test_get_distributions_error_ndk_api( |
| 149 | + self, mock_glob, mock_exists, mock_open_dist_info |
| 150 | + ): |
| 151 | + dist_info_data_no_ndk_api = dist_info_data.copy() |
| 152 | + dist_info_data_no_ndk_api.pop("ndk_api") |
| 153 | + self.setUp_distribution_with_bootstrap( |
| 154 | + Bootstrap().get_bootstrap("sdl2", self.ctx) |
| 155 | + ) |
| 156 | + mock_glob.return_value = ["sdl2-python3"] |
| 157 | + mock_open_dist_info.side_effect = [ |
| 158 | + mock.mock_open( |
| 159 | + read_data=json.dumps(dist_info_data_no_ndk_api) |
| 160 | + ).return_value |
| 161 | + ] |
| 162 | + |
| 163 | + dists = self.ctx.bootstrap.distribution.get_distributions(self.ctx) |
| 164 | + self.assertEqual(dists[0].ndk_api, None) |
| 165 | + mock_open_dist_info.assert_called_with("sdl2-python3/dist_info.json") |
| 166 | + mock_open_dist_info.reset_mock() |
| 167 | + |
| 168 | + @mock.patch("pythonforandroid.distribution.Distribution.get_distributions") |
| 169 | + @mock.patch("pythonforandroid.distribution.exists") |
| 170 | + @mock.patch("pythonforandroid.distribution.glob.glob") |
| 171 | + def test_get_distributions_error_ndk_api_mismatch( |
| 172 | + self, mock_glob, mock_exists, mock_get_dists |
| 173 | + ): |
| 174 | + expected_dist = Distribution.get_distribution( |
| 175 | + self.ctx, name="test_prj", recipes=["python3", "kivy"] |
| 176 | + ) |
| 177 | + mock_get_dists.return_value = [expected_dist] |
| 178 | + mock_glob.return_value = ["sdl2-python3"] |
| 179 | + |
| 180 | + with self.assertRaises(BuildInterruptingException) as e: |
| 181 | + self.setUp_distribution_with_bootstrap( |
| 182 | + Bootstrap().get_bootstrap("sdl2", self.ctx), |
| 183 | + allow_replace_dist=False, |
| 184 | + ndk_api=22, |
| 185 | + ) |
| 186 | + self.assertEqual( |
| 187 | + e.exception.args[0], |
| 188 | + "Asked for dist with name test_prj with recipes (python3, kivy)" |
| 189 | + " and NDK API 22, but a dist with this name already exists and has" |
| 190 | + " either incompatible recipes (python3, kivy) or NDK API 21", |
| 191 | + ) |
| 192 | + |
| 193 | + def test_get_distributions_error_extra_dist_dirs(self): |
| 194 | + self.setUp_distribution_with_bootstrap( |
| 195 | + Bootstrap().get_bootstrap("sdl2", self.ctx) |
| 196 | + ) |
| 197 | + |
| 198 | + with self.assertRaises(BuildInterruptingException) as e: |
| 199 | + self.ctx.bootstrap.distribution.get_distributions( |
| 200 | + self.ctx, extra_dist_dirs=["/fake/extra/dist_dirs"] |
| 201 | + ) |
| 202 | + self.assertEqual( |
| 203 | + e.exception.args[0], |
| 204 | + "extra_dist_dirs argument to get" |
| 205 | + "_distributions is not yet implemented", |
| 206 | + ) |
| 207 | + |
| 208 | + @mock.patch("pythonforandroid.distribution.Distribution.get_distributions") |
| 209 | + def test_get_distributions_possible_dists(self, mock_get_dists): |
| 210 | + expected_dist = Distribution.get_distribution( |
| 211 | + self.ctx, name="test_prj", recipes=["python3", "kivy"] |
| 212 | + ) |
| 213 | + mock_get_dists.return_value = [expected_dist] |
| 214 | + self.setUp_distribution_with_bootstrap( |
| 215 | + Bootstrap().get_bootstrap("sdl2", self.ctx), name="test_prj" |
| 216 | + ) |
| 217 | + dists = self.ctx.bootstrap.distribution.get_distributions(self.ctx) |
| 218 | + self.assertEqual(dists[0], expected_dist) |
0 commit comments