fdroid-server/tests/main.TestCase

107 lines
4.0 KiB
Plaintext
Raw Normal View History

2020-01-20 23:16:34 +01:00
#!/usr/bin/env python3
import inspect
import optparse
import os
import sys
2020-01-23 04:13:14 +01:00
import textwrap
2020-01-20 23:16:34 +01:00
import unittest
2020-01-23 04:13:14 +01:00
import tempfile
2020-01-20 23:16:34 +01:00
from unittest import mock
2020-01-23 04:13:14 +01:00
from testcommon import TmpCwd, TmpPyPath
2020-01-20 23:16:34 +01:00
localmodule = os.path.realpath(
os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..'))
print('localmodule: ' + localmodule)
if localmodule not in sys.path:
sys.path.insert(0, localmodule)
from fdroidserver import common
import fdroidserver.__main__
2020-01-23 04:13:14 +01:00
class MainTest(unittest.TestCase):
2020-01-20 23:16:34 +01:00
'''this tests fdroid.py'''
def test_commands(self):
"""make sure the built in sub-command defs didn't change unintentionally"""
self.assertListEqual([x for x in fdroidserver.__main__.commands.keys()],
['build',
'init',
'publish',
'gpgsign',
'update',
'deploy',
'verify',
'checkupdates',
'import',
'install',
'readmeta',
'rewritemeta',
'lint',
'scanner',
'stats',
'server',
'signindex',
'btlog',
'signatures',
'nightly',
'mirror'])
def test_call_init(self):
co = mock.Mock()
with mock.patch('sys.argv', ['', 'init', '-h']):
with mock.patch('fdroidserver.init.main', co):
2020-01-23 04:13:14 +01:00
with mock.patch('sys.exit') as exit_mock:
2020-01-20 23:16:34 +01:00
fdroidserver.__main__.main()
2020-01-23 04:13:14 +01:00
exit_mock.assert_called_once_with(0)
2020-01-28 11:40:58 +01:00
co.assert_called_once_with()
2020-01-20 23:16:34 +01:00
def test_call_deploy(self):
co = mock.Mock()
with mock.patch('sys.argv', ['', 'deploy', '-h']):
with mock.patch('fdroidserver.server.main', co):
2020-01-23 04:13:14 +01:00
with mock.patch('sys.exit') as exit_mock:
2020-01-20 23:16:34 +01:00
fdroidserver.__main__.main()
2020-01-23 04:13:14 +01:00
exit_mock.assert_called_once_with(0)
2020-01-28 11:40:58 +01:00
co.assert_called_once_with()
2020-01-20 23:16:34 +01:00
2020-01-23 04:13:14 +01:00
def test_find_plugins(self):
with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir):
with open('fdroid_testy.py', 'w') as f:
f.write(textwrap.dedent("""\
fdroid_summary = "ttt"
main = lambda: 'all good'"""))
with TmpPyPath(tmpdir):
plugins = fdroidserver.__main__.find_plugins()
self.assertIn('testy', plugins.keys())
self.assertEqual(plugins['testy']['summary'], 'ttt')
self.assertEqual(plugins['testy']['module'].main(), 'all good')
def test_main_plugin(self):
with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir):
with open('fdroid_testy.py', 'w') as f:
f.write(textwrap.dedent("""\
fdroid_summary = "ttt"
main = lambda: pritn('all good')"""))
2020-01-29 13:08:43 +01:00
test_path = sys.path.copy()
test_path.append(tmpdir)
with mock.patch('sys.path', test_path):
with mock.patch('sys.argv', ['', 'testy']):
with mock.patch('sys.exit') as exit_mock:
fdroidserver.__main__.main()
exit_mock.assert_called_once_with(0)
2020-01-23 04:13:14 +01:00
2020-01-20 23:16:34 +01:00
if __name__ == "__main__":
os.chdir(os.path.dirname(__file__))
parser = optparse.OptionParser()
parser.add_option("-v", "--verbose", action="store_true", default=False,
help="Spew out even more information than normal")
(common.options, args) = parser.parse_args(['--verbose'])
newSuite = unittest.TestSuite()
2020-01-23 04:13:14 +01:00
newSuite.addTest(unittest.makeSuite(MainTest))
2020-01-20 23:16:34 +01:00
unittest.main(failfast=False)