2019-02-17 17:04:09 +01:00
|
|
|
import re
|
2019-01-21 18:46:56 +01:00
|
|
|
import os
|
2019-02-17 17:04:09 +01:00
|
|
|
import codecs
|
2019-01-16 23:10:39 +01:00
|
|
|
import unittest
|
2019-01-21 18:46:56 +01:00
|
|
|
import buildozer as buildozer_module
|
2020-05-18 22:44:36 +02:00
|
|
|
from buildozer import Buildozer
|
2019-02-08 18:51:07 +01:00
|
|
|
from six import StringIO
|
2019-02-17 17:04:09 +01:00
|
|
|
import tempfile
|
2020-05-18 22:44:36 +02:00
|
|
|
from unittest import mock
|
2019-10-05 13:34:50 +02:00
|
|
|
|
2019-08-01 09:36:33 +02:00
|
|
|
from buildozer.targets.android import (
|
2019-10-05 13:34:50 +02:00
|
|
|
TargetAndroid, DEFAULT_ANDROID_NDK_VERSION, MSG_P4A_RECOMMENDED_NDK_ERROR
|
2019-08-01 09:36:33 +02:00
|
|
|
)
|
2019-03-18 02:05:55 +01:00
|
|
|
|
2019-01-16 23:10:39 +01:00
|
|
|
|
|
|
|
class TestBuildozer(unittest.TestCase):
|
|
|
|
|
2019-02-17 17:04:09 +01:00
|
|
|
def setUp(self):
|
|
|
|
"""
|
|
|
|
Creates a temporary spec file containing the content of the default.spec.
|
|
|
|
"""
|
|
|
|
self.specfile = tempfile.NamedTemporaryFile(suffix='.spec', delete=False)
|
|
|
|
self.specfilename = self.specfile.name
|
|
|
|
default_spec = codecs.open(self.default_specfile_path(), encoding='utf-8')
|
|
|
|
self.specfile.write(default_spec.read().encode('utf-8'))
|
|
|
|
self.specfile.close()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
"""
|
|
|
|
Deletes the temporary spec file.
|
|
|
|
"""
|
|
|
|
os.unlink(self.specfile.name)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def default_specfile_path():
|
|
|
|
return os.path.join(
|
|
|
|
os.path.dirname(buildozer_module.__file__),
|
|
|
|
'default.spec')
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def file_re_sub(filepath, pattern, replace):
|
|
|
|
"""
|
|
|
|
Helper method for inplace file regex editing.
|
|
|
|
"""
|
|
|
|
with open(filepath) as f:
|
|
|
|
file_content = f.read()
|
2020-04-13 17:26:17 +02:00
|
|
|
file_content = re.sub(pattern, replace, file_content)
|
2019-02-17 17:04:09 +01:00
|
|
|
with open(filepath, 'w') as f:
|
|
|
|
f.write(file_content)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def set_specfile_log_level(cls, specfilename, log_level):
|
|
|
|
"""
|
|
|
|
Helper method for setting `log_level` in a given `specfilename`.
|
|
|
|
"""
|
|
|
|
pattern = 'log_level = [0-9]'
|
|
|
|
replace = 'log_level = {}'.format(log_level)
|
|
|
|
cls.file_re_sub(specfilename, pattern, replace)
|
|
|
|
buildozer = Buildozer(specfilename)
|
2020-04-13 17:26:17 +02:00
|
|
|
assert buildozer.log_level == log_level
|
2019-02-17 17:04:09 +01:00
|
|
|
|
2019-01-16 23:10:39 +01:00
|
|
|
def test_buildozer_base(self):
|
|
|
|
"""
|
|
|
|
Basic test making sure the Buildozer object can be instanciated.
|
|
|
|
"""
|
|
|
|
buildozer = Buildozer()
|
2019-03-12 19:31:46 +01:00
|
|
|
assert buildozer.specfilename == 'buildozer.spec'
|
2019-01-21 18:46:56 +01:00
|
|
|
# spec file doesn't have to exist
|
2019-03-12 19:31:46 +01:00
|
|
|
assert os.path.exists(buildozer.specfilename) is False
|
2019-01-21 18:46:56 +01:00
|
|
|
|
|
|
|
def test_buildozer_read_spec(self):
|
|
|
|
"""
|
|
|
|
Initializes Buildozer object from existing spec file.
|
|
|
|
"""
|
2019-02-17 17:04:09 +01:00
|
|
|
buildozer = Buildozer(filename=self.default_specfile_path())
|
2019-03-12 19:31:46 +01:00
|
|
|
assert os.path.exists(buildozer.specfilename) is True
|
2019-02-08 18:51:07 +01:00
|
|
|
|
|
|
|
def test_buildozer_help(self):
|
|
|
|
"""
|
|
|
|
Makes sure the help gets display with no error, refs:
|
|
|
|
https://github.com/kivy/buildozer/issues/813
|
|
|
|
"""
|
|
|
|
buildozer = Buildozer()
|
|
|
|
with mock.patch('sys.stdout', new_callable=StringIO) as mock_stdout:
|
|
|
|
buildozer.usage()
|
2019-03-12 19:31:46 +01:00
|
|
|
assert 'Usage:' in mock_stdout.getvalue()
|
2019-02-17 17:04:09 +01:00
|
|
|
|
|
|
|
def test_log_get_set(self):
|
|
|
|
"""
|
|
|
|
Tests reading and setting log level from spec file.
|
|
|
|
"""
|
|
|
|
# the default log level value is known
|
|
|
|
buildozer = Buildozer('does_not_exist.spec')
|
|
|
|
assert buildozer.log_level == 2
|
2019-03-12 20:04:13 +01:00
|
|
|
# sets log level to 1 on the spec file
|
|
|
|
self.set_specfile_log_level(self.specfile.name, 1)
|
|
|
|
buildozer = Buildozer(self.specfile.name)
|
|
|
|
assert buildozer.log_level == 1
|
2019-02-17 17:04:09 +01:00
|
|
|
|
|
|
|
def test_log_print(self):
|
|
|
|
"""
|
|
|
|
Checks logger prints different info depending on log level.
|
|
|
|
"""
|
|
|
|
# sets log level to 1 in the spec file
|
|
|
|
self.set_specfile_log_level(self.specfile.name, 1)
|
|
|
|
buildozer = Buildozer(self.specfile.name)
|
|
|
|
assert buildozer.log_level == 1
|
|
|
|
# at this level, debug messages shouldn't not be printed
|
|
|
|
with mock.patch('sys.stdout', new_callable=StringIO) as mock_stdout:
|
|
|
|
buildozer.debug('debug message')
|
|
|
|
buildozer.info('info message')
|
|
|
|
buildozer.error('error message')
|
|
|
|
# using `in` keyword rather than `==` because of bash color prefix/suffix
|
|
|
|
assert 'debug message' not in mock_stdout.getvalue()
|
|
|
|
assert 'info message' in mock_stdout.getvalue()
|
|
|
|
assert 'error message' in mock_stdout.getvalue()
|
|
|
|
# sets log level to 2 in the spec file
|
|
|
|
self.set_specfile_log_level(self.specfile.name, 2)
|
|
|
|
buildozer = Buildozer(self.specfile.name)
|
|
|
|
assert buildozer.log_level == 2
|
|
|
|
# at this level all message types should be printed
|
|
|
|
with mock.patch('sys.stdout', new_callable=StringIO) as mock_stdout:
|
|
|
|
buildozer.debug('debug message')
|
|
|
|
buildozer.info('info message')
|
|
|
|
buildozer.error('error message')
|
|
|
|
assert 'debug message' in mock_stdout.getvalue()
|
|
|
|
assert 'info message' in mock_stdout.getvalue()
|
|
|
|
assert 'error message' in mock_stdout.getvalue()
|
2019-03-12 19:31:46 +01:00
|
|
|
|
|
|
|
def test_run_command_unknown(self):
|
|
|
|
"""
|
|
|
|
Makes sure the unknown command/target is handled gracefully, refs:
|
|
|
|
https://github.com/kivy/buildozer/issues/812
|
|
|
|
"""
|
|
|
|
buildozer = Buildozer()
|
|
|
|
command = 'foobar'
|
|
|
|
args = [command, 'debug']
|
|
|
|
with mock.patch('sys.stdout', new_callable=StringIO) as mock_stdout:
|
|
|
|
with self.assertRaises(SystemExit):
|
|
|
|
buildozer.run_command(args)
|
|
|
|
assert mock_stdout.getvalue() == 'Unknown command/target {}\n'.format(command)
|
2019-03-18 02:05:55 +01:00
|
|
|
|
|
|
|
def test_android_ant_path(self):
|
|
|
|
"""
|
|
|
|
Verify that the selected ANT path is being used from the spec file
|
|
|
|
"""
|
|
|
|
my_ant_path = '/my/ant/path'
|
|
|
|
|
|
|
|
buildozer = Buildozer(filename=self.default_specfile_path(), target='android')
|
|
|
|
buildozer.config.set('app', 'android.ant_path', my_ant_path) # Set ANT path
|
|
|
|
target = TargetAndroid(buildozer=buildozer)
|
|
|
|
|
|
|
|
# Mock first run
|
|
|
|
with mock.patch('buildozer.Buildozer.download') as download, \
|
2020-04-13 17:26:17 +02:00
|
|
|
mock.patch('buildozer.Buildozer.file_extract') as m_file_extract, \
|
2019-03-18 02:05:55 +01:00
|
|
|
mock.patch('os.makedirs'):
|
|
|
|
ant_path = target._install_apache_ant()
|
2020-04-13 17:26:17 +02:00
|
|
|
assert m_file_extract.call_args_list == [mock.call(mock.ANY, cwd='/my/ant/path')]
|
|
|
|
assert ant_path == my_ant_path
|
|
|
|
assert download.call_args_list == [
|
|
|
|
mock.call("http://archive.apache.org/dist/ant/binaries/", mock.ANY, cwd=my_ant_path)]
|
2019-03-18 02:05:55 +01:00
|
|
|
# Mock ant already installed
|
|
|
|
with mock.patch.object(Buildozer, 'file_exists', return_value=True):
|
|
|
|
ant_path = target._install_apache_ant()
|
2020-04-13 17:26:17 +02:00
|
|
|
assert ant_path == my_ant_path
|
2019-05-28 22:28:03 +02:00
|
|
|
|
|
|
|
def test_cmd_unicode_decode(self):
|
|
|
|
"""
|
|
|
|
Verifies Buildozer.cmd() can properly handle non-unicode outputs.
|
|
|
|
refs: https://github.com/kivy/buildozer/issues/857
|
|
|
|
"""
|
|
|
|
buildozer = Buildozer()
|
|
|
|
command = 'uname'
|
|
|
|
kwargs = {
|
|
|
|
'show_output': True,
|
|
|
|
'get_stdout': True,
|
|
|
|
'get_stderr': True,
|
|
|
|
}
|
|
|
|
command_output = b'\x80 cannot decode \x80'
|
|
|
|
# showing the point that we can't decode it
|
|
|
|
with self.assertRaises(UnicodeDecodeError):
|
|
|
|
command_output.decode('utf-8')
|
|
|
|
with mock.patch('buildozer.Popen') as m_popen, \
|
|
|
|
mock.patch('buildozer.select') as m_select, \
|
|
|
|
mock.patch('buildozer.stdout') as m_stdout:
|
|
|
|
m_select.select().__getitem__.return_value = [0]
|
|
|
|
# makes sure fcntl.fcntl() gets what it expects so it doesn't crash
|
|
|
|
m_popen().stdout.fileno.return_value = 0
|
|
|
|
m_popen().stderr.fileno.return_value = 2
|
|
|
|
# Buildozer.cmd() is iterating through command output "chunk" until
|
|
|
|
# one chunk is None
|
|
|
|
m_popen().stdout.read.side_effect = [command_output, None]
|
|
|
|
m_popen().returncode = 0
|
|
|
|
stdout, stderr, returncode = buildozer.cmd(command, **kwargs)
|
|
|
|
# when get_stdout is True, the command output also gets returned
|
|
|
|
assert stdout == command_output.decode('utf-8', 'ignore')
|
|
|
|
assert stderr is None
|
|
|
|
assert returncode == 0
|
|
|
|
# Python2 and Python3 have different approaches for decoding the output
|
2020-05-18 22:44:36 +02:00
|
|
|
assert m_stdout.write.call_args_list == [
|
|
|
|
mock.call(command_output.decode('utf-8', 'replace'))
|
|
|
|
]
|
2019-08-01 09:36:33 +02:00
|
|
|
|
2019-10-05 13:34:50 +02:00
|
|
|
def test_p4a_recommended_ndk_version_default_value(self):
|
2019-08-01 09:36:33 +02:00
|
|
|
self.set_specfile_log_level(self.specfile.name, 1)
|
|
|
|
buildozer = Buildozer(self.specfile.name, 'android')
|
2019-10-05 13:34:50 +02:00
|
|
|
assert buildozer.target.p4a_recommended_ndk_version is None
|
2019-08-01 09:36:33 +02:00
|
|
|
|
2019-10-05 13:34:50 +02:00
|
|
|
def test_p4a_recommended_android_ndk_error(self):
|
2019-08-01 09:36:33 +02:00
|
|
|
self.set_specfile_log_level(self.specfile.name, 1)
|
|
|
|
buildozer = Buildozer(self.specfile.name, 'android')
|
|
|
|
|
|
|
|
with mock.patch('sys.stdout', new_callable=StringIO) as mock_stdout:
|
2019-10-05 13:34:50 +02:00
|
|
|
ndk_version = buildozer.target.p4a_recommended_android_ndk
|
2019-08-01 09:36:33 +02:00
|
|
|
assert MSG_P4A_RECOMMENDED_NDK_ERROR in mock_stdout.getvalue()
|
|
|
|
# and we should get the default android's ndk version of buildozer
|
2019-10-05 13:34:50 +02:00
|
|
|
assert ndk_version == DEFAULT_ANDROID_NDK_VERSION
|
2019-08-01 09:36:33 +02:00
|
|
|
|
|
|
|
@mock.patch('buildozer.targets.android.os.path.isfile')
|
|
|
|
@mock.patch('buildozer.targets.android.os.path.exists')
|
2019-10-05 13:34:50 +02:00
|
|
|
@mock.patch('buildozer.targets.android.open', create=True)
|
|
|
|
def test_p4a_recommended_android_ndk_found(
|
2019-08-01 09:36:33 +02:00
|
|
|
self, mock_open, mock_exists, mock_isfile
|
|
|
|
):
|
|
|
|
self.set_specfile_log_level(self.specfile.name, 1)
|
|
|
|
buildozer = Buildozer(self.specfile.name, 'android')
|
|
|
|
expected_ndk = '19b'
|
2019-10-05 14:46:10 +02:00
|
|
|
recommended_line = 'RECOMMENDED_NDK_VERSION = {expected_ndk}\n'.format(
|
|
|
|
expected_ndk=expected_ndk)
|
|
|
|
mock_open.return_value = StringIO(recommended_line)
|
2019-10-05 13:34:50 +02:00
|
|
|
ndk_version = buildozer.target.p4a_recommended_android_ndk
|
|
|
|
p4a_dir = os.path.join(
|
|
|
|
buildozer.platform_dir, buildozer.target.p4a_directory_name)
|
2019-08-01 09:36:33 +02:00
|
|
|
mock_open.assert_called_once_with(
|
2019-10-05 13:34:50 +02:00
|
|
|
os.path.join(p4a_dir, "pythonforandroid", "recommendations.py"), 'r'
|
2019-08-01 09:36:33 +02:00
|
|
|
)
|
|
|
|
assert ndk_version == expected_ndk
|
|
|
|
|
|
|
|
# now test that we only read one time p4a file, so we call again to
|
2019-10-05 13:34:50 +02:00
|
|
|
# `p4a_recommended_android_ndk` and we should still have one call to `open`
|
2019-08-01 09:36:33 +02:00
|
|
|
# file, the performed above
|
2019-10-05 13:34:50 +02:00
|
|
|
ndk_version = buildozer.target.p4a_recommended_android_ndk
|
2019-08-01 09:36:33 +02:00
|
|
|
mock_open.assert_called_once()
|