2016-01-04 21:17:58 +01:00
|
|
|
#!/usr/bin/env python3
|
2015-08-06 00:55:16 +02:00
|
|
|
|
|
|
|
# http://www.drdobbs.com/testing/unit-testing-with-python/240165163
|
|
|
|
|
2023-04-20 22:48:52 +02:00
|
|
|
import git
|
2017-11-30 10:14:38 +01:00
|
|
|
import logging
|
2015-08-06 00:55:16 +02:00
|
|
|
import optparse
|
2023-04-20 22:48:52 +02:00
|
|
|
import os
|
2020-01-31 23:49:50 +01:00
|
|
|
import shutil
|
2015-08-06 00:55:16 +02:00
|
|
|
import sys
|
2020-01-31 23:49:50 +01:00
|
|
|
import tempfile
|
2015-08-06 00:55:16 +02:00
|
|
|
import unittest
|
2023-04-20 22:48:52 +02:00
|
|
|
import yaml
|
2020-01-31 23:49:50 +01:00
|
|
|
from unittest import mock
|
2021-06-08 12:33:22 +02:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
import requests
|
2023-04-20 22:48:52 +02:00
|
|
|
from testcommon import TmpCwd, mkdtemp
|
2021-06-08 12:33:22 +02:00
|
|
|
|
|
|
|
localmodule = Path(__file__).resolve().parent.parent
|
|
|
|
print('localmodule: ' + str(localmodule))
|
2015-08-06 00:55:16 +02:00
|
|
|
if localmodule not in sys.path:
|
2021-06-08 12:33:22 +02:00
|
|
|
sys.path.insert(0, str(localmodule))
|
2015-08-06 00:55:16 +02:00
|
|
|
|
|
|
|
import fdroidserver.common
|
2022-08-05 19:34:28 +02:00
|
|
|
import fdroidserver.import_subcommand
|
2015-08-06 00:55:16 +02:00
|
|
|
import fdroidserver.metadata
|
2023-04-20 22:48:52 +02:00
|
|
|
from fdroidserver.exception import FDroidException
|
2021-06-07 11:49:21 +02:00
|
|
|
|
2015-08-06 00:55:16 +02:00
|
|
|
|
|
|
|
class ImportTest(unittest.TestCase):
|
|
|
|
'''fdroid import'''
|
|
|
|
|
2017-11-30 10:14:38 +01:00
|
|
|
def setUp(self):
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
2021-06-08 12:33:22 +02:00
|
|
|
self.basedir = localmodule / 'tests'
|
2022-08-05 19:34:28 +02:00
|
|
|
fdroidserver.import_subcommand.options = mock.Mock()
|
|
|
|
fdroidserver.import_subcommand.options.rev = None
|
2023-04-20 22:48:52 +02:00
|
|
|
os.chdir(self.basedir)
|
|
|
|
self._td = mkdtemp()
|
|
|
|
self.testdir = self._td.name
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
os.chdir(self.basedir)
|
|
|
|
self._td.cleanup()
|
2017-11-30 10:14:38 +01:00
|
|
|
|
2015-08-06 00:55:16 +02:00
|
|
|
def test_import_gitlab(self):
|
2022-11-22 17:17:45 +01:00
|
|
|
with tempfile.TemporaryDirectory() as testdir, TmpCwd(testdir):
|
|
|
|
# FDroidPopen needs some config to work
|
|
|
|
config = dict()
|
|
|
|
fdroidserver.common.fill_config_defaults(config)
|
|
|
|
fdroidserver.common.config = config
|
|
|
|
|
|
|
|
url = 'https://gitlab.com/fdroid/ci-test-app'
|
|
|
|
r = requests.head(url, timeout=300)
|
|
|
|
if r.status_code != 200:
|
|
|
|
print("ERROR", url, 'unreachable (', r.status_code, ')')
|
|
|
|
print('Skipping ImportTest!')
|
|
|
|
return
|
|
|
|
|
|
|
|
app = fdroidserver.import_subcommand.get_app_from_url(url)
|
|
|
|
fdroidserver.import_subcommand.clone_to_tmp_dir(app)
|
|
|
|
self.assertEqual(app.RepoType, 'git')
|
|
|
|
self.assertEqual(app.Repo, 'https://gitlab.com/fdroid/ci-test-app.git')
|
2015-08-06 00:55:16 +02:00
|
|
|
|
2020-02-03 12:39:41 +01:00
|
|
|
def test_get_app_from_url(self):
|
2022-11-22 17:17:45 +01:00
|
|
|
with tempfile.TemporaryDirectory() as testdir, TmpCwd(testdir):
|
2021-06-08 12:33:22 +02:00
|
|
|
testdir = Path(testdir)
|
|
|
|
(testdir / 'tmp').mkdir()
|
|
|
|
tmp_importer = testdir / 'tmp/importer'
|
|
|
|
data = (
|
|
|
|
(
|
|
|
|
'cn.wildfirechat.chat',
|
|
|
|
'https://github.com/wildfirechat/android-chat',
|
|
|
|
'0.6.9',
|
2022-09-14 03:45:24 +02:00
|
|
|
23,
|
2021-06-08 12:33:22 +02:00
|
|
|
),
|
|
|
|
(
|
|
|
|
'com.anpmech.launcher',
|
|
|
|
'https://github.com/KeikaiLauncher/KeikaiLauncher',
|
|
|
|
'Unknown',
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
'ut.ewh.audiometrytest',
|
|
|
|
'https://github.com/ReeceStevens/ut_ewh_audiometer_2014',
|
|
|
|
'1.65',
|
2022-09-14 03:45:24 +02:00
|
|
|
14,
|
2021-06-08 12:33:22 +02:00
|
|
|
),
|
2021-06-07 11:49:21 +02:00
|
|
|
)
|
2021-06-08 12:33:22 +02:00
|
|
|
for appid, url, vn, vc in data:
|
|
|
|
shutil.rmtree(
|
2023-02-15 20:25:48 +01:00
|
|
|
tmp_importer,
|
2022-08-06 10:21:12 +02:00
|
|
|
onerror=fdroidserver.import_subcommand.handle_retree_error_on_windows,
|
2021-06-08 12:33:22 +02:00
|
|
|
)
|
2023-02-15 20:25:48 +01:00
|
|
|
shutil.copytree(self.basedir / 'source-files' / appid, tmp_importer)
|
2021-06-08 12:33:22 +02:00
|
|
|
|
2022-08-06 10:21:12 +02:00
|
|
|
app = fdroidserver.import_subcommand.get_app_from_url(url)
|
2021-06-08 12:33:22 +02:00
|
|
|
with mock.patch(
|
|
|
|
'fdroidserver.common.getvcs',
|
|
|
|
lambda a, b, c: fdroidserver.common.vcs(url, testdir),
|
|
|
|
), mock.patch(
|
|
|
|
'fdroidserver.common.vcs.gotorevision', lambda s, rev: None
|
|
|
|
), mock.patch(
|
|
|
|
'shutil.rmtree', lambda a, onerror=None: None
|
|
|
|
):
|
2022-08-05 19:34:28 +02:00
|
|
|
build_dir = fdroidserver.import_subcommand.clone_to_tmp_dir(app)
|
2021-06-08 12:33:22 +02:00
|
|
|
self.assertEqual('git', app.RepoType)
|
|
|
|
self.assertEqual(url, app.Repo)
|
|
|
|
self.assertEqual(url, app.SourceCode)
|
|
|
|
logging.info(build_dir)
|
|
|
|
paths = fdroidserver.common.get_all_gradle_and_manifests(build_dir)
|
|
|
|
self.assertNotEqual(paths, [])
|
|
|
|
(
|
|
|
|
versionName,
|
|
|
|
versionCode,
|
|
|
|
package,
|
|
|
|
) = fdroidserver.common.parse_androidmanifests(paths, app)
|
|
|
|
self.assertEqual(vn, versionName)
|
|
|
|
self.assertEqual(vc, versionCode)
|
|
|
|
self.assertEqual(appid, package)
|
2020-01-31 23:49:50 +01:00
|
|
|
|
2022-08-06 10:21:12 +02:00
|
|
|
def test_bad_urls(self):
|
|
|
|
for url in (
|
|
|
|
'asdf',
|
|
|
|
'file://thing.git',
|
|
|
|
'https:///github.com/my/project',
|
|
|
|
'git:///so/many/slashes',
|
|
|
|
'ssh:/notabug.org/missing/a/slash',
|
|
|
|
'git:notabug.org/missing/some/slashes',
|
|
|
|
'https//github.com/bar/baz',
|
|
|
|
):
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
fdroidserver.import_subcommand.get_app_from_url(url)
|
|
|
|
|
2023-04-20 22:48:52 +02:00
|
|
|
@mock.patch('sys.argv', ['fdroid import', '-u', 'https://example.com/mystery/url'])
|
|
|
|
@mock.patch('fdroidserver.import_subcommand.clone_to_tmp_dir', lambda a: None)
|
|
|
|
def test_unrecognized_url(self):
|
|
|
|
"""Test whether error is thrown when the RepoType was not found.
|
|
|
|
|
|
|
|
clone_to_tmp_dir is mocked out to prevent this test from using
|
|
|
|
the network, if it gets past the code that throws the error.
|
|
|
|
|
|
|
|
"""
|
|
|
|
with self.assertRaises(FDroidException):
|
|
|
|
fdroidserver.import_subcommand.main()
|
|
|
|
|
|
|
|
@mock.patch('sys.argv', ['fdroid import', '-u', 'https://fake/git/url.git'])
|
|
|
|
@mock.patch('fdroidserver.import_subcommand.clone_to_tmp_dir', lambda a: Path('td'))
|
|
|
|
def test_main_local_git(self):
|
|
|
|
os.chdir(self.testdir)
|
|
|
|
git.Repo.init('td')
|
|
|
|
with Path('td/build.gradle').open('w') as fp:
|
|
|
|
fp.write('android { defaultConfig { applicationId "com.example" } }')
|
|
|
|
fdroidserver.import_subcommand.main()
|
|
|
|
with open('metadata/com.example.yml') as fp:
|
|
|
|
data = yaml.safe_load(fp)
|
|
|
|
self.assertEqual(data['Repo'], sys.argv[2])
|
|
|
|
self.assertEqual(data['RepoType'], 'git')
|
|
|
|
self.assertEqual(1, len(data['Builds']))
|
|
|
|
|
2015-08-06 00:55:16 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
parser = optparse.OptionParser()
|
2021-06-07 11:49:21 +02:00
|
|
|
parser.add_option(
|
|
|
|
"-v",
|
|
|
|
"--verbose",
|
|
|
|
action="store_true",
|
|
|
|
default=False,
|
|
|
|
help="Spew out even more information than normal",
|
|
|
|
)
|
2015-08-06 00:55:16 +02:00
|
|
|
(fdroidserver.common.options, args) = parser.parse_args(['--verbose'])
|
|
|
|
|
|
|
|
newSuite = unittest.TestSuite()
|
|
|
|
newSuite.addTest(unittest.makeSuite(ImportTest))
|
2017-10-20 11:51:59 +02:00
|
|
|
unittest.main(failfast=False)
|