build: auto-download missing NDKS if they're known and can be verified

refs #517 #717
This commit is contained in:
Hans-Christoph Steiner 2021-05-12 11:28:25 +02:00
parent 7a7ecbf9dc
commit 69fcd6a024
No known key found for this signature in database
GPG key ID: 3E177817BA1B9BFA
3 changed files with 378 additions and 11 deletions

View file

@ -12,10 +12,12 @@ import tempfile
import textwrap
import unittest
import yaml
import zipfile
from unittest import mock
localmodule = os.path.realpath(
os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..'))
os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..')
)
print('localmodule: ' + localmodule)
if localmodule not in sys.path:
sys.path.insert(0, localmodule)
@ -26,6 +28,14 @@ import fdroidserver.metadata
import fdroidserver.scanner
class FakeProcess:
output = 'fake output'
returncode = 0
def __init__(self, args, **kwargs):
print('FakeFDroidPopen', args, kwargs)
class BuildTest(unittest.TestCase):
'''fdroidserver/build.py'''
@ -94,9 +104,106 @@ class BuildTest(unittest.TestCase):
self.assertEqual(versionCode, vc)
self.assertEqual(versionName, vn)
def test_build_local_ndk(self):
"""Test if `fdroid build` detects installed NDKs and auto-installs when missing"""
testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
os.chdir(testdir)
config = {'ndk_paths': {}, 'sdk_path': tempfile.mkdtemp(prefix='android-sdk-')}
fdroidserver.common.config = config
fdroidserver.build.config = config
fdroidserver.build.options = mock.Mock()
fdroidserver.build.options.scan_binary = False
fdroidserver.build.options.notarball = True
fdroidserver.build.options.skipscan = True
app = fdroidserver.metadata.App()
app.id = 'mocked.app.id'
build = fdroidserver.metadata.Build()
build.commit = '1.0'
build.output = app.id + '.apk'
build.versionCode = '1'
build.versionName = '1.0'
build.ndk = 'r21e' # aka 21.4.7075529
vcs = mock.Mock()
def make_fake_apk(output, build):
with open(build.output, 'w') as fp:
fp.write('APK PLACEHOLDER')
return output
def fake_download_file(_ignored, local_filename):
_ignored # silence the linters
with zipfile.ZipFile(local_filename, 'x') as zipfp:
zipfp.writestr(
'android-ndk-r21e/source.properties',
'Pkg.Revision = 21.4.7075529\n',
)
# use "as _ignored" just to make a pretty layout
with mock.patch(
'fdroidserver.common.replace_build_vars', wraps=make_fake_apk
) as _ignored, mock.patch(
'fdroidserver.common.get_native_code', return_value='x86'
) as _ignored, mock.patch(
'fdroidserver.common.get_apk_id',
return_value=(app.id, build.versionCode, build.versionName),
) as _ignored, mock.patch(
'fdroidserver.common.is_apk_and_debuggable', return_value=False
) as _ignored, mock.patch(
'fdroidserver.common.sha256sum',
return_value='ad7ce5467e18d40050dc51b8e7affc3e635c85bd8c59be62de32352328ed467e',
) as _ignored, mock.patch(
'fdroidserver.common.is_apk_and_debuggable', return_value=False
) as _ignored, mock.patch(
'fdroidserver.build.FDroidPopen', FakeProcess
) as _ignored, mock.patch(
'fdroidserver.net.download_file', wraps=fake_download_file
) as _ignored:
_ignored # silence the linters
with self.assertRaises(
fdroidserver.exception.FDroidException,
msg="No NDK setup, `fdroid build` should fail with error",
):
fdroidserver.build.build_local(
app,
build,
vcs,
build_dir=testdir,
output_dir=testdir,
log_dir=None,
srclib_dir=None,
extlib_dir=None,
tmp_dir=None,
force=False,
onserver=False,
refresh=False,
)
# now run `fdroid buid --onserver`
self.assertTrue('r21e' not in config['ndk_paths'])
fdroidserver.build.build_local(
app,
build,
vcs,
build_dir=testdir,
output_dir=testdir,
log_dir=os.getcwd(),
srclib_dir=None,
extlib_dir=None,
tmp_dir=None,
force=False,
onserver=True,
refresh=False,
)
self.assertTrue(os.path.exists(config['ndk_paths']['r21e']))
def test_build_local_clean(self):
"""Test if `fdroid build` cleans ant and gradle build products"""
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir)
testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
os.chdir(testdir)
config = dict()
@ -214,14 +321,9 @@ class BuildTest(unittest.TestCase):
def test_failed_verifies_are_not_in_unsigned(self):
class FakeProcess:
output = 'fake output'
returncode = 0
def __init__(self, args, **kwargs):
print('FakeFDroidPopen', args, kwargs)
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir)
testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
os.chdir(testdir)
sdk_path = os.path.join(testdir, 'android-sdk')
self.create_fake_android_home(sdk_path)