1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
|
# Copyright 2008-2013 Jaap Karssenberg <jaap.karssenberg@gmail.com>
import tests
import os
from zim.plugins import *
from tests.mainwindow import setUpMainWindow
import zim.plugins
assert len(zim.plugins.__path__) > 1 # test __path__ magic
zim.plugins.__path__ = [os.path.abspath('./zim/plugins')] # set back default search path
class TestPluginClasses(tests.TestCase):
'''Test case to check coding and documentation of plugin classes'''
def runTest(self):
plugins = PluginManager.list_installed_plugins()
self.assertTrue(len(plugins) > 10)
self.assertTrue('spell' in plugins)
self.assertTrue('linkmap' in plugins)
pluginindex = tests.ZIM_DATA_FOLDER.file('manual/Plugins.txt').read()
seen = {
'name': set(),
'description': set(),
'help': set(),
}
for name in plugins:
#~ print('>>', name)
klass = PluginManager.get_plugin_class(name)
# test plugin info
for key in ('name', 'description', 'author'):
self.assertTrue(
klass.plugin_info.get(key),
'Plugin %s misses info field \'%s\'' % (name, key)
)
for key in ('name', 'description', 'help'):
self.assertIn(key, klass.plugin_info, 'Plugin %s missing "%s"' % (name, key))
value = klass.plugin_info[key]
self.assertFalse(
value in seen[key],
'Value for \'%s\' in %s seen before - copy-paste error ?' % (key, name)
)
seen[key].add(value)
# test manual page present and at least documents preferences
page = klass.plugin_info['help']
self.assertTrue(page.startswith('Plugins:'), 'Help page for %s not valid' % name)
rellink = "+%s" % page[8:]
self.assertIn(rellink, pluginindex, 'Missing links "%s" in manual/Plugins.txt' % rellink)
file = tests.ZIM_DATA_FOLDER.file('manual/' + page.replace(':', '/').replace(' ', '_') + '.txt')
self.assertTrue(file.exists(), 'Missing file: %s' % file)
manual = file.read()
ignore = getattr(klass, 'hide_preferences', [])
prefs = [p for p in klass.plugin_preferences if not p[0] in ignore]
props = [p for p in klass.plugin_notebook_properties]
for pref in prefs + props:
label = pref[2]
if label is not None: # else it is hidden option
if '\n' in label:
label, x = label.split('\n', 1)
label = label.rstrip(',')
self.assertTrue(label in manual, 'Preference or property "%s" for %s plugin not documented in manual page' % (label, name))
# test dependencies data
dep = klass.check_dependencies()
self.assertTrue(isinstance(dep, tuple))
check, dep = dep
self.assertTrue(isinstance(check, bool))
self.assertTrue(isinstance(dep, list))
for i in range(len(dep)):
self.assertTrue(isinstance(dep[i], tuple))
self.assertTrue(isinstance(dep[i][0], str))
self.assertTrue(isinstance(dep[i][1], bool))
self.assertTrue(isinstance(dep[i][2], bool))
class TestPluginManager(tests.TestCase):
'''Test case for TestManager infrastructure'''
def testLoadAndRemovePlugin(self):
manager = PluginManager()
self.assertEqual(len(manager), 0)
self.assertEqual(list(manager), [])
obj = manager.load_plugin('journal')
self.assertEqual(len(manager), 1)
self.assertEqual(list(manager), ['journal'])
self.assertEqual(manager['journal'], obj)
obj1 = manager.load_plugin('journal') # redundant call
self.assertEqual(obj1, obj)
self.assertEqual(len(manager), 1)
manager.remove_plugin('journal')
self.assertEqual(len(manager), 0)
self.assertEqual(list(manager), [])
self.assertRaises(KeyError, manager.__getitem__, 'journal')
manager.remove_plugin('journal') # redundant call
def testLoadNonExistingPlugin(self):
manager = PluginManager()
self.assertRaises(ImportError, manager.load_plugin, 'nonexistingplugin')
class TestPlugins(tests.TestCase):
'''Test case to initiate all (loadable) plugins and load some extensions'''
def runTest(self):
preferences = ConfigManager.get_config_dict('preferences.conf')
manager = PluginManager()
self.assertFalse(preferences.modified)
for name in PluginManager.list_installed_plugins():
klass = PluginManager.get_plugin_class(name)
if klass.check_dependencies_ok():
manager.load_plugin(name)
self.assertIn(name, manager)
self.assertFalse(preferences.modified,
'Plugin "%s" modified the preferences while loading' % name)
self.assertTrue(len(manager) > 3)
for i, name in enumerate(manager):
manager[name].preferences.emit('changed')
# Checking for exceptions and infinite recursion
self.assertTrue(i > 0)
#~ self.assertTrue(preferences.modified)
# If "False" the check while loading the plugins is not valid
# FIXME this detection is broken due to autosave in ConfigManager ...
notebook = self.setUpNotebook(content=tests.FULL_NOTEBOOK)
self.assertGreaterEqual(len(notebook.__zim_extension_objects__), 2)
# At least journal and tasklist should load
mainwindow = setUpMainWindow(notebook)
self.assertGreaterEqual(len(mainwindow.pageview.__zim_extension_objects__), 3)
# enough plugins without dependencies here
for i, name in enumerate(manager):
manager[name].preferences.emit('changed')
# Checking for exceptions and infinite recursion
loaded = list(manager)
for name in manager:
# print("REMOVE:", name)
self.assertIsInstance(manager[name], PluginClass)
manager.remove_plugin(name)
self.assertNotIn(name, manager)
self.assertEqual(len(manager), 0)
for name in sorted(loaded):
# print("LOAD AGAIN:", name)
manager.load_plugin(name)
self.assertIsInstance(manager[name], PluginClass)
self.assertEqual(len(manager), len(loaded))
class TestFunctions(tests.TestCase):
def test_find_extension(self):
class Extension(ExtensionBase):
pass
@extendable(Extension)
class Extendable(object):
pass
obj = Extendable()
ext = Extension(None, obj)
self.assertIs(find_extension(obj, Extension), ext)
with self.assertRaises(ValueError):
self.assertIs(find_extension(obj, Extendable), ext)
def test_find_action(self):
from zim.actions import action
class Extension(ExtensionBase):
@action('Bar')
def bar(self):
pass
@extendable(Extension)
class Extendable(object):
@action('Foo')
def foo(self):
pass
obj = Extendable()
ext = Extension(None, obj)
self.assertTrue(hasaction(obj, 'foo'))
self.assertTrue(hasaction(ext, 'bar'))
self.assertIsNotNone(find_action(obj, 'foo'))
self.assertIsNotNone(find_action(obj, 'bar'))
with self.assertRaises(ValueError):
find_action(obj, 'dus')
|