From 783ea197646fb53ac4a26e62ae14a6b36997a87a Mon Sep 17 00:00:00 2001 From: Dan Nicholson Date: Mon, 25 Apr 2022 12:38:02 -0600 Subject: [PATCH 1/2] Correct recipe test assertion `Arch.find_executable` uses the context environment `PATH` rather than the process environment `PATH`, so change the assertion to test the correct value. At present these are the same in this test, but since f7f8cea636714dc4843b8818c553510f99084b6a `PATH` is updated in the `Context.env` attribute by `Context.prepare_build_environment` instead of `os.environ`. If `Recipe.get_recipe_env` or `Arch.get_env` were changed to call `prepare_build_environment`, this test would fail. --- tests/test_recipe.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_recipe.py b/tests/test_recipe.py index 54b347801f..c82a32fb95 100644 --- a/tests/test_recipe.py +++ b/tests/test_recipe.py @@ -294,7 +294,7 @@ def test_get_recipe_env_with( # check that the mocks have been called mock_ensure_dir.assert_called() mock_find_executable.assert_called_once_with( - expected_compiler, path=os.environ['PATH'] + expected_compiler, path=self.ctx.env['PATH'] ) self.assertIsInstance(env, dict) From 0ef906f9a7dfc512ad43145ad3b20b52fe11fa15 Mon Sep 17 00:00:00 2001 From: Dan Nicholson Date: Tue, 19 Apr 2022 13:35:26 -0600 Subject: [PATCH 2/2] Set PATH using real SDK and NDK directories This is a regression from f7f8cea636714dc4843b8818c553510f99084b6a. Previously, `self.sdk_dir` and `self.ndk_dir` were passed to `select_and_check_toolchain_version`. --- pythonforandroid/build.py | 6 +++--- tests/test_build.py | 45 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/pythonforandroid/build.py b/pythonforandroid/build.py index 32d54eaa28..81b9e309e8 100644 --- a/pythonforandroid/build.py +++ b/pythonforandroid/build.py @@ -370,9 +370,9 @@ def prepare_build_environment(self, self.env["PATH"] = ":".join( [ - f"{ndk_dir}/toolchains/llvm/prebuilt/{py_platform}-x86_64/bin", - ndk_dir, - f"{sdk_dir}/tools", + f"{self.ndk_dir}/toolchains/llvm/prebuilt/{py_platform}-x86_64/bin", + self.ndk_dir, + f"{self.sdk_dir}/tools", environ.get("PATH"), ] ) diff --git a/tests/test_build.py b/tests/test_build.py index 1ffa46fc52..634a04b643 100644 --- a/tests/test_build.py +++ b/tests/test_build.py @@ -1,9 +1,13 @@ +import os +import sys import unittest from unittest import mock import jinja2 -from pythonforandroid.build import run_pymodules_install +from pythonforandroid.build import ( + Context, RECOMMENDED_TARGET_API, run_pymodules_install, +) from pythonforandroid.archs import ArchARMv7_a, ArchAarch_64 @@ -89,3 +93,42 @@ def test_android_manifest_xml(self): assert xml.count('android:debuggable="true"') == 1 assert xml.count('') == 1 # TODO: potentially some other checks to be added here to cover other "logic" (flags and loops) in the template + + +class TestContext(unittest.TestCase): + + @mock.patch.dict('pythonforandroid.build.Context.env') + @mock.patch('pythonforandroid.build.get_available_apis') + @mock.patch('pythonforandroid.build.ensure_dir') + def test_sdk_ndk_paths( + self, + mock_ensure_dir, + mock_get_available_apis, + ): + mock_get_available_apis.return_value = [RECOMMENDED_TARGET_API] + context = Context() + context.setup_dirs(os.getcwd()) + context.prepare_build_environment( + user_sdk_dir='sdk', + user_ndk_dir='ndk', + user_android_api=None, + user_ndk_api=None, + ) + + # The context was supplied with relative SDK and NDK dirs. Check + # that it resolved them to absolute paths. + real_sdk_dir = os.path.join(os.getcwd(), 'sdk') + real_ndk_dir = os.path.join(os.getcwd(), 'ndk') + assert context.sdk_dir == real_sdk_dir + assert context.ndk_dir == real_ndk_dir + + py_platform = sys.platform + if py_platform in ['linux2', 'linux3']: + py_platform = 'linux' + + context_paths = context.env['PATH'].split(':') + assert context_paths[0:3] == [ + f'{real_ndk_dir}/toolchains/llvm/prebuilt/{py_platform}-x86_64/bin', + real_ndk_dir, + f'{real_sdk_dir}/tools' + ]