File: test_FilterPlugin.py

package info (click to toggle)
yapsy 1.12.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 888 kB
  • sloc: python: 2,684; makefile: 82
file content (244 lines) | stat: -rw-r--r-- 8,647 bytes parent folder | download | duplicates (4)
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
# -*- coding: utf-8; tab-width: 4; indent-tabs-mode: t; python-indent: 4 -*-

from . import test_settings
from .test_settings import TEST_MESSAGE
import unittest
import os 
import re

from yapsy.FilteredPluginManager import FilteredPluginManager


class testFilter(FilteredPluginManager):
	"""
	Test filter class.
	Refused to load plugins whose Name starts with 'C'.
	"""
	_bannednames = re.compile("^C")

	def isPluginOk(self,info):
		return not self._bannednames.match(info.name)


class FilteredTestsCase(unittest.TestCase):
	"""
	Test the correct loading of a simple plugin as well as basic
	commands.
	"""
	
	def setUp(self):
		"""
		init
		"""
		# create the plugin manager
#		print os.path.join(os.path.dirname(os.path.abspath(__file__)),"plugins")
		self.filteredPluginManager = testFilter(
			directories_list=[os.path.join(
					os.path.dirname(os.path.abspath(__file__)),"plugins")],
			plugin_info_ext="yapsy-filter-plugin",
			)
		# load the plugins that may be found
		self.filteredPluginManager.collectPlugins()
		# Will be used later
		self.plugin_info = None

	def plugin_loading_check(self):
		"""
		Test if the correct plugins have been loaded.
		"""
		# check nb of categories
		self.assertEqual(len(self.filteredPluginManager.getCategories()),1)
		sole_category = self.filteredPluginManager.getCategories()[0]
		# check the number of plugins
		self.assertEqual(len(self.filteredPluginManager.getPluginsOfCategory(sole_category)),1)
		plugins = self.filteredPluginManager.getPluginsOfCategory(sole_category)
		for plugin_info in plugins:
			TEST_MESSAGE("plugin info: %s" % plugin_info)
			self.plugin_info = plugin_info	
			self.assertTrue(self.plugin_info)
			self.assertEqual(self.plugin_info.name,"Simple Plugin")
			self.assertEqual(sole_category,self.plugin_info.category)

	def testLoaded(self):
		"""
		Test if the correct plugin has been loaded.
		"""
		self.plugin_loading_check()
		

	def testActivationAndDeactivation(self):
		"""
		Test if the activation procedure works.
		"""
		self.plugin_loading_check()
		self.assertTrue(not self.plugin_info.plugin_object.is_activated)
		TEST_MESSAGE("plugin object = %s" % self.plugin_info.plugin_object)
		self.plugin_info.plugin_object.activate()
		self.assertTrue(self.plugin_info.plugin_object.is_activated)
		self.plugin_info.plugin_object.deactivate()
		self.assertTrue(not self.plugin_info.plugin_object.is_activated)	


	def testRejectedList(self):
		"""
		Test if the list of rejected plugins is correct.
		"""
		for plugin in self.filteredPluginManager.getRejectedPlugins():
			TEST_MESSAGE("plugin info: %s" % plugin[2])
			self.assertEqual(plugin[2].name,"Config Plugin")

	def testRejectedStable(self):
		reject1 = list(self.filteredPluginManager.getRejectedPlugins())
		self.filteredPluginManager.collectPlugins()
		reject2 = list(self.filteredPluginManager.getRejectedPlugins())
		self.assertEqual(len(reject1),len(reject2))


	def testRejectPlugin(self):
		self.filteredPluginManager.locatePlugins()
		rejected = self.filteredPluginManager.rejectedPlugins
		#If this fails the test in not meaningful..
		self.assertTrue(len(rejected) > 0)
		nrRejected = len(rejected)
		for plugin in rejected:
			 self.filteredPluginManager.rejectPluginCandidate(plugin)
		self.assertEqual(nrRejected,len(self.filteredPluginManager.rejectedPlugins))

	def testRemovePlugin(self):
		self.filteredPluginManager.locatePlugins()
		rejected = self.filteredPluginManager.rejectedPlugins
		nrCandidates = len(self.filteredPluginManager.getPluginCandidates())
		#If this fails the test in not meaningful..
		self.assertTrue(len(rejected) > 0)
		for plugin in rejected:
			 self.filteredPluginManager.removePluginCandidate(plugin)
		self.assertEqual(0,len(self.filteredPluginManager.rejectedPlugins))
		self.assertEqual( nrCandidates , len(self.filteredPluginManager.getPluginCandidates()))

	def testAppendRejectedPlugin(self):
		self.filteredPluginManager.locatePlugins()
		rejected = self.filteredPluginManager.getRejectedPlugins()
		nrRejected = len(rejected) 
		nrCandidates = len(self.filteredPluginManager.getPluginCandidates())

		#If this fails the test in not meaningful..
		self.assertTrue(len(rejected) > 0)
		#Remove the rejected plugins into out own list.
		for plugin in rejected:
			 self.filteredPluginManager.removePluginCandidate(plugin)
		self.assertEqual(len(self.filteredPluginManager.getRejectedPlugins()),0)

		##Now Actually test Append.
		for plugin in rejected:
			  self.filteredPluginManager.appendPluginCandidate(plugin)
		self.assertEqual(nrRejected ,len(self.filteredPluginManager.rejectedPlugins))
		self.assertEqual(nrCandidates , len(self.filteredPluginManager.getPluginCandidates()))

	def testAppendOkPlugins(self):
		self.filteredPluginManager.locatePlugins()
		rejected = self.filteredPluginManager.getRejectedPlugins()
		nrRejected = len(rejected) 
		nrCandidates = len(self.filteredPluginManager.getPluginCandidates())

		#If this fails the test in not meaningful..
		self.assertTrue(len(rejected) > 0)
		#Remove the rejected plugins again.
		for plugin in rejected:
			 self.filteredPluginManager.removePluginCandidate(plugin)
		self.assertEqual(len(self.filteredPluginManager.getRejectedPlugins()),0)

		for plugin in rejected:
			 #change the name so it is acceptable.
			 plugin[2].name = "X" + plugin[2].name[1:]
			 self.filteredPluginManager.appendPluginCandidate(plugin)
		self.assertEqual(0,len(self.filteredPluginManager.rejectedPlugins))
		self.assertEqual(nrRejected + nrCandidates , len(self.filteredPluginManager.getPluginCandidates()))

			   


	def testUnrejectPlugin(self):
		self.filteredPluginManager.locatePlugins()
		rejected = self.filteredPluginManager.rejectedPlugins
		nrRejected = len(rejected)
		nrCandidates = len(self.filteredPluginManager.getPluginCandidates())
		#If this fails the test in not meaningful..
		self.assertTrue(len(rejected) > 0)
		for plugin in rejected:
			 self.filteredPluginManager.unrejectPluginCandidate(plugin)
		self.assertEqual(0,len(self.filteredPluginManager.rejectedPlugins))
		self.assertEqual( nrRejected + nrCandidates ,
						 len(self.filteredPluginManager.getPluginCandidates()))


class FilteredWithMonkeyPathTestsCase(unittest.TestCase):
	"""
	Test the correct loading oand filtering of plugins when the FilteredPluginManager is just monkey-patched
	"""
	
	def setUp(self):
		"""
		init
		"""
		# create the plugin manager
#		print os.path.join(os.path.dirname(os.path.abspath(__file__)),"plugins")
		self.filteredPluginManager = FilteredPluginManager(
			directories_list=[os.path.join(
					os.path.dirname(os.path.abspath(__file__)),"plugins")],
			plugin_info_ext="yapsy-filter-plugin",
			)
		self.filteredPluginManager.isPluginOk = lambda info:not re.match("^C",info.name)
		# load the plugins that may be found
		self.filteredPluginManager.collectPlugins()
		# Will be used later
		self.plugin_info = None

	def plugin_loading_check(self):
		"""
		Test if the correct plugins have been loaded.
		"""
		# check nb of categories
		self.assertEqual(len(self.filteredPluginManager.getCategories()),1)
		sole_category = self.filteredPluginManager.getCategories()[0]
		# check the number of plugins
		self.assertEqual(len(self.filteredPluginManager.getPluginsOfCategory(sole_category)),1)
		plugins = self.filteredPluginManager.getPluginsOfCategory(sole_category)
		for plugin_info in plugins:
			TEST_MESSAGE("plugin info: %s" % plugin_info)
			self.plugin_info = plugin_info	
			self.assertTrue(self.plugin_info)
			self.assertEqual(self.plugin_info.name,"Simple Plugin")
			self.assertEqual(sole_category,self.plugin_info.category)

	def testLoaded(self):
		"""
		Test if the correct plugin has been loaded.
		"""
		self.plugin_loading_check()
		

	def testActivationAndDeactivation(self):
		"""
		Test if the activation procedure works.
		"""
		self.plugin_loading_check()
		self.assertTrue(not self.plugin_info.plugin_object.is_activated)
		TEST_MESSAGE("plugin object = %s" % self.plugin_info.plugin_object)
		self.plugin_info.plugin_object.activate()
		self.assertTrue(self.plugin_info.plugin_object.is_activated)
		self.plugin_info.plugin_object.deactivate()
		self.assertTrue(not self.plugin_info.plugin_object.is_activated)	


	def testRejectedList(self):
		"""
		Test if the list of rejected plugins is correct.
		"""
		for plugin in self.filteredPluginManager.getRejectedPlugins():
			TEST_MESSAGE("plugin info: %s" % plugin[2])
			self.assertEqual(plugin[2].name,"Config Plugin")

suite = unittest.TestSuite([
		unittest.TestLoader().loadTestsFromTestCase(FilteredTestsCase),
		unittest.TestLoader().loadTestsFromTestCase(FilteredWithMonkeyPathTestsCase),
		])