<s> """<STR_LIT>""" <EOL> import sys , errno , os , time <EOL> import compileall <EOL> from zope . interface import Interface <EOL> from twisted . trial import unittest <EOL> from twisted . python . filepath import FilePath <EOL> from twisted . python . util import mergeFunctionMetadata <EOL> from twisted import plugin <EOL> class ITestPlugin ( Interface ) : <EOL> """<STR_LIT>""" <EOL> class ITestPlugin2 ( Interface ) : <EOL> """<STR_LIT>""" <EOL> class PluginTestCase ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> self . originalPath = sys . path [ : ] <EOL> self . savedModules = sys . modules . copy ( ) <EOL> self . root = FilePath ( self . mktemp ( ) ) <EOL> self . root . createDirectory ( ) <EOL> self . package = self . root . child ( '<STR_LIT>' ) <EOL> self . package . createDirectory ( ) <EOL> self . package . child ( '<STR_LIT>' ) . setContent ( "<STR_LIT>" ) <EOL> FilePath ( __file__ ) . sibling ( '<STR_LIT>' <EOL> ) . copyTo ( self . package . child ( '<STR_LIT>' ) ) <EOL> self . originalPlugin = "<STR_LIT>" <EOL> sys . path . insert ( <NUM_LIT:0> , self . root . path ) <EOL> import mypackage <EOL> self . module = mypackage <EOL> def tearDown ( self ) : <EOL> """<STR_LIT>""" <EOL> sys . path [ : ] = self . originalPath <EOL> sys . modules . clear ( ) <EOL> sys . modules . update ( self . savedModules ) <EOL> def _unimportPythonModule ( self , module , deleteSource = False ) : <EOL> modulePath = module . __name__ . split ( '<STR_LIT:.>' ) <EOL> packageName = '<STR_LIT:.>' . join ( modulePath [ : - <NUM_LIT:1> ] ) <EOL> moduleName = modulePath [ - <NUM_LIT:1> ] <EOL> delattr ( sys . modules [ packageName ] , moduleName ) <EOL> del sys . modules [ module . __name__ ] <EOL> for ext in [ '<STR_LIT:c>' , '<STR_LIT:o>' ] + ( deleteSource and [ '<STR_LIT>' ] or [ ] ) : <EOL> try : <EOL> os . remove ( module . __file__ + ext ) <EOL> except OSError , ose : <EOL> if ose . errno != errno . ENOENT : <EOL> raise <EOL> def _clearCache ( self ) : <EOL> """<STR_LIT>""" <EOL> self . package . child ( '<STR_LIT>' ) . remove ( ) <EOL> def _withCacheness ( meth ) : <EOL> """<STR_LIT>""" <EOL> def wrapped ( self ) : <EOL> meth ( self ) <EOL> meth ( self ) <EOL> self . _clearCache ( ) <EOL> meth ( self ) <EOL> meth ( self ) <EOL> return mergeFunctionMetadata ( meth , wrapped ) <EOL> def test_cache ( self ) : <EOL> """<STR_LIT>""" <EOL> cache = plugin . getCache ( self . module ) <EOL> dropin = cache [ self . originalPlugin ] <EOL> self . assertEquals ( dropin . moduleName , <EOL> '<STR_LIT>' % ( self . originalPlugin , ) ) <EOL> self . assertIn ( "<STR_LIT>" , dropin . description ) <EOL> p1 = [ p for p in dropin . plugins if ITestPlugin in p . provided ] [ <NUM_LIT:0> ] <EOL> self . assertIdentical ( p1 . dropin , dropin ) <EOL> self . assertEquals ( p1 . name , "<STR_LIT>" ) <EOL> self . assertEquals ( <EOL> p1 . description . strip ( ) , <EOL> "<STR_LIT>" ) <EOL> self . assertEquals ( p1 . provided , [ ITestPlugin , plugin . IPlugin ] ) <EOL> realPlugin = p1 . load ( ) <EOL> self . assertIdentical ( <EOL> realPlugin , <EOL> sys . modules [ '<STR_LIT>' % ( self . originalPlugin , ) ] . TestPlugin ) <EOL> import mypackage . testplugin as tp <EOL> self . assertIdentical ( realPlugin , tp . TestPlugin ) <EOL> test_cache = _withCacheness ( test_cache ) <EOL> def test_plugins ( self ) : <EOL> """<STR_LIT>""" <EOL> plugins = list ( plugin . getPlugins ( ITestPlugin2 , self . module ) ) <EOL> self . assertEquals ( len ( plugins ) , <NUM_LIT:2> ) <EOL> names = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> for p in plugins : <EOL> names . remove ( p . __name__ ) <EOL> p . test ( ) <EOL> test_plugins = _withCacheness ( test_plugins ) <EOL> def test_detectNewFiles ( self ) : <EOL> """<STR_LIT>""" <EOL> FilePath ( __file__ ) . sibling ( '<STR_LIT>' <EOL> ) . copyTo ( self . package . child ( '<STR_LIT>' ) ) <EOL> try : <EOL> self . failIfIn ( '<STR_LIT>' , sys . modules ) <EOL> self . failIf ( hasattr ( sys . modules [ '<STR_LIT>' ] , '<STR_LIT>' ) , <EOL> "<STR_LIT>" ) <EOL> plgs = list ( plugin . getPlugins ( ITestPlugin , self . module ) ) <EOL> self . assertEquals ( len ( plgs ) , <NUM_LIT:2> ) <EOL> names = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> for p in plgs : <EOL> names . remove ( p . __name__ ) <EOL> p . test1 ( ) <EOL> finally : <EOL> self . _unimportPythonModule ( <EOL> sys . modules [ '<STR_LIT>' ] , <EOL> True ) <EOL> test_detectNewFiles = _withCacheness ( test_detectNewFiles ) <EOL> def test_detectFilesChanged ( self ) : <EOL> """<STR_LIT>""" <EOL> FilePath ( __file__ ) . sibling ( '<STR_LIT>' <EOL> ) . copyTo ( self . package . child ( '<STR_LIT>' ) ) <EOL> try : <EOL> plgs = list ( plugin . getPlugins ( ITestPlugin , self . module ) ) <EOL> self . assertEquals ( len ( plgs ) , <NUM_LIT:2> ) <EOL> FilePath ( __file__ ) . sibling ( '<STR_LIT>' <EOL> ) . copyTo ( self . package . child ( '<STR_LIT>' ) ) <EOL> self . _unimportPythonModule ( sys . modules [ '<STR_LIT>' ] ) <EOL> plgs = list ( plugin . getPlugins ( ITestPlugin , self . module ) ) <EOL> self . assertEquals ( len ( plgs ) , <NUM_LIT:3> ) <EOL> names = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> for p in plgs : <EOL> names . remove ( p . __name__ ) <EOL> p . test1 ( ) <EOL> finally : <EOL> self . _unimportPythonModule ( <EOL> sys . modules [ '<STR_LIT>' ] , <EOL> True ) <EOL> test_detectFilesChanged = _withCacheness ( test_detectFilesChanged ) <EOL> def test_detectFilesRemoved ( self ) : <EOL> """<STR_LIT>""" <EOL> FilePath ( __file__ ) . sibling ( '<STR_LIT>' <EOL> ) . copyTo ( self . package . child ( '<STR_LIT>' ) ) <EOL> try : <EOL> list ( plugin . getPlugins ( ITestPlugin , self . module ) ) <EOL> finally : <EOL> self . _unimportPythonModule ( <EOL> sys . modules [ '<STR_LIT>' ] , <EOL> True ) <EOL> plgs = list ( plugin . getPlugins ( ITestPlugin , self . module ) ) <EOL> self . assertEquals ( <NUM_LIT:1> , len ( plgs ) ) <EOL> test_detectFilesRemoved = _withCacheness ( test_detectFilesRemoved ) <EOL> def test_nonexistentPathEntry ( self ) : <EOL> """<STR_LIT>""" <EOL> path = self . mktemp ( ) <EOL> self . failIf ( os . path . exists ( path ) ) <EOL> self . module . __path__ . append ( path ) <EOL> try : <EOL> plgs = list ( plugin . getPlugins ( ITestPlugin , self . module ) ) <EOL> self . assertEqual ( len ( plgs ) , <NUM_LIT:1> ) <EOL> finally : <EOL> self . module . __path__ . remove ( path ) <EOL> test_nonexistentPathEntry = _withCacheness ( test_nonexistentPathEntry ) <EOL> def test_nonDirectoryChildEntry ( self ) : <EOL> """<STR_LIT>""" <EOL> path = FilePath ( self . mktemp ( ) ) <EOL> self . failIf ( path . exists ( ) ) <EOL> path . touch ( ) <EOL> child = path . child ( "<STR_LIT>" ) . path <EOL> self . module . __path__ . append ( child ) <EOL> try : <EOL> plgs = list ( plugin . getPlugins ( ITestPlugin , self . module ) ) <EOL> self . assertEqual ( len ( plgs ) , <NUM_LIT:1> ) <EOL> finally : <EOL> self . module . __path__ . remove ( child ) <EOL> test_nonDirectoryChildEntry = _withCacheness ( test_nonDirectoryChildEntry ) <EOL> def test_deployedMode ( self ) : <EOL> """<STR_LIT>""" <EOL> plugin . getCache ( self . module ) <EOL> FilePath ( __file__ ) . sibling ( '<STR_LIT>' <EOL> ) . copyTo ( self . package . child ( '<STR_LIT>' ) ) <EOL> os . chmod ( self . package . path , <NUM_LIT:0> <NUM_LIT> ) <EOL> os . chmod ( self . package . child ( '<STR_LIT>' ) . path , <NUM_LIT:0> <NUM_LIT> ) <EOL> self . addCleanup ( os . chmod , self . package . path , <NUM_LIT:0> <NUM_LIT> ) <EOL> self . addCleanup ( os . chmod , <EOL> self . package . child ( '<STR_LIT>' ) . path , <NUM_LIT:0> <NUM_LIT> ) <EOL> cache = plugin . getCache ( self . module ) <EOL> self . assertIn ( '<STR_LIT>' , cache ) <EOL> self . assertIn ( self . originalPlugin , cache ) <EOL> errors = self . flushLoggedErrors ( ) <EOL> self . assertEquals ( len ( errors ) , <NUM_LIT:1> ) <EOL> errors [ <NUM_LIT:0> ] . trap ( OSError , IOError ) <EOL> pluginInitFile = """<STR_LIT>""" <EOL> def pluginFileContents ( name ) : <EOL> return ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT:\n>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) % ( name , ) <EOL> def _createPluginDummy ( entrypath , pluginContent , real , pluginModule ) : <EOL> """<STR_LIT>""" <EOL> entrypath . createDirectory ( ) <EOL> pkg = entrypath . child ( '<STR_LIT>' ) <EOL> pkg . createDirectory ( ) <EOL> if real : <EOL> pkg . child ( '<STR_LIT>' ) . setContent ( '<STR_LIT>' ) <EOL> plugs = pkg . child ( '<STR_LIT>' ) <EOL> plugs . createDirectory ( ) <EOL> if real : <EOL> plugs . child ( '<STR_LIT>' ) . setContent ( pluginInitFile ) <EOL> plugs . child ( pluginModule + '<STR_LIT>' ) . setContent ( pluginContent ) <EOL> return plugs <EOL> class DeveloperSetupTests ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> self . savedPath = sys . path [ : ] <EOL> self . savedModules = sys . modules . copy ( ) <EOL> self . fakeRoot = FilePath ( self . mktemp ( ) ) <EOL> self . fakeRoot . createDirectory ( ) <EOL> self . systemPath = self . fakeRoot . child ( '<STR_LIT>' ) <EOL> self . devPath = self . fakeRoot . child ( '<STR_LIT>' ) <EOL> self . appPath = self . fakeRoot . child ( '<STR_LIT>' ) <EOL> self . systemPackage = _createPluginDummy ( <EOL> self . systemPath , pluginFileContents ( '<STR_LIT>' ) , <EOL> True , '<STR_LIT>' ) <EOL> self . devPackage = _createPluginDummy ( <EOL> self . devPath , pluginFileContents ( '<STR_LIT>' ) , <EOL> True , '<STR_LIT>' ) <EOL> self . appPackage = _createPluginDummy ( <EOL> self . appPath , pluginFileContents ( '<STR_LIT>' ) , <EOL> False , '<STR_LIT>' ) <EOL> sys . path . extend ( [ x . path for x in [ self . systemPath , <EOL> self . appPath ] ] ) <EOL> self . getAllPlugins ( ) <EOL> self . sysplug = self . systemPath . child ( '<STR_LIT>' ) . child ( '<STR_LIT>' ) <EOL> self . syscache = self . sysplug . child ( '<STR_LIT>' ) <EOL> now = time . time ( ) <EOL> os . utime ( <EOL> self . sysplug . child ( '<STR_LIT>' ) . path , <EOL> ( now - <NUM_LIT> , ) * <NUM_LIT:2> ) <EOL> os . utime ( self . syscache . path , ( now - <NUM_LIT> , ) * <NUM_LIT:2> ) <EOL> self . lockSystem ( ) <EOL> self . resetEnvironment ( ) <EOL> def lockSystem ( self ) : <EOL> """<STR_LIT>""" <EOL> os . chmod ( self . sysplug . path , <NUM_LIT:0> <NUM_LIT> ) <EOL> os . chmod ( self . syscache . path , <NUM_LIT:0> <NUM_LIT> ) <EOL> def unlockSystem ( self ) : <EOL> """<STR_LIT>""" <EOL> os . chmod ( self . sysplug . path , <NUM_LIT:0> <NUM_LIT> ) <EOL> os . chmod ( self . syscache . path , <NUM_LIT:0> <NUM_LIT> ) <EOL> def getAllPlugins ( self ) : <EOL> """<STR_LIT>""" <EOL> import plugindummy . plugins <EOL> x = list ( plugin . getPlugins ( ITestPlugin , plugindummy . plugins ) ) <EOL> return [ plug . __name__ for plug in x ] <EOL> def resetEnvironment ( self ) : <EOL> """<STR_LIT>""" <EOL> self . unsetEnvironment ( ) <EOL> sys . path . extend ( [ x . path for x in [ self . devPath , <EOL> self . systemPath , <EOL> self . appPath ] ] ) <EOL> def unsetEnvironment ( self ) : <EOL> """<STR_LIT>""" <EOL> sys . modules . clear ( ) <EOL> sys . modules . update ( self . savedModules ) <EOL> sys . path [ : ] = self . savedPath <EOL> def tearDown ( self ) : <EOL> """<STR_LIT>""" <EOL> self . unsetEnvironment ( ) <EOL> self . unlockSystem ( ) <EOL> def test_developmentPluginAvailability ( self ) : <EOL> """<STR_LIT>""" <EOL> for x in range ( <NUM_LIT:3> ) : <EOL> names = self . getAllPlugins ( ) <EOL> names . sort ( ) <EOL> self . assertEqual ( names , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_freshPyReplacesStalePyc ( self ) : <EOL> """<STR_LIT>""" <EOL> mypath = self . appPackage . child ( "<STR_LIT>" ) <EOL> mypath . setContent ( pluginFileContents ( '<STR_LIT>' ) ) <EOL> x = time . time ( ) - <NUM_LIT:1000> <EOL> os . utime ( mypath . path , ( x , x ) ) <EOL> pyc = mypath . sibling ( '<STR_LIT>' ) <EOL> compileall . compile_dir ( self . appPackage . path , quiet = <NUM_LIT:1> ) <EOL> os . utime ( pyc . path , ( x , x ) ) <EOL> mypath . remove ( ) <EOL> self . resetEnvironment ( ) <EOL> self . assertIn ( '<STR_LIT>' , self . getAllPlugins ( ) ) <EOL> self . failIfIn ( '<STR_LIT>' , self . getAllPlugins ( ) ) <EOL> self . resetEnvironment ( ) <EOL> mypath . setContent ( pluginFileContents ( '<STR_LIT>' ) ) <EOL> self . failIfIn ( '<STR_LIT>' , self . getAllPlugins ( ) ) <EOL> self . assertIn ( '<STR_LIT>' , self . getAllPlugins ( ) ) <EOL> def test_newPluginsOnReadOnlyPath ( self ) : <EOL> """<STR_LIT>""" <EOL> self . unlockSystem ( ) <EOL> self . sysplug . child ( '<STR_LIT>' ) . setContent ( pluginFileContents ( '<STR_LIT>' ) ) <EOL> self . lockSystem ( ) <EOL> sys . path . remove ( self . devPath . path ) <EOL> self . assertEqual ( len ( self . flushLoggedErrors ( ) ) , <NUM_LIT:0> ) <EOL> self . assertIn ( '<STR_LIT>' , self . getAllPlugins ( ) ) <EOL> self . assertEqual ( len ( self . flushLoggedErrors ( ) ) , <NUM_LIT:1> ) <EOL> class AdjacentPackageTests ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> self . originalPath = sys . path [ : ] <EOL> self . savedModules = sys . modules . copy ( ) <EOL> def tearDown ( self ) : <EOL> """<STR_LIT>""" <EOL> sys . path [ : ] = self . originalPath <EOL> sys . modules . clear ( ) <EOL> sys . modules . update ( self . savedModules ) <EOL> def createDummyPackage ( self , root , name , pluginName ) : <EOL> """<STR_LIT>""" <EOL> directory = root . child ( name ) <EOL> package = directory . child ( '<STR_LIT>' ) <EOL> package . makedirs ( ) <EOL> package . child ( '<STR_LIT>' ) . setContent ( '<STR_LIT>' ) <EOL> plugins = package . child ( '<STR_LIT>' ) <EOL> plugins . makedirs ( ) <EOL> plugins . child ( '<STR_LIT>' ) . setContent ( pluginInitFile ) <EOL> pluginModule = plugins . child ( pluginName + '<STR_LIT>' ) <EOL> pluginModule . setContent ( pluginFileContents ( name ) ) <EOL> return directory <EOL> def test_hiddenPackageSamePluginModuleNameObscured ( self ) : <EOL> """<STR_LIT>""" <EOL> root = FilePath ( self . mktemp ( ) ) <EOL> root . makedirs ( ) <EOL> firstDirectory = self . createDummyPackage ( root , '<STR_LIT>' , '<STR_LIT>' ) <EOL> secondDirectory = self . createDummyPackage ( root , '<STR_LIT>' , '<STR_LIT>' ) <EOL> sys . path . append ( firstDirectory . path ) <EOL> sys . path . append ( secondDirectory . path ) <EOL> import dummy . plugins <EOL> plugins = list ( plugin . getPlugins ( ITestPlugin , dummy . plugins ) ) <EOL> self . assertEqual ( [ '<STR_LIT>' ] , [ p . __name__ for p in plugins ] ) <EOL> def test_hiddenPackageDifferentPluginModuleNameObscured ( self ) : <EOL> """<STR_LIT>""" <EOL> root = FilePath ( self . mktemp ( ) ) <EOL> root . makedirs ( ) <EOL> firstDirectory = self . createDummyPackage ( root , '<STR_LIT>' , '<STR_LIT>' ) <EOL> secondDirectory = self . createDummyPackage ( root , '<STR_LIT>' , '<STR_LIT>' ) <EOL> sys . path . append ( firstDirectory . path ) <EOL> sys . path . append ( secondDirectory . path ) <EOL> import dummy . plugins <EOL> plugins = list ( plugin . getPlugins ( ITestPlugin , dummy . plugins ) ) <EOL> self . assertEqual ( [ '<STR_LIT>' ] , [ p . __name__ for p in plugins ] ) <EOL> class PackagePathTests ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> self . originalPath = sys . path [ : ] <EOL> def tearDown ( self ) : <EOL> """<STR_LIT>""" <EOL> sys . path [ : ] = self . originalPath <EOL> def test_pluginDirectories ( self ) : <EOL> """<STR_LIT>""" <EOL> foo = FilePath ( '<STR_LIT:foo>' ) <EOL> bar = FilePath ( '<STR_LIT:bar>' ) <EOL> sys . path = [ foo . path , bar . path ] <EOL> self . assertEqual ( <EOL> plugin . pluginPackagePaths ( '<STR_LIT>' ) , <EOL> [ foo . child ( '<STR_LIT>' ) . child ( '<STR_LIT>' ) . path , <EOL> bar . child ( '<STR_LIT>' ) . child ( '<STR_LIT>' ) . path ] ) <EOL> def test_pluginPackagesExcluded ( self ) : <EOL> """<STR_LIT>""" <EOL> root = FilePath ( self . mktemp ( ) ) <EOL> foo = root . child ( '<STR_LIT:foo>' ) . child ( '<STR_LIT>' ) . child ( '<STR_LIT>' ) <EOL> foo . makedirs ( ) <EOL> foo . child ( '<STR_LIT>' ) . setContent ( '<STR_LIT>' ) <EOL> sys . path = [ root . child ( '<STR_LIT:foo>' ) . path , root . child ( '<STR_LIT:bar>' ) . path ] <EOL> self . assertEqual ( <EOL> plugin . pluginPackagePaths ( '<STR_LIT>' ) , <EOL> [ root . child ( '<STR_LIT:bar>' ) . child ( '<STR_LIT>' ) . child ( '<STR_LIT>' ) . path ] ) </s>
