Back to index

moin  1.9.0~rc2
test_pysupport.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - MoinMoin.util.pysupport Tests
00004 
00005     @copyright: 2004 Oliver Graf <ograf@bitart.de>,
00006                 2007 MoinMoin:ThomasWaldmann
00007     @license: GNU GPL, see COPYING for details.
00008 """
00009 
00010 import os, errno
00011 
00012 import py
00013 
00014 from MoinMoin.util import pysupport, random_string
00015 from MoinMoin import wikiutil
00016 
00017 
00018 class TestImportNameFromMoin(object):
00019     """ Test importName of MoinMoin modules
00020 
00021     We don't make any testing for files, assuming that moin package is
00022     not broken.
00023     """
00024 
00025     def testNonExistingModule(self):
00026         """ pysupport: import nonexistent module raises ImportError """
00027         py.test.raises(ImportError, pysupport.importName,
00028                        'MoinMoin.parser.abcdefghijkl', 'Parser')
00029 
00030     def testNonExistingAttribute(self):
00031         """ pysupport: import nonexistent attritbue raises AttributeError """
00032         py.test.raises(AttributeError, pysupport.importName,
00033                        'MoinMoin.parser.text_moin_wiki', 'NoSuchParser')
00034 
00035     def testExisting(self):
00036         """ pysupport: import name from existing module """
00037         from MoinMoin.parser import text_moin_wiki
00038         Parser = pysupport.importName('MoinMoin.parser.text_moin_wiki', 'Parser')
00039         assert Parser is text_moin_wiki.Parser
00040 
00041 
00042 class TestImportNameFromPlugin(object):
00043     """ Base class for import plugin tests """
00044 
00045     name = 'Parser'
00046 
00047     def setup_method(self, method):
00048         """ Check for valid plugin package """
00049         self.pluginDirectory = os.path.join(self.request.cfg.data_dir, 'plugin', 'parser')
00050         self.checkPackage(self.pluginDirectory)
00051 
00052     def checkPackage(self, path):
00053         for item in (path, os.path.join(path, '__init__.py')):
00054             if not os.path.exists(item):
00055                 py.test.skip("Missing or wrong permissions: %s" % item)
00056 
00057     def pluginExists(self):
00058         return (os.path.exists(self.pluginFilePath('.py')) or
00059                 os.path.exists(self.pluginFilePath('.pyc')))
00060 
00061     def pluginFilePath(self, suffix):
00062         return os.path.join(self.pluginDirectory, self.plugin + suffix)
00063 
00064 
00065 class TestImportNonExisting(TestImportNameFromPlugin):
00066 
00067     plugin = 'NonExistingWikiPlugin'
00068 
00069     def testNonExisting(self):
00070         """ pysupport: import nonexistent wiki plugin fail """
00071         if self.pluginExists():
00072             py.test.skip('plugin exists: %s' % self.plugin)
00073         py.test.raises(wikiutil.PluginMissingError,
00074                        wikiutil.importWikiPlugin,
00075                            self.request.cfg, 'parser',
00076                            self.plugin, 'Parser')
00077 
00078 
00079 class TestImportExisting(TestImportNameFromPlugin):
00080 
00081     plugin = 'AutoCreatedMoinMoinTestPlugin'
00082     shouldDeleteTestPlugin = True
00083 
00084     def testExisting(self):
00085         """ pysupport: import existing wiki plugin
00086 
00087         Tests if a module can be imported from an arbitrary path
00088         like it is done in moin for plugins. Some strange bug
00089         in the old implementation failed on an import of os,
00090         cause os does a from os.path import that will stumble
00091         over a poisoned sys.modules.
00092         """
00093         try:
00094             self.createTestPlugin()
00095             # clear the plugin cache...
00096             self.request.cfg._site_plugin_lists = {}
00097             parser = wikiutil.importWikiPlugin(self.request.cfg, 'parser',
00098                                                self.plugin, 'Parser')
00099             assert getattr(parser, '__name__', None) == 'Parser'
00100             assert parser.key == self.key
00101         finally:
00102             self.deleteTestPlugin()
00103 
00104     def createTestPlugin(self):
00105         """ Create test plugin, skiping if plugin exists """
00106         if self.pluginExists():
00107             self.shouldDeleteTestPlugin = False
00108             py.test.skip("Won't overwrite existing plugin: %s" % self.plugin)
00109         self.key = random_string(32, 'abcdefg')
00110         data = '''
00111 # If you find this file in your wiki plugin directory, you can safely
00112 # delete it.
00113 import sys, os
00114 
00115 class Parser:
00116     key = '%s'
00117 ''' % self.key
00118         try:
00119             file(self.pluginFilePath('.py'), 'w').write(data)
00120         except Exception, err:
00121             py.test.skip("Can't create test plugin: %s" % str(err))
00122 
00123     def deleteTestPlugin(self):
00124         """ Delete plugin files ignoring missing files errors """
00125         if not self.shouldDeleteTestPlugin:
00126             return
00127         for suffix in ('.py', '.pyc'):
00128             try:
00129                 os.unlink(self.pluginFilePath(suffix))
00130             except OSError, err:
00131                 if err.errno != errno.ENOENT:
00132                     raise
00133 
00134 coverage_modules = ['MoinMoin.util.pysupport']