Skip to content

Commit 92cb8f4

Browse files
authored
Set PATH using real SDK and NDK directories (#2583)
* 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 f7f8cea `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. * Set PATH using real SDK and NDK directories This is a regression from f7f8cea. Previously, `self.sdk_dir` and `self.ndk_dir` were passed to `select_and_check_toolchain_version`.
1 parent 846d80d commit 92cb8f4

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

pythonforandroid/build.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -370,9 +370,9 @@ def prepare_build_environment(self,
370370

371371
self.env["PATH"] = ":".join(
372372
[
373-
f"{ndk_dir}/toolchains/llvm/prebuilt/{py_platform}-x86_64/bin",
374-
ndk_dir,
375-
f"{sdk_dir}/tools",
373+
f"{self.ndk_dir}/toolchains/llvm/prebuilt/{py_platform}-x86_64/bin",
374+
self.ndk_dir,
375+
f"{self.sdk_dir}/tools",
376376
environ.get("PATH"),
377377
]
378378
)

tests/test_build.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
import os
2+
import sys
13
import unittest
24
from unittest import mock
35

46
import jinja2
57

6-
from pythonforandroid.build import run_pymodules_install
8+
from pythonforandroid.build import (
9+
Context, RECOMMENDED_TARGET_API, run_pymodules_install,
10+
)
711
from pythonforandroid.archs import ArchARMv7_a, ArchAarch_64
812

913

@@ -89,3 +93,42 @@ def test_android_manifest_xml(self):
8993
assert xml.count('android:debuggable="true"') == 1
9094
assert xml.count('<service android:name="abcd" />') == 1
9195
# TODO: potentially some other checks to be added here to cover other "logic" (flags and loops) in the template
96+
97+
98+
class TestContext(unittest.TestCase):
99+
100+
@mock.patch.dict('pythonforandroid.build.Context.env')
101+
@mock.patch('pythonforandroid.build.get_available_apis')
102+
@mock.patch('pythonforandroid.build.ensure_dir')
103+
def test_sdk_ndk_paths(
104+
self,
105+
mock_ensure_dir,
106+
mock_get_available_apis,
107+
):
108+
mock_get_available_apis.return_value = [RECOMMENDED_TARGET_API]
109+
context = Context()
110+
context.setup_dirs(os.getcwd())
111+
context.prepare_build_environment(
112+
user_sdk_dir='sdk',
113+
user_ndk_dir='ndk',
114+
user_android_api=None,
115+
user_ndk_api=None,
116+
)
117+
118+
# The context was supplied with relative SDK and NDK dirs. Check
119+
# that it resolved them to absolute paths.
120+
real_sdk_dir = os.path.join(os.getcwd(), 'sdk')
121+
real_ndk_dir = os.path.join(os.getcwd(), 'ndk')
122+
assert context.sdk_dir == real_sdk_dir
123+
assert context.ndk_dir == real_ndk_dir
124+
125+
py_platform = sys.platform
126+
if py_platform in ['linux2', 'linux3']:
127+
py_platform = 'linux'
128+
129+
context_paths = context.env['PATH'].split(':')
130+
assert context_paths[0:3] == [
131+
f'{real_ndk_dir}/toolchains/llvm/prebuilt/{py_platform}-x86_64/bin',
132+
real_ndk_dir,
133+
f'{real_sdk_dir}/tools'
134+
]

tests/test_recipe.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ def test_get_recipe_env_with(
294294
# check that the mocks have been called
295295
mock_ensure_dir.assert_called()
296296
mock_find_executable.assert_called_once_with(
297-
expected_compiler, path=os.environ['PATH']
297+
expected_compiler, path=self.ctx.env['PATH']
298298
)
299299
self.assertIsInstance(env, dict)
300300

0 commit comments

Comments
 (0)