Skip to content

Commit 6ca4f29

Browse files
committed
fix tests
1 parent 8ec52ce commit 6ca4f29

File tree

2 files changed

+46
-21
lines changed

2 files changed

+46
-21
lines changed

pythonforandroid/build.py

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -694,32 +694,48 @@ def process_python_modules(ctx, modules):
694694
processed_modules.extend(modules)
695695

696696
if len(modules) == 0:
697-
return modules
697+
return processed_modules
698698

699699
# temp file for pip report
700700
fd, path = tempfile.mkstemp()
701701
os.close(fd)
702702

703703
# setup hostpython recipe
704-
host_recipe = Recipe.get_recipe("hostpython3", ctx)
705-
706704
env = environ.copy()
707-
_python_path = host_recipe.get_path_to_python()
708-
libdir = glob.glob(join(_python_path, "build", "lib*"))
709-
env['PYTHONPATH'] = host_recipe.site_dir + ":" + join(
710-
_python_path, "Modules") + ":" + (libdir[0] if libdir else "")
711-
712-
shprint(
713-
host_recipe.pip, 'install', *modules,
714-
'--dry-run', '--break-system-packages', '--ignore-installed',
715-
'--report', path, '-q', _env=env
716-
)
705+
try:
706+
host_recipe = Recipe.get_recipe("hostpython3", ctx)
707+
_python_path = host_recipe.get_path_to_python()
708+
libdir = glob.glob(join(_python_path, "build", "lib*"))
709+
env['PYTHONPATH'] = host_recipe.site_dir + ":" + join(
710+
_python_path, "Modules") + ":" + (libdir[0] if libdir else "")
711+
pip = host_recipe.pip
712+
except Exception:
713+
# hostpython3 non available so we use system pip (like in tests)
714+
pip = sh.Command("pip")
715+
716+
try:
717+
shprint(
718+
pip, 'install', *modules,
719+
'--dry-run', '--break-system-packages', '--ignore-installed',
720+
'--report', path, '-q', _env=env
721+
)
722+
except Exception as e:
723+
warning(f"Auto module resolution failed: {e}")
724+
return processed_modules
717725

718726
with open(path, "r") as f:
719-
report = json.load(f)
727+
try:
728+
report = json.load(f)
729+
except Exception:
730+
report = {}
720731

721732
os.remove(path)
722733

734+
if "install" not in report.keys():
735+
# pip changed json reporting format?
736+
warning("Auto module resolution failed: invalid json!")
737+
return processed_modules
738+
723739
info('Extra resolved pure python dependencies :')
724740

725741
ignored_str = " (ignored)"
@@ -740,7 +756,7 @@ def process_python_modules(ctx, modules):
740756
pure_python = False
741757

742758
# does this module matches any recipe name?
743-
if mname.lower() in _requirement_names:
759+
if mname.lower().replace("-", "_") in _requirement_names:
744760
continue
745761

746762
color = Out_Fore.GREEN if pure_python else Out_Fore.RED
@@ -777,9 +793,7 @@ def run_pymodules_install(ctx, arch, modules, project_dir=None,
777793

778794
info('*** PYTHON PACKAGE / PROJECT INSTALL STAGE FOR ARCH: {} ***'.format(arch))
779795

780-
# don't run process_python_modules in tests
781-
if ctx.recipe_build_order.__class__.__name__ != "Mock":
782-
modules = process_python_modules(ctx, modules)
796+
modules = process_python_modules(ctx, modules)
783797

784798
modules = [m for m in modules if ctx.not_has_package(m, arch)]
785799

tests/test_build.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import jinja2
66

77
from pythonforandroid.build import (
8-
Context, RECOMMENDED_TARGET_API, run_pymodules_install,
8+
Context, RECOMMENDED_TARGET_API, run_pymodules_install, process_python_modules
99
)
1010
from pythonforandroid.archs import ArchARMv7_a, ArchAarch_64
1111

@@ -17,7 +17,7 @@ def test_run_pymodules_install_optional_project_dir(self):
1717
Makes sure the `run_pymodules_install()` doesn't crash when the
1818
`project_dir` optional parameter is None, refs #1898
1919
"""
20-
ctx = mock.Mock()
20+
ctx = mock.Mock(recipe_build_order=[])
2121
ctx.archs = [ArchARMv7_a(ctx), ArchAarch_64(ctx)]
2222
modules = []
2323
project_dir = None
@@ -26,8 +26,19 @@ def test_run_pymodules_install_optional_project_dir(self):
2626
assert m_info.call_args_list[-1] == mock.call(
2727
'No Python modules and no setup.py to process, skipping')
2828

29+
def test_python_module_parser(self):
30+
ctx = mock.Mock(recipe_build_order=[])
31+
ctx.archs = [ArchARMv7_a(ctx), ArchAarch_64(ctx)]
32+
# should not alter original module name (like with adding version number)
33+
assert "kivy_garden.frostedglass" in process_python_modules(ctx, ["kivy_garden.frostedglass"])
34+
35+
# should skip urls and other unsupported format
36+
modules = ["https://example.com/some.zip", "git+https://github.com/kivy/python-for-android@develop"]
37+
result = process_python_modules(ctx, modules)
38+
assert modules == result
39+
2940
def test_strip_if_with_debug_symbols(self):
30-
ctx = mock.Mock()
41+
ctx = mock.Mock(recipe_build_order=[])
3142
ctx.python_recipe.major_minor_version_string = "3.6"
3243
ctx.get_site_packages_dir.return_value = "test-doesntexist"
3344
ctx.build_dir = "nonexistant_directory"

0 commit comments

Comments
 (0)