Skip to content

Commit 30165e8

Browse files
committed
Increases toolchain.py test coverage
Increases `test_create()` coverage demonstrating crash referenced in: <#1867 (comment)> Adds `test_recipes()` checking if it prints out without crashing.
1 parent e102f59 commit 30165e8

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

pythonforandroid/toolchain.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,15 @@ def _read_configuration():
739739
sys.argv.append(arg)
740740

741741
def recipes(self, args):
742+
"""
743+
Prints recipes basic info, e.g.
744+
```
745+
python3 3.7.1
746+
depends: ['hostpython3', 'sqlite3', 'openssl', 'libffi']
747+
conflicts: ['python2']
748+
optional depends: ['sqlite3', 'libffi', 'openssl']
749+
```
750+
"""
742751
ctx = self.ctx
743752
if args.compact:
744753
print(" ".join(set(Recipe.list_recipes(ctx))))

tests/test_toolchain.py

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import io
12
import sys
23
import pytest
34
import mock
@@ -13,6 +14,10 @@ def patch_argparse_print_help():
1314
return mock.patch('argparse.ArgumentParser.print_help')
1415

1516

17+
def patch_sys_stdout():
18+
return mock.patch('sys.stdout', new_callable=io.StringIO)
19+
20+
1621
def raises_system_exit():
1722
return pytest.raises(SystemExit)
1823

@@ -51,6 +56,8 @@ def test_create(self):
5156
'create',
5257
'--sdk-dir=/tmp/android-sdk',
5358
'--ndk-dir=/tmp/android-ndk',
59+
'--bootstrap=service_only',
60+
'--requirements=python3',
5461
'--dist-name=test_toolchain',
5562
]
5663
with patch_sys_argv(argv), mock.patch(
@@ -62,8 +69,11 @@ def test_create(self):
6269
) as m_get_ndk_platform_dir, mock.patch(
6370
'pythonforandroid.build.get_cython_path'
6471
) as m_get_cython_path, mock.patch(
65-
'pythonforandroid.toolchain.build_dist_from_args'
66-
) as m_build_dist_from_args:
72+
'pythonforandroid.toolchain.build_recipes'
73+
) as m_build_recipes, mock.patch(
74+
'pythonforandroid.bootstraps.service_only.'
75+
'ServiceOnlyBootstrap.run_distribute'
76+
) as m_run_distribute:
6777
m_get_available_apis.return_value = [27]
6878
m_get_toolchain_versions.return_value = (['4.9'], True)
6979
m_get_ndk_platform_dir.return_value = (
@@ -74,16 +84,52 @@ def test_create(self):
7484
assert m_get_toolchain_versions.call_args_list == [
7585
mock.call('/tmp/android-ndk', mock.ANY)]
7686
assert m_get_cython_path.call_args_list == [mock.call()]
77-
assert m_build_dist_from_args.call_count == 1
87+
build_order = [
88+
'hostpython3', 'libffi', 'openssl', 'sqlite3', 'python3',
89+
'genericndkbuild', 'setuptools', 'six', 'pyjnius', 'android',
90+
]
91+
python_modules = []
92+
context = mock.ANY
93+
project_dir = None
94+
assert m_build_recipes.call_args_list == [
95+
mock.call(
96+
build_order,
97+
python_modules,
98+
context,
99+
project_dir,
100+
ignore_project_setup_py=False
101+
)
102+
]
103+
assert m_run_distribute.call_args_list == [mock.call()]
78104

79105
def test_create_no_sdk_dir(self):
80106
"""
81107
The `--sdk-dir` is mandatory to `create` a distribution.
82108
"""
83109
argv = ['toolchain.py', 'create']
84-
with mock.patch('sys.argv', argv), pytest.raises(
110+
with patch_sys_argv(argv), pytest.raises(
85111
BuildInterruptingException
86112
) as ex_info:
87113
ToolchainCL()
88114
assert ex_info.value.message == (
89115
'Android SDK dir was not specified, exiting.')
116+
117+
@pytest.mark.skipif(sys.version_info < (3, 0), reason="requires python3")
118+
def test_recipes(self):
119+
"""
120+
Checks the `recipes` command prints out recipes information without crashing.
121+
"""
122+
argv = ['toolchain.py', 'recipes']
123+
with patch_sys_argv(argv), patch_sys_stdout() as m_stdout:
124+
ToolchainCL()
125+
# check if we have common patterns in the output
126+
expected_strings = (
127+
'conflicts:',
128+
'depends:',
129+
'kivy',
130+
'optional depends:',
131+
'python3',
132+
'sdl2',
133+
)
134+
for expected_string in expected_strings:
135+
assert expected_string in m_stdout.getvalue()

0 commit comments

Comments
 (0)