plugin system: regex instead of import bases plugin parsing

This commit is contained in:
Michael Pöhn 2020-06-10 18:43:11 +02:00
parent b257a3411a
commit 77167e098e
2 changed files with 122 additions and 16 deletions

View file

@ -4,6 +4,7 @@ import inspect
import optparse
import os
import sys
import pkgutil
import textwrap
import unittest
import tempfile
@ -74,24 +75,94 @@ class MainTest(unittest.TestCase):
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')
self.assertIn('testy', plugins.keys())
self.assertEqual(plugins['testy']['summary'], 'ttt')
self.assertEqual(__import__(plugins['testy']['name'],
None,
None,
['testy'])
.main(),
'all good')
def test_main_plugin(self):
def test_main_plugin_lambda(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')"""))
test_path = sys.path.copy()
test_path.append(tmpdir)
with mock.patch('sys.path', test_path):
with TmpPyPath(tmpdir):
with mock.patch('sys.argv', ['', 'testy']):
with mock.patch('sys.exit') as exit_mock:
fdroidserver.__main__.main()
exit_mock.assert_called_once_with(0)
def test_main_plugin_def(self):
with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir):
with open('fdroid_testy.py', 'w') as f:
f.write(textwrap.dedent("""\
fdroid_summary = "ttt"
def main():
pritn('all good')"""))
with TmpPyPath(tmpdir):
with mock.patch('sys.argv', ['', 'testy']):
with mock.patch('sys.exit') as exit_mock:
fdroidserver.__main__.main()
exit_mock.assert_called_once_with(0)
def test_preparse_plugin_lookup_bad_name(self):
self.assertRaises(ValueError,
fdroidserver.__main__.preparse_plugin,
"some.package", "/non/existent/module/path")
def test_preparse_plugin_lookup_bad_path(self):
self.assertRaises(ValueError,
fdroidserver.__main__.preparse_plugin,
"fake_module_name", "/non/existent/module/path")
def test_preparse_plugin_lookup_summary_missing(self):
with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir):
with open('fdroid_testy.py', 'w') as f:
f.write(textwrap.dedent("""\
main = lambda: print('all good')"""))
with TmpPyPath(tmpdir):
p = [x for x in pkgutil.iter_modules() if x[1].startswith('fdroid_')]
module_dir = p[0][0].path
module_name = p[0][1]
self.assertRaises(NameError,
fdroidserver.__main__.preparse_plugin,
module_name, module_dir)
def test_preparse_plugin_lookup_module_file(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')"""))
with TmpPyPath(tmpdir):
p = [x for x in pkgutil.iter_modules() if x[1].startswith('fdroid_')]
module_path = p[0][0].path
module_name = p[0][1]
d = fdroidserver.__main__.preparse_plugin(module_name, module_path)
self.assertDictEqual(d, {'name': 'fdroid_testy',
'summary': 'ttt'})
def test_preparse_plugin_lookup_module_dir(self):
with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir):
os.mkdir(os.path.join(tmpdir, 'fdroid_testy'))
with open('fdroid_testy/__main__.py', 'w') as f:
f.write(textwrap.dedent("""\
fdroid_summary = "ttt"
main = lambda: print('all good')"""))
with open('fdroid_testy/__init__.py', 'w') as f:
pass
with TmpPyPath(tmpdir):
p = [x for x in pkgutil.iter_modules() if x[1].startswith('fdroid_')]
module_path = p[0][0].path
module_name = p[0][1]
d = fdroidserver.__main__.preparse_plugin(module_name, module_path)
self.assertDictEqual(d, {'name': 'fdroid_testy',
'summary': 'ttt'})
if __name__ == "__main__":
os.chdir(os.path.dirname(__file__))