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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
|
# -*- coding: utf-8; tab-width: 4; indent-tabs-mode: t; python-indent: 4 -*-
from . import test_settings
import unittest
import os
from yapsy.PluginManager import PluginManager
from yapsy.IPlugin import IPlugin
from yapsy.PluginFileLocator import PluginFileLocator
from yapsy.PluginFileLocator import IPluginFileAnalyzer
from yapsy import NormalizePluginNameForModuleName
from configparser import ConfigParser
class YapsyUtils(unittest.TestCase):
def test_NormalizePluginNameForModuleName_on_ok_name(self):
self.assertEqual("moufGlop2",NormalizePluginNameForModuleName("moufGlop2"))
def test_NormalizePluginNameForModuleName_on_empty_name(self):
self.assertEqual("_",NormalizePluginNameForModuleName(""))
def test_NormalizePluginNameForModuleName_on_name_with_space(self):
self.assertEqual("mouf_glop",NormalizePluginNameForModuleName("mouf glop"))
def test_NormalizePluginNameForModuleName_on_name_with_nonalphanum(self):
self.assertEqual("mouf__glop_a_é",NormalizePluginNameForModuleName("mouf+?glop:a/é"))
class SimpleTestCase(unittest.TestCase):
"""
Test the correct loading of a simple plugin as well as basic
commands.
"""
def setUp(self):
"""
init
"""
# create the plugin manager
self.simplePluginManager = PluginManager(directories_list=[
os.path.join(
os.path.dirname(os.path.abspath(__file__)),"plugins")])
# load the plugins that may be found
self.simplePluginManager.collectPlugins()
# Will be used later
self.plugin_info = None
def plugin_loading_check(self):
"""
Test if the correct plugin has been loaded.
"""
if self.plugin_info is None:
# check nb of categories
self.assertEqual(len(self.simplePluginManager.getCategories()),1)
sole_category = self.simplePluginManager.getCategories()[0]
# check the number of plugins
self.assertEqual(len(self.simplePluginManager.getPluginsOfCategory(sole_category)),1)
self.plugin_info = self.simplePluginManager.getPluginsOfCategory(sole_category)[0]
# test that the name of the plugin has been correctly defined
self.assertEqual(self.plugin_info.name,"Simple Plugin")
self.assertEqual(sole_category,self.plugin_info.category)
else:
self.assertTrue(True)
def testLoaded(self):
"""
Test if the correct plugin has been loaded.
"""
self.plugin_loading_check()
def testGetAll(self):
"""
Test if the correct plugin has been loaded.
"""
self.plugin_loading_check()
self.assertEqual(len(self.simplePluginManager.getAllPlugins()),1)
self.assertEqual(self.simplePluginManager.getAllPlugins()[0],self.plugin_info)
def testActivationAndDeactivation(self):
"""
Test if the activation procedure works.
"""
self.plugin_loading_check()
self.assertTrue(not self.plugin_info.plugin_object.is_activated)
self.simplePluginManager.activatePluginByName(self.plugin_info.name,
self.plugin_info.category)
self.assertTrue(self.plugin_info.plugin_object.is_activated)
self.simplePluginManager.deactivatePluginByName(self.plugin_info.name,
self.plugin_info.category)
self.assertTrue(not self.plugin_info.plugin_object.is_activated)
class SimplePluginAdvancedManipulationTestsCase(unittest.TestCase):
"""
Test some advanced manipulation on the core data of a PluginManager.
"""
def testCategoryManipulation(self):
"""
Test querying, removing and adding plugins from/to a category.
"""
spm = PluginManager(directories_list=[
os.path.join(
os.path.dirname(os.path.abspath(__file__)),"plugins")])
# load the plugins that may be found
spm.collectPlugins()
# check that the getCategories works
self.assertEqual(len(spm.getCategories()),1)
sole_category = spm.getCategories()[0]
# check the getPluginsOfCategory
self.assertEqual(len(spm.getPluginsOfCategory(sole_category)),1)
plugin_info = spm.getPluginsOfCategory(sole_category)[0]
# try to remove it and check that is worked
spm.removePluginFromCategory(plugin_info,sole_category)
self.assertEqual(len(spm.getPluginsOfCategory(sole_category)),0)
# now re-add this plugin the to same category
spm.appendPluginToCategory(plugin_info,sole_category)
self.assertEqual(len(spm.getPluginsOfCategory(sole_category)),1)
def testChangingCategoriesFilter(self):
"""
Test the effect of setting a new category filer.
"""
spm = PluginManager(directories_list=[
os.path.join(
os.path.dirname(os.path.abspath(__file__)),"plugins")])
# load the plugins that may be found
spm.collectPlugins()
newCategory = "Mouf"
# Pre-requisite for the test
previousCategories = spm.getCategories()
self.assertTrue(len(previousCategories) >= 1)
self.assertTrue(newCategory not in previousCategories)
# change the category and see what's happening
spm.setCategoriesFilter({newCategory: IPlugin})
spm.collectPlugins()
for categoryName in previousCategories:
self.assertRaises(KeyError, spm.getPluginsOfCategory, categoryName)
self.assertTrue(len(spm.getPluginsOfCategory(newCategory)) >= 1)
def testCandidatesManipulation(self):
"""
Test querying, removing and adding plugins from/to the lkist
of plugins to load.
"""
spm = PluginManager(directories_list=[
os.path.join(
os.path.dirname(os.path.abspath(__file__)),"plugins")])
# locate the plugins that should be loaded
spm.locatePlugins()
# check nb of candidatesx
self.assertEqual(len(spm.getPluginCandidates()),1)
# get the description of the plugin candidate
candidate = spm.getPluginCandidates()[0]
self.assertTrue(isinstance(candidate,tuple))
# try removing the candidate
spm.removePluginCandidate(candidate)
self.assertEqual(len(spm.getPluginCandidates()),0)
# try re-adding it
spm.appendPluginCandidate(candidate)
self.assertEqual(len(spm.getPluginCandidates()),1)
def testTwoStepsLoad(self):
"""
Test loading the plugins in two steps in order to collect more
deltailed informations.
"""
spm = PluginManager(directories_list=[
os.path.join(
os.path.dirname(os.path.abspath(__file__)),"plugins")])
# trigger the first step to look up for plugins
spm.locatePlugins()
# make full use of the "feedback" the loadPlugins can give
# - set-up the callback function that will be called *before*
# loading each plugin
callback_infos = []
def preload_cbk(plugin_info):
callback_infos.append(plugin_info)
callback_after_infos = []
def postload_cbk(plugin_info):
callback_after_infos.append(plugin_info)
# - gather infos about the processed plugins (loaded or not)
loadedPlugins = spm.loadPlugins(callback=preload_cbk, callback_after=postload_cbk)
self.assertEqual(len(loadedPlugins),1)
self.assertEqual(len(callback_infos),1)
self.assertEqual(loadedPlugins[0].error,None)
self.assertEqual(loadedPlugins[0],callback_infos[0])
self.assertEqual(len(callback_after_infos),1)
self.assertEqual(loadedPlugins[0],callback_infos[0])
# check that the getCategories works
self.assertEqual(len(spm.getCategories()),1)
sole_category = spm.getCategories()[0]
# check the getPluginsOfCategory
self.assertEqual(len(spm.getPluginsOfCategory(sole_category)),1)
plugin_info = spm.getPluginsOfCategory(sole_category)[0]
# try to remove it and check that is worked
spm.removePluginFromCategory(plugin_info,sole_category)
self.assertEqual(len(spm.getPluginsOfCategory(sole_category)),0)
# now re-add this plugin the to same category
spm.appendPluginToCategory(plugin_info,sole_category)
self.assertEqual(len(spm.getPluginsOfCategory(sole_category)),1)
def testMultipleCategoriesForASamePlugin(self):
"""
Test that associating a plugin to multiple categories works as expected.
"""
class AnotherPluginIfce(object):
def __init__(self):
pass
def activate(self):
pass
def deactivate(self):
pass
spm = PluginManager(
categories_filter = {
"Default": IPlugin,
"IP": IPlugin,
"Other": AnotherPluginIfce,
},
directories_list=[
os.path.join(
os.path.dirname(os.path.abspath(__file__)),"plugins")])
# load the plugins that may be found
spm.collectPlugins()
# check that the getCategories works
self.assertEqual(len(spm.getCategories()),3)
categories = spm.getCategories()
self.assertTrue("Default" in categories)
# check the getPluginsOfCategory
self.assertEqual(len(spm.getPluginsOfCategory("Default")), 1)
plugin_info = spm.getPluginsOfCategory("Default")[0]
self.assertTrue("Default" in plugin_info.categories)
self.assertTrue("IP" in plugin_info.categories)
self.assertTrue("IP" in categories)
# check the getPluginsOfCategory
self.assertEqual(len(spm.getPluginsOfCategory("IP")),1)
self.assertTrue("Other" in categories)
# check the getPluginsOfCategory
self.assertEqual(len(spm.getPluginsOfCategory("Other")),0)
# try to remove the plugin from one category and check the
# other category
spm.removePluginFromCategory(plugin_info, "Default")
self.assertEqual(len(spm.getPluginsOfCategory("Default")), 0)
self.assertEqual(len(spm.getPluginsOfCategory("IP")), 1)
# now re-add this plugin the to same category
spm.appendPluginToCategory(plugin_info, "Default")
self.assertEqual(len(spm.getPluginsOfCategory("Default")),1)
self.assertEqual(len(spm.getPluginsOfCategory("IP")),1)
def testGetPluginOf(self):
"""
Test the plugin query function.
"""
spm = PluginManager(
categories_filter = {
"Default": IPlugin,
"IP": IPlugin,
},
directories_list=[
os.path.join(
os.path.dirname(os.path.abspath(__file__)),"plugins")])
# load the plugins that may be found
spm.collectPlugins()
# check the getPluginsOfCategory
self.assertEqual(len(spm.getPluginsOf(categories="IP")), 1)
self.assertEqual(len(spm.getPluginsOf(categories="Default")), 1)
self.assertEqual(len(spm.getPluginsOf(name="Simple Plugin")), 1)
self.assertEqual(len(spm.getPluginsOf(is_activated=False)), 1)
self.assertEqual(len(spm.getPluginsOf(categories="IP", is_activated=True)), 0)
self.assertEqual(len(spm.getPluginsOf(categories="IP", is_activated=False)), 1)
self.assertEqual(len(spm.getPluginsOf(categories="IP", pouet=False)), 0)
self.assertEqual(len(spm.getPluginsOf(categories=["IP"])), 0)
# The order in the categories are added to plugin info is random in this setup, hence the strange formula below
self.assertEqual(len(spm.getPluginsOf(categories=["IP", "Default"]) | spm.getPluginsOf(categories=["Default", "IP"])), 1)
self.assertEqual(len(spm.getPluginsOf(category="Default") | spm.getPluginsOf(category="IP")), 1)
class SimplePluginDetectionTestsCase(unittest.TestCase):
"""
Test particular aspects of plugin detection
"""
def testRecursivePluginlocation(self):
"""
Test detection of plugins which by default must be
recursive. Here we give the test directory as a plugin place
whereas we expect the plugins to be in test/plugins.
"""
spm = PluginManager(directories_list=[
os.path.dirname(os.path.abspath(__file__))])
# load the plugins that may be found
spm.collectPlugins()
# check that the getCategories works
self.assertEqual(len(spm.getCategories()),1)
sole_category = spm.getCategories()[0]
# check the getPluginsOfCategory
self.assertEqual(len(spm.getPluginsOfCategory(sole_category)),2)
def testDisablingRecursivePluginLocationIsEnforced(self):
"""
Test detection of plugins when the detection is non recursive.
Here we test that it cannot look into subdirectories of the
test directory.
"""
pluginLocator = PluginFileLocator()
pluginLocator.setPluginPlaces([
os.path.dirname(os.path.abspath(__file__))])
pluginLocator.disableRecursiveScan()
spm = PluginManager()
spm.setPluginLocator(pluginLocator)
# load the plugins that may be found
spm.collectPlugins()
# check that the getCategories works
self.assertEqual(len(spm.getCategories()),1)
sole_category = spm.getCategories()[0]
# check the getPluginsOfCategory
self.assertEqual(len(spm.getPluginsOfCategory(sole_category)),0)
def testDisablingRecursivePluginLocationAllowsFindingTopLevelPlugins(self):
"""
Test detection of plugins when the detection is non
recursive. Here we test that if we give test/plugin as the
directory to scan it can find the plugin.
"""
pluginLocator = PluginFileLocator()
pluginLocator.setPluginPlaces([
os.path.join(
os.path.dirname(os.path.abspath(__file__)),"plugins")])
pluginLocator.disableRecursiveScan()
spm = PluginManager()
spm.setPluginLocator(pluginLocator)
# load the plugins that may be found
spm.collectPlugins()
# check that the getCategories works
self.assertEqual(len(spm.getCategories()),1)
sole_category = spm.getCategories()[0]
# check the getPluginsOfCategory
self.assertEqual(len(spm.getPluginsOfCategory(sole_category)),1)
def testEnforcingPluginDirsDoesNotKeepDefaultDir(self):
"""
Test that providing the directories list override the default search directory
instead of extending the default list.
"""
class AcceptAllPluginFileAnalyzer(IPluginFileAnalyzer):
def __init__(self):
IPluginFileAnalyzer.__init__(self, "AcceptAll")
def isValidPlugin(self, filename):
return True
def getInfosDictFromPlugin(self, dirpath, filename):
return { "name": filename, "path": dirpath}, ConfigParser()
pluginLocator = PluginFileLocator()
pluginLocator.setAnalyzers([AcceptAllPluginFileAnalyzer()])
spm_default_dirs = PluginManager(plugin_locator= pluginLocator)
spm_default_dirs.locatePlugins()
candidates_in_default_dir = spm_default_dirs.getPluginCandidates()
candidates_files_in_default_dir = set([c[0] for c in candidates_in_default_dir])
pluginLocator = PluginFileLocator()
pluginLocator.setAnalyzers([AcceptAllPluginFileAnalyzer()])
spm = PluginManager(plugin_locator= pluginLocator,
directories_list=[os.path.dirname(os.path.abspath(__file__)),"does-not-exists"])
spm.locatePlugins()
candidates = spm.getPluginCandidates()
candidates_files = set([c[0] for c in candidates])
self.assertFalse(set(candidates_files_in_default_dir).issubset(set(candidates_files)))
suite = unittest.TestSuite([
unittest.TestLoader().loadTestsFromTestCase(YapsyUtils),
unittest.TestLoader().loadTestsFromTestCase(SimpleTestCase),
unittest.TestLoader().loadTestsFromTestCase(SimplePluginAdvancedManipulationTestsCase),
unittest.TestLoader().loadTestsFromTestCase(SimplePluginDetectionTestsCase),
])
|