Skip to content

Commit

Permalink
Increases toolchain.py test coverage
Browse files Browse the repository at this point in the history
Increases `test_create()` coverage demonstrating crash referenced in:
<kivy#1867 (comment)>
Adds `test_recipes()` checking if it prints out without crashing.
  • Loading branch information
AndreMiras committed Jul 27, 2019
1 parent e102f59 commit 30165e8
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
9 changes: 9 additions & 0 deletions pythonforandroid/toolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,15 @@ def _read_configuration():
sys.argv.append(arg)

def recipes(self, args):
"""
Prints recipes basic info, e.g.
```
python3 3.7.1
depends: ['hostpython3', 'sqlite3', 'openssl', 'libffi']
conflicts: ['python2']
optional depends: ['sqlite3', 'libffi', 'openssl']
```
"""
ctx = self.ctx
if args.compact:
print(" ".join(set(Recipe.list_recipes(ctx))))
Expand Down
54 changes: 50 additions & 4 deletions tests/test_toolchain.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import io
import sys
import pytest
import mock
Expand All @@ -13,6 +14,10 @@ def patch_argparse_print_help():
return mock.patch('argparse.ArgumentParser.print_help')


def patch_sys_stdout():
return mock.patch('sys.stdout', new_callable=io.StringIO)


def raises_system_exit():
return pytest.raises(SystemExit)

Expand Down Expand Up @@ -51,6 +56,8 @@ def test_create(self):
'create',
'--sdk-dir=/tmp/android-sdk',
'--ndk-dir=/tmp/android-ndk',
'--bootstrap=service_only',
'--requirements=python3',
'--dist-name=test_toolchain',
]
with patch_sys_argv(argv), mock.patch(
Expand All @@ -62,8 +69,11 @@ def test_create(self):
) as m_get_ndk_platform_dir, mock.patch(
'pythonforandroid.build.get_cython_path'
) as m_get_cython_path, mock.patch(
'pythonforandroid.toolchain.build_dist_from_args'
) as m_build_dist_from_args:
'pythonforandroid.toolchain.build_recipes'
) as m_build_recipes, mock.patch(
'pythonforandroid.bootstraps.service_only.'
'ServiceOnlyBootstrap.run_distribute'
) as m_run_distribute:
m_get_available_apis.return_value = [27]
m_get_toolchain_versions.return_value = (['4.9'], True)
m_get_ndk_platform_dir.return_value = (
Expand All @@ -74,16 +84,52 @@ def test_create(self):
assert m_get_toolchain_versions.call_args_list == [
mock.call('/tmp/android-ndk', mock.ANY)]
assert m_get_cython_path.call_args_list == [mock.call()]
assert m_build_dist_from_args.call_count == 1
build_order = [
'hostpython3', 'libffi', 'openssl', 'sqlite3', 'python3',
'genericndkbuild', 'setuptools', 'six', 'pyjnius', 'android',
]
python_modules = []
context = mock.ANY
project_dir = None
assert m_build_recipes.call_args_list == [
mock.call(
build_order,
python_modules,
context,
project_dir,
ignore_project_setup_py=False
)
]
assert m_run_distribute.call_args_list == [mock.call()]

def test_create_no_sdk_dir(self):
"""
The `--sdk-dir` is mandatory to `create` a distribution.
"""
argv = ['toolchain.py', 'create']
with mock.patch('sys.argv', argv), pytest.raises(
with patch_sys_argv(argv), pytest.raises(
BuildInterruptingException
) as ex_info:
ToolchainCL()
assert ex_info.value.message == (
'Android SDK dir was not specified, exiting.')

@pytest.mark.skipif(sys.version_info < (3, 0), reason="requires python3")
def test_recipes(self):
"""
Checks the `recipes` command prints out recipes information without crashing.
"""
argv = ['toolchain.py', 'recipes']
with patch_sys_argv(argv), patch_sys_stdout() as m_stdout:
ToolchainCL()
# check if we have common patterns in the output
expected_strings = (
'conflicts:',
'depends:',
'kivy',
'optional depends:',
'python3',
'sdl2',
)
for expected_string in expected_strings:
assert expected_string in m_stdout.getvalue()

0 comments on commit 30165e8

Please sign in to comment.