vendor/CMF/1.6.3/CMFCore

view tests/test_DirectoryView.py @ 0:587011552858

import CMF 1.6.3
author bdelbosc
date Mon, 23 Apr 2007 13:58:01 +0000
parents
children
line source
1 from unittest import TestCase, TestSuite, makeSuite, main
2 import Testing
3 try:
4 import Zope2
5 except ImportError: # BBB: for Zope 2.7
6 import Zope as Zope2
7 Zope2.startup()
9 from os import remove, mkdir, rmdir
10 from os.path import join
11 from tempfile import mktemp
13 from Globals import DevelopmentMode
15 from Products.CMFCore.tests.base.dummy import DummyFolder
16 from Products.CMFCore.tests.base.testcase import _prefix
17 from Products.CMFCore.tests.base.testcase import FSDVTest
18 from Products.CMFCore.tests.base.testcase import WarningInterceptor
21 class DirectoryViewPathTests( TestCase, WarningInterceptor ):
22 """
23 These test that, no matter what is stored in their dirpath,
24 FSDV's will do their best to find an appropriate skin
25 and only do nothing in the case where an appropriate skin
26 can't be found.
27 """
29 def setUp(self):
30 self._trap_warning_output()
31 from Products.CMFCore.DirectoryView import registerDirectory
32 from Products.CMFCore.DirectoryView import addDirectoryViews
33 registerDirectory('fake_skins', _prefix)
34 self.ob = DummyFolder()
35 addDirectoryViews(self.ob, 'fake_skins', _prefix)
37 def tearDown(self):
38 self._free_warning_output()
40 def test_getDirectoryInfo(self):
41 skin = self.ob.fake_skin
42 skin.manage_properties('CMFCore/tests/fake_skins/fake_skin')
43 self.failUnless( hasattr(self.ob.fake_skin, 'test1'),
44 self.ob.fake_skin.getDirPath() )
46 # 1 - 7, in effect, test the pre CMF 1.5 backwards compatibility code in
47 # DirectoryView's __of__ method. See DirectoryView.py for details
49 # windows INSTANCE_HOME
50 def test_getDirectoryInfo1(self):
51 skin = self.ob.fake_skin
52 skin.manage_properties(r'Products\CMFCore\tests\fake_skins\fake_skin')
53 self.failUnless( hasattr(self.ob.fake_skin, 'test1'),
54 self.ob.fake_skin.getDirPath() )
56 # windows SOFTWARE_HOME
57 def test_getDirectoryInfo2(self):
58 skin = self.ob.fake_skin
59 skin.manage_properties(
60 r'C:\Zope\2.5.1\Products\CMFCore\tests\fake_skins\fake_skin')
61 self.failUnless( hasattr(self.ob.fake_skin, 'test1'),
62 self.ob.fake_skin.getDirPath() )
64 # *nix INSTANCE_HOME
65 def test_getDirectoryInfo3(self):
66 skin = self.ob.fake_skin
67 skin.manage_properties('Products/CMFCore/tests/fake_skins/fake_skin')
68 self.failUnless( hasattr(self.ob.fake_skin, 'test1'),
69 self.ob.fake_skin.getDirPath() )
71 # *nix SOFTWARE_HOME
72 def test_getDirectoryInfo4(self):
73 skin = self.ob.fake_skin
74 skin.manage_properties(
75 '/usr/local/zope/2.5.1/Products/CMFCore/tests/fake_skins/fake_skin')
76 self.failUnless( hasattr(self.ob.fake_skin, 'test1'),
77 self.ob.fake_skin.getDirPath() )
79 # windows PRODUCTS_PATH
80 def test_getDirectoryInfo5(self):
81 skin = self.ob.fake_skin
82 skin.manage_properties( mktemp() +
83 r'\Products\CMFCore\tests\fake_skins\fake_skin')
84 self.failUnless( hasattr(self.ob.fake_skin, 'test1'),
85 self.ob.fake_skin.getDirPath() )
87 # linux PRODUCTS_PATH
88 def test_getDirectoryInfo6(self):
89 skin = self.ob.fake_skin
90 skin.manage_properties( mktemp() +
91 '/Products/CMFCore/tests/fake_skins/fake_skin')
92 self.failUnless( hasattr(self.ob.fake_skin, 'test1'),
93 self.ob.fake_skin.getDirPath() )
95 # second 'Products' in path
96 def test_getDirectoryInfo7(self):
97 skin = self.ob.fake_skin
98 skin.manage_properties(
99 r'C:\CoolProducts\Zope\Products\CMFCore\tests\fake_skins\fake_skin')
100 self.failUnless( hasattr(self.ob.fake_skin, 'test1'),
101 self.ob.fake_skin.getDirPath() )
103 # Test we do nothing if given a really wacky path
104 def test_UnhandleableExpandPath( self ):
105 from tempfile import mktemp
106 self._trap_warning_output()
107 file = mktemp()
108 self.ob.fake_skin.manage_properties(file)
109 self.assertEqual(self.ob.fake_skin.objectIds(),[])
110 # Check that a warning was raised.
111 from Products.CMFCore import DirectoryView
112 warnings = [t[0] for t in DirectoryView.__warningregistry__]
113 text = 'DirectoryView fake_skin refers to a non-existing path %s' % file
114 text = text.replace('\\','/')
115 self.assert_(text in warnings)
116 self.failUnless(text in self._our_stderr_stream.getvalue())
118 def test_UnhandleableMinimalPath( self ):
119 from Products.CMFCore.utils import minimalpath, normalize
120 from tempfile import mktemp
121 weirdpath = mktemp()
122 # we need to normalize 'cos minimalpath does, btu we're not testing
123 # normalize in this unit test.
124 self.assertEqual( normalize(weirdpath), minimalpath(weirdpath) )
126 # this test tests that registerDirectory calls minimalpath correctly
127 # the only way to test this works under SOFTWARE_HOME,INSTANCE_HOME and
128 # PRODUCTS_PATH setups is to run the test in those environments
129 def test_registerDirectoryMinimalPath(self):
130 from Products.CMFCore.DirectoryView import _dirreg
131 dirs = _dirreg._directories
132 self.failUnless( dirs.has_key('CMFCore/tests/fake_skins/fake_skin'),
133 dirs.keys() )
134 self.assertEqual( self.ob.fake_skin.getDirPath(),
135 'CMFCore/tests/fake_skins/fake_skin' )
138 class DirectoryViewTests( FSDVTest ):
140 def setUp( self ):
141 FSDVTest.setUp(self)
142 self._registerDirectory(self)
144 def test_addDirectoryViews( self ):
145 # Test addDirectoryViews
146 # also test registration of directory views doesn't barf
147 pass
149 def test_DirectoryViewExists( self ):
150 # Check DirectoryView added by addDirectoryViews
151 # appears as a DirectoryViewSurrogate due
152 # to Acquisition hackery.
153 from Products.CMFCore.DirectoryView import DirectoryViewSurrogate
154 self.failUnless(isinstance(self.ob.fake_skin,DirectoryViewSurrogate))
156 def test_DirectoryViewMethod( self ):
157 # Check if DirectoryView method works
158 self.assertEqual(self.ob.fake_skin.test1(),'test1')
160 def test_properties(self):
161 # Make sure the directory view is reading properties
162 self.assertEqual(self.ob.fake_skin.testPT.title, 'Zope Pope')
164 def test_ignored(self):
165 # Test that "artifact" files and dirs are ignored
166 for name in '#test1', 'CVS', '.test1', 'test1~':
167 assert(name not in self.ob.fake_skin.objectIds(),
168 '%s not ignored' % name)
170 def test_surrogate_writethrough(self):
171 # CMF Collector 316: It is possible to cause ZODB writes because
172 # setting attributes on the non-persistent surrogate writes them
173 # into the persistent DirectoryView as well. This is bad in situations
174 # where you only want to store markers and remove them before the
175 # transaction has ended - they never got removed because there was
176 # no equivalent __delattr__ on the surrogate that would clean up
177 # the persistent DirectoryView as well.
178 fs = self.ob.fake_skin
179 test_foo = 'My Foovalue'
180 fs.foo = test_foo
182 self.assertEqual(fs.foo, test_foo)
183 self.assertEqual(fs.__dict__['_real'].foo, test_foo)
185 del fs.foo
187 self.assertRaises(AttributeError, getattr, fs, 'foo')
188 self.assertRaises( AttributeError
189 , getattr
190 , fs.__dict__['_real']
191 , 'foo'
192 )
195 class DirectoryViewIgnoreTests(FSDVTest):
197 def setUp( self ):
198 FSDVTest.setUp(self)
199 self.manual_ign = ('CVS', 'SVN', 'test_manual_ignore.py')
200 self._registerDirectory(self , ignore=self.manual_ign)
202 def test_ignored(self):
203 # Test that "artifact" files and dirs are ignored,
204 # even when a custom ignore list is used; and that the
205 # custom ignore list is also honored
206 auto_ign = ('#test1', '.test1', 'test1~')
207 must_ignore = self.manual_ign + auto_ign + ('test_manual_ignore',)
208 visible = self.ob.fake_skin.objectIds()
210 for name in must_ignore:
211 self.failIf(name in visible)
215 if DevelopmentMode:
217 class DebugModeTests( FSDVTest ):
219 def setUp( self ):
220 FSDVTest.setUp(self)
221 self.test1path = join(self.skin_path_name,'test1.py')
222 self.test2path = join(self.skin_path_name,'test2.py')
223 self.test3path = join(self.skin_path_name,'test3')
225 # initialise skins
226 self._registerDirectory(self)
228 # add a method to the fake skin folder
229 self._writeFile(self.test2path, "return 'test2'")
231 # edit the test1 method
232 self._writeFile(self.test1path, "return 'new test1'")
234 # add a new folder
235 mkdir(self.test3path)
237 def test_AddNewMethod( self ):
238 # See if a method added to the skin folder can be found
239 self.assertEqual(self.ob.fake_skin.test2(),'test2')
241 def test_EditMethod( self ):
242 # See if an edited method exhibits its new behaviour
243 self.assertEqual(self.ob.fake_skin.test1(),'new test1')
245 def test_NewFolder( self ):
246 # See if a new folder shows up
247 from Products.CMFCore.DirectoryView import DirectoryViewSurrogate
248 self.failUnless(isinstance(self.ob.fake_skin.test3,DirectoryViewSurrogate))
249 self.ob.fake_skin.test3.objectIds()
251 def test_DeleteMethod( self ):
252 # Make sure a deleted method goes away
253 remove(self.test2path)
254 self.failIf(hasattr(self.ob.fake_skin,'test2'))
256 def test_DeleteAddEditMethod( self ):
257 # Check that if we delete a method, then add it back,
258 # then edit it, the DirectoryView notices.
259 # This exercises yet another Win32 mtime weirdity.
260 remove(self.test2path)
261 self.failIf(hasattr(self.ob.fake_skin,'test2'))
263 # add method back to the fake skin folder
264 self._writeFile(self.test2path, "return 'test2.2'")
266 # check
267 self.assertEqual(self.ob.fake_skin.test2(),'test2.2')
269 # edit method
270 self._writeFile(self.test2path, "return 'test2.3'")
272 # check
273 self.assertEqual(self.ob.fake_skin.test2(),'test2.3')
275 def test_DeleteFolder( self ):
276 # Make sure a deleted folder goes away
277 rmdir(self.test3path)
278 self.failIf(hasattr(self.ob.fake_skin,'test3'))
280 else:
282 class DebugModeTests( TestCase ):
283 pass
286 def test_suite():
287 return TestSuite((
288 makeSuite(DirectoryViewPathTests),
289 makeSuite(DirectoryViewTests),
290 makeSuite(DirectoryViewIgnoreTests),
291 makeSuite(DebugModeTests),
292 ))
294 if __name__ == '__main__':
295 main(defaultTest='test_suite')