|
| 1 | +from unittest import mock |
| 2 | +from tests.recipes.recipe_ctx import RecipeCtx |
| 3 | + |
| 4 | + |
| 5 | +class BaseTestForMakeRecipe(RecipeCtx): |
| 6 | + """ |
| 7 | + An unittest for testing any recipe using the standard build commands |
| 8 | + (`configure/make`). |
| 9 | +
|
| 10 | + .. note:: Note that Some cmake recipe may need some more specific testing |
| 11 | + ...but this should cover the basics. |
| 12 | + """ |
| 13 | + |
| 14 | + recipe_name = None |
| 15 | + recipes = ["python3", "kivy"] |
| 16 | + recipe_build_order = ["hostpython3", "python3", "sdl2", "kivy"] |
| 17 | + expected_compiler = ( |
| 18 | + "{android_ndk}/toolchains/llvm/prebuilt/linux-x86_64/bin/clang" |
| 19 | + ) |
| 20 | + |
| 21 | + sh_command_calls = ["./configure"] |
| 22 | + """The expected commands that the recipe runs via `sh.command`.""" |
| 23 | + |
| 24 | + extra_env_flags = {} |
| 25 | + """ |
| 26 | + This must be a dictionary containing pairs of key (env var) and value. |
| 27 | + """ |
| 28 | + |
| 29 | + def __new__(cls, *args): |
| 30 | + obj = super().__new__(cls) |
| 31 | + if obj.recipe_name is not None: |
| 32 | + print(f"We are testing recipe: {obj.recipe_name}") |
| 33 | + obj.recipes.append(obj.recipe_name) |
| 34 | + obj.recipe_build_order.insert(1, obj.recipe_name) |
| 35 | + return obj |
| 36 | + |
| 37 | + @mock.patch("pythonforandroid.recipe.Recipe.check_recipe_choices") |
| 38 | + @mock.patch("pythonforandroid.build.ensure_dir") |
| 39 | + @mock.patch("pythonforandroid.archs.glob") |
| 40 | + @mock.patch("pythonforandroid.archs.find_executable") |
| 41 | + def test_get_recipe_env( |
| 42 | + self, |
| 43 | + mock_find_executable, |
| 44 | + mock_glob, |
| 45 | + mock_ensure_dir, |
| 46 | + mock_check_recipe_choices, |
| 47 | + ): |
| 48 | + """ |
| 49 | + Test that get_recipe_env contains some expected arch flags and that |
| 50 | + some internal methods has been called. |
| 51 | + """ |
| 52 | + mock_find_executable.return_value = self.expected_compiler.format( |
| 53 | + android_ndk=self.ctx._ndk_dir |
| 54 | + ) |
| 55 | + mock_glob.return_value = ["llvm"] |
| 56 | + mock_check_recipe_choices.return_value = sorted( |
| 57 | + self.ctx.recipe_build_order |
| 58 | + ) |
| 59 | + |
| 60 | + # make sure the arch flags are in env |
| 61 | + env = self.recipe.get_recipe_env(self.arch) |
| 62 | + for flag in self.arch.arch_cflags: |
| 63 | + self.assertIn(flag, env["CFLAGS"]) |
| 64 | + self.assertIn( |
| 65 | + f"-target {self.arch.target}", |
| 66 | + env["CFLAGS"], |
| 67 | + ) |
| 68 | + |
| 69 | + for flag, value in self.extra_env_flags.items(): |
| 70 | + self.assertIn(value, env[flag]) |
| 71 | + |
| 72 | + # make sure that the mocked methods are actually called |
| 73 | + mock_glob.assert_called() |
| 74 | + mock_ensure_dir.assert_called() |
| 75 | + mock_find_executable.assert_called() |
| 76 | + mock_check_recipe_choices.assert_called() |
| 77 | + |
| 78 | + @mock.patch("pythonforandroid.util.chdir") |
| 79 | + @mock.patch("pythonforandroid.build.ensure_dir") |
| 80 | + @mock.patch("pythonforandroid.archs.glob") |
| 81 | + @mock.patch("pythonforandroid.archs.find_executable") |
| 82 | + def test_build_arch( |
| 83 | + self, |
| 84 | + mock_find_executable, |
| 85 | + mock_glob, |
| 86 | + mock_ensure_dir, |
| 87 | + mock_current_directory, |
| 88 | + ): |
| 89 | + mock_find_executable.return_value = self.expected_compiler.format( |
| 90 | + android_ndk=self.ctx._ndk_dir |
| 91 | + ) |
| 92 | + mock_glob.return_value = ["llvm"] |
| 93 | + |
| 94 | + # Since the following mocks are dynamic, |
| 95 | + # we mock it inside a Context Manager |
| 96 | + with mock.patch( |
| 97 | + f"pythonforandroid.recipes.{self.recipe_name}.sh.Command" |
| 98 | + ) as mock_sh_command, mock.patch( |
| 99 | + f"pythonforandroid.recipes.{self.recipe_name}.sh.make" |
| 100 | + ) as mock_make: |
| 101 | + self.recipe.build_arch(self.arch) |
| 102 | + |
| 103 | + # make sure that the mocked methods are actually called |
| 104 | + for command in self.sh_command_calls: |
| 105 | + self.assertIn( |
| 106 | + mock.call(command), |
| 107 | + mock_sh_command.mock_calls, |
| 108 | + ) |
| 109 | + mock_make.assert_called() |
| 110 | + mock_glob.assert_called() |
| 111 | + mock_ensure_dir.assert_called() |
| 112 | + mock_current_directory.assert_called() |
| 113 | + mock_find_executable.assert_called() |
| 114 | + |
| 115 | + |
| 116 | +class BaseTestForCmakeRecipe(BaseTestForMakeRecipe): |
| 117 | + """ |
| 118 | + An unittest for testing any recipe using `cmake`. It inherits from |
| 119 | + `BaseTestForMakeRecipe` but we override the build method to match the cmake |
| 120 | + build method. |
| 121 | +
|
| 122 | + .. note:: Note that Some cmake recipe may need some more specific testing |
| 123 | + ...but this should cover the basics. |
| 124 | + """ |
| 125 | + |
| 126 | + @mock.patch("pythonforandroid.util.chdir") |
| 127 | + @mock.patch("pythonforandroid.build.ensure_dir") |
| 128 | + @mock.patch("pythonforandroid.archs.glob") |
| 129 | + @mock.patch("pythonforandroid.archs.find_executable") |
| 130 | + def test_build_arch( |
| 131 | + self, |
| 132 | + mock_find_executable, |
| 133 | + mock_glob, |
| 134 | + mock_ensure_dir, |
| 135 | + mock_current_directory, |
| 136 | + ): |
| 137 | + mock_find_executable.return_value = self.expected_compiler.format( |
| 138 | + android_ndk=self.ctx._ndk_dir |
| 139 | + ) |
| 140 | + mock_glob.return_value = ["llvm"] |
| 141 | + |
| 142 | + # Since the following mocks are dynamic, |
| 143 | + # we mock it inside a Context Manager |
| 144 | + with mock.patch( |
| 145 | + f"pythonforandroid.recipes.{self.recipe_name}.sh.make" |
| 146 | + ) as mock_make, mock.patch( |
| 147 | + f"pythonforandroid.recipes.{self.recipe_name}.sh.cmake" |
| 148 | + ) as mock_cmake: |
| 149 | + self.recipe.build_arch(self.arch) |
| 150 | + |
| 151 | + # make sure that the mocked methods are actually called |
| 152 | + mock_cmake.assert_called() |
| 153 | + mock_make.assert_called() |
| 154 | + mock_glob.assert_called() |
| 155 | + mock_ensure_dir.assert_called() |
| 156 | + mock_current_directory.assert_called() |
| 157 | + mock_find_executable.assert_called() |
0 commit comments