Back to index

obnam  1.1
pluginmgr_tests.py
Go to the documentation of this file.
00001 # Copyright (C) 2009  Lars Wirzenius <liw@liw.fi>
00002 # 
00003 # This program is free software: you can redistribute it and/or modify
00004 # it under the terms of the GNU General Public License as published by
00005 # the Free Software Foundation, either version 3 of the License, or
00006 # (at your option) any later version.
00007 # 
00008 # This program is distributed in the hope that it will be useful,
00009 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 # GNU General Public License for more details.
00012 # 
00013 # You should have received a copy of the GNU General Public License
00014 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
00015 
00016 
00017 import unittest
00018 
00019 from pluginmgr import Plugin, PluginManager
00020 
00021 
00022 class PluginTests(unittest.TestCase):
00023 
00024     def setUp(self):
00025         self.plugin = Plugin()
00026         
00027     def test_name_is_class_name(self):
00028         self.assertEqual(self.plugin.name, 'Plugin')
00029 
00030     def test_description_is_empty_string(self):
00031         self.assertEqual(self.plugin.description, '')
00032 
00033     def test_version_is_zeroes(self):
00034         self.assertEqual(self.plugin.version, '0.0.0')
00035 
00036     def test_required_application_version_is_zeroes(self):
00037         self.assertEqual(self.plugin.required_application_version, '0.0.0')
00038 
00039     def test_enable_raises_exception(self):
00040         self.assertRaises(Exception, self.plugin.enable)
00041 
00042     def test_disable_raises_exception(self):
00043         self.assertRaises(Exception, self.plugin.disable)
00044 
00045     def test_enable_wrapper_calls_enable(self):
00046         self.plugin.enable = lambda: setattr(self, 'enabled', True)
00047         self.plugin.enable_wrapper()
00048         self.assert_(self.enabled, True)
00049 
00050     def test_disable_wrapper_calls_disable(self):
00051         self.plugin.disable = lambda: setattr(self, 'disabled', True)
00052         self.plugin.disable_wrapper()
00053         self.assert_(self.disabled, True)
00054 
00055 
00056 class PluginManagerInitialStateTests(unittest.TestCase):
00057 
00058     def setUp(self):
00059         self.pm = PluginManager()
00060     
00061     def test_locations_is_empty_list(self):
00062         self.assertEqual(self.pm.locations, [])
00063 
00064     def test_plugins_is_empty_list(self):
00065         self.assertEqual(self.pm.plugins, [])
00066 
00067     def test_application_version_is_zeroes(self):
00068         self.assertEqual(self.pm.application_version, '0.0.0')
00069 
00070     def test_plugin_files_is_empty(self):
00071         self.assertEqual(self.pm.plugin_files, [])
00072 
00073     def test_plugin_arguments_is_empty(self):
00074         self.assertEqual(self.pm.plugin_arguments, [])
00075         
00076     def test_plugin_keyword_arguments_is_empty(self):
00077         self.assertEqual(self.pm.plugin_keyword_arguments, {})
00078 
00079 
00080 class PluginManagerTests(unittest.TestCase):
00081 
00082     def setUp(self):
00083         self.pm = PluginManager()
00084         self.pm.locations = ['test-plugins', 'not-exist']
00085         self.pm.plugin_arguments = ('fooarg',)
00086         self.pm.plugin_keyword_arguments = { 'bar': 'bararg' }
00087         
00088         self.files = sorted(['test-plugins/hello_plugin.py',
00089                              'test-plugins/aaa_hello_plugin.py',
00090                              'test-plugins/oldhello_plugin.py',
00091                              'test-plugins/wrongversion_plugin.py'])
00092 
00093     def test_finds_the_right_plugin_files(self):
00094         self.assertEqual(self.pm.find_plugin_files(), self.files)
00095 
00096     def test_plugin_files_attribute_implicitly_searches(self):
00097         self.assertEqual(self.pm.plugin_files, self.files)
00098 
00099     def test_loads_hello_plugin(self):
00100         plugins = self.pm.load_plugins()
00101         self.assertEqual(len(plugins), 1)
00102         self.assertEqual(plugins[0].name, 'Hello')
00103 
00104     def test_plugins_attribute_implicitly_searches(self):
00105         self.assertEqual(len(self.pm.plugins), 1)
00106         self.assertEqual(self.pm.plugins[0].name, 'Hello')
00107 
00108     def test_initializes_hello_with_correct_args(self):
00109         plugin = self.pm['Hello']
00110         self.assertEqual(plugin.foo, 'fooarg')
00111         self.assertEqual(plugin.bar, 'bararg')
00112 
00113     def test_raises_keyerror_for_unknown_plugin(self):
00114         self.assertRaises(KeyError, self.pm.__getitem__, 'Hithere')
00115 
00116     def test_enable_plugins_enables_all_plugins(self):
00117         enabled = set()
00118         for plugin in self.pm.plugins:
00119             plugin.enable = lambda: enabled.add(plugin)
00120         self.pm.enable_plugins()
00121         self.assertEqual(enabled, set(self.pm.plugins))
00122 
00123     def test_disable_plugins_disables_all_plugins(self):
00124         disabled = set()
00125         for plugin in self.pm.plugins:
00126             plugin.disable = lambda: disabled.add(plugin)
00127         self.pm.disable_plugins()
00128         self.assertEqual(disabled, set(self.pm.plugins))
00129 
00130 
00131 class PluginManagerCompatibleApplicationVersionTests(unittest.TestCase):
00132 
00133     def setUp(self):
00134         self.pm = PluginManager()
00135         self.pm.application_version = '1.2.3'
00136         
00137     def test_rejects_zero(self):
00138         self.assertFalse(self.pm.compatible_version('0'))
00139         
00140     def test_rejects_two(self):
00141         self.assertFalse(self.pm.compatible_version('2'))
00142         
00143     def test_rejects_one_two_four(self):
00144         self.assertFalse(self.pm.compatible_version('1.2.4'))
00145         
00146     def test_accepts_one(self):
00147         self.assert_(self.pm.compatible_version('1'))
00148         
00149     def test_accepts_one_two_three(self):
00150         self.assert_(self.pm.compatible_version('1.2.3'))
00151