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
|
# -*- coding: utf-8; tab-width: 4; indent-tabs-mode: t; python-indent: 4 -*-
from . import test_settings
import unittest
import sys
import os
import shutil
from yapsy.AutoInstallPluginManager import AutoInstallPluginManager
class AutoInstallTestsCase(unittest.TestCase):
"""
Test the correct installation and loading of a simple plugin.
"""
def setUp(self):
"""
init
"""
# create the plugin manager
self.storing_dir = os.path.join(
os.path.dirname(os.path.abspath(__file__)),"plugins")
self.pluginManager = AutoInstallPluginManager(
self.storing_dir,
directories_list=[self.storing_dir],
plugin_info_ext="yapsy-autoinstall-plugin")
# load the plugins that may be found
self.pluginManager.collectPlugins()
# Will be used later
self.plugin_info = None
self.new_plugins_waiting_dir = os.path.join(
os.path.dirname(os.path.abspath(__file__)),"pluginstoinstall")
def tearDown(self):
"""
Clean the plugin installation directory.
"""
try:
os.remove(os.path.join(self.pluginManager.plugins_places[0],
"autoinstallplugin.yapsy-autoinstall-plugin"))
except OSError:
pass
try:
os.remove(os.path.join(self.pluginManager.plugins_places[0],
"AutoInstallPlugin.py"))
except OSError:
pass
try:
os.remove(os.path.join(self.pluginManager.plugins_places[0],
"autoinstalldirplugin.yapsy-autoinstall-plugin"))
except OSError:
pass
try:
shutil.rmtree(os.path.join(self.pluginManager.plugins_places[0],
"autoinstalldirplugin"))
except OSError:
pass
def plugin_loading_check_none(self):
"""
Test that no plugin has been loaded.
"""
# check nb of categories
self.assertEqual(len(self.pluginManager.getCategories()),1)
sole_category = self.pluginManager.getCategories()[0]
# check the number of plugins
self.assertEqual(len(self.pluginManager.getPluginsOfCategory(sole_category)),0)
def plugin_loading_check(self,new_plugin_name):
"""
Test if the correct plugin has been loaded.
"""
if self.plugin_info is None:
# check nb of categories
self.assertEqual(len(self.pluginManager.getCategories()),1)
sole_category = self.pluginManager.getCategories()[0]
# check the number of plugins
self.assertEqual(len(self.pluginManager.getPluginsOfCategory(sole_category)),1)
self.plugin_info = self.pluginManager.getPluginsOfCategory(sole_category)[0]
# test that the name of the plugin has been correctly defined
self.assertEqual(self.plugin_info.name,new_plugin_name)
self.assertEqual(sole_category,self.plugin_info.category)
else:
self.assertTrue(True)
def testGetSetInstallDir(self):
"""
Test getting and setting install dir.
"""
self.assertEqual(self.storing_dir,self.pluginManager.getInstallDir())
custom_install_dir = os.path.join("mouf", "bla")
self.pluginManager.setInstallDir(custom_install_dir)
self.assertEqual(custom_install_dir, self.pluginManager.getInstallDir())
def testNoneLoaded(self):
"""
Test if the correct plugin has been loaded.
"""
self.plugin_loading_check_none()
def testInstallFile(self):
"""
Test if the correct plugin (defined by a file) can be installed and loaded.
"""
install_success = self.pluginManager.install(self.new_plugins_waiting_dir,
"autoinstallplugin.yapsy-autoinstall-plugin")
self.assertTrue(install_success)
self.pluginManager.collectPlugins()
self.plugin_loading_check("Auto Install Plugin")
def testInstallDir(self):
"""
Test if the correct plugin (define by a directory) can be installed and loaded.
"""
install_success = self.pluginManager.install(self.new_plugins_waiting_dir,
"autoinstalldirplugin.yapsy-autoinstall-plugin")
self.assertTrue(install_success)
self.pluginManager.collectPlugins()
self.plugin_loading_check("Auto Install Dir Plugin")
def testActivationAndDeactivation(self):
"""
Test if the activation procedure works.
"""
install_success = self.pluginManager.install(self.new_plugins_waiting_dir,
"autoinstallplugin.yapsy-autoinstall-plugin")
self.assertTrue(install_success)
self.pluginManager.collectPlugins()
self.plugin_loading_check("Auto Install Plugin")
self.assertTrue(not self.plugin_info.plugin_object.is_activated)
self.pluginManager.activatePluginByName(self.plugin_info.name,
self.plugin_info.category)
self.assertTrue(self.plugin_info.plugin_object.is_activated)
self.pluginManager.deactivatePluginByName(self.plugin_info.name,
self.plugin_info.category)
self.assertTrue(not self.plugin_info.plugin_object.is_activated)
class AutoInstallZIPTestsCase(unittest.TestCase):
"""
Test the correct installation and loading of a zipped plugin.
"""
def setUp(self):
"""
init
"""
# create the plugin manager
storing_dir = os.path.join(
os.path.dirname(os.path.abspath(__file__)),"plugins")
self.pluginManager = AutoInstallPluginManager(
storing_dir,
directories_list=[storing_dir],
plugin_info_ext="yapsy-autoinstall-plugin")
# load the plugins that may be found
self.pluginManager.collectPlugins()
# Will be used later
self.plugin_info = None
self.new_plugins_waiting_dir = os.path.join(
os.path.dirname(os.path.abspath(__file__)),"pluginstoinstall")
def tearDown(self):
"""
Clean the plugin installation directory.
"""
try:
os.remove(os.path.join(self.pluginManager.plugins_places[0],
"autoinstallzipplugin.yapsy-autoinstall-plugin"))
except OSError:
pass
try:
shutil.rmtree(os.path.join(self.pluginManager.plugins_places[0],
"autoinstallzipplugin"))
except OSError:
pass
def plugin_loading_check_none(self):
"""
Test that no plugin has been loaded.
"""
# check nb of categories
self.assertEqual(len(self.pluginManager.getCategories()),1)
sole_category = self.pluginManager.getCategories()[0]
# check the number of plugins
self.assertEqual(len(self.pluginManager.getPluginsOfCategory(sole_category)),0)
def plugin_loading_check(self,new_plugin_name):
"""
Test if the correct plugin has been loaded.
"""
if self.plugin_info is None:
# check nb of categories
self.assertEqual(len(self.pluginManager.getCategories()),1)
sole_category = self.pluginManager.getCategories()[0]
# check the number of plugins
self.assertEqual(len(self.pluginManager.getPluginsOfCategory(sole_category)),1)
self.plugin_info = self.pluginManager.getPluginsOfCategory(sole_category)[0]
# test that the name of the plugin has been correctly defined
self.assertEqual(self.plugin_info.name,new_plugin_name)
self.assertEqual(sole_category,self.plugin_info.category)
else:
self.assertTrue(True)
def testNoneLoaded(self):
"""
Test if the correct plugin has been loaded.
"""
self.plugin_loading_check_none()
def testInstallZIP(self):
"""
Test if the correct plugin (define by a zip file) can be installed and loaded.
"""
test_file = os.path.join(self.new_plugins_waiting_dir,"autoinstallZIPplugin.zip")
if sys.version_info < (2, 6):
self.assertRaises(NotImplementedError,self.pluginManager.installFromZIP,test_file)
return
install_success = self.pluginManager.installFromZIP(test_file)
self.assertTrue(install_success)
self.pluginManager.collectPlugins()
self.plugin_loading_check("Auto Install ZIP Plugin")
def testInstallZIPFailOnWrongZip(self):
"""
Test if, when the zip file does not contain what is required the installation fails.
"""
test_file = os.path.join(self.new_plugins_waiting_dir,"autoinstallWRONGzipplugin.zip")
if sys.version_info < (2, 6):
self.assertRaises(NotImplementedError,self.pluginManager.installFromZIP,test_file)
return
install_success = self.pluginManager.installFromZIP(test_file)
self.assertFalse(install_success)
self.pluginManager.collectPlugins()
self.plugin_loading_check_none()
def testInstallZIPFailOnUnexistingFile(self):
"""
Test if, when the zip file is not a file.
"""
test_file = os.path.join(self.new_plugins_waiting_dir,"doesNotExists.zip")
if sys.version_info < (2, 6):
self.assertRaises(NotImplementedError,self.pluginManager.installFromZIP,test_file)
return
install_success = self.pluginManager.installFromZIP(test_file)
self.assertFalse(install_success)
self.pluginManager.collectPlugins()
self.plugin_loading_check_none()
def testInstallZIPFailOnNotAZipFile(self):
"""
Test if, when the zip file is not a valid zip.
"""
test_file = os.path.join(self.new_plugins_waiting_dir,"AutoInstallPlugin.py")
if sys.version_info < (2, 6):
self.assertRaises(NotImplementedError,self.pluginManager.installFromZIP,test_file)
return
install_success = self.pluginManager.installFromZIP(test_file)
self.assertFalse(install_success)
self.pluginManager.collectPlugins()
self.plugin_loading_check_none()
def testActivationAndDeactivation(self):
"""
Test if the activation procedure works.
"""
test_file = os.path.join(self.new_plugins_waiting_dir,"autoinstallZIPplugin.zip")
if sys.version_info < (2, 6):
self.assertRaises(NotImplementedError,self.pluginManager.installFromZIP,test_file)
return
install_success = self.pluginManager.installFromZIP(test_file)
self.assertTrue(install_success)
self.pluginManager.collectPlugins()
self.plugin_loading_check("Auto Install ZIP Plugin")
self.assertTrue(not self.plugin_info.plugin_object.is_activated)
self.pluginManager.activatePluginByName(self.plugin_info.name,
self.plugin_info.category)
self.assertTrue(self.plugin_info.plugin_object.is_activated)
self.pluginManager.deactivatePluginByName(self.plugin_info.name,
self.plugin_info.category)
self.assertTrue(not self.plugin_info.plugin_object.is_activated)
suite = unittest.TestSuite([
unittest.TestLoader().loadTestsFromTestCase(AutoInstallTestsCase),
unittest.TestLoader().loadTestsFromTestCase(AutoInstallZIPTestsCase),
])
|