<s> """<STR_LIT>""" <EOL> import sys <EOL> import itertools <EOL> import zipfile <EOL> import compileall <EOL> import twisted <EOL> from twisted . trial . unittest import TestCase <EOL> from twisted . python import modules <EOL> from twisted . python . filepath import FilePath <EOL> from twisted . python . reflect import namedAny <EOL> from twisted . python . test . modules_helpers import TwistedModulesMixin <EOL> from twisted . python . test . test_zippath import zipit <EOL> class TwistedModulesTestCase ( TwistedModulesMixin , TestCase ) : <EOL> """<STR_LIT>""" <EOL> def findByIteration ( self , modname , where = modules , importPackages = False ) : <EOL> """<STR_LIT>""" <EOL> for modinfo in where . walkModules ( importPackages = importPackages ) : <EOL> if modinfo . name == modname : <EOL> return modinfo <EOL> self . fail ( "<STR_LIT>" % ( modname , ) ) <EOL> class BasicTests ( TwistedModulesTestCase ) : <EOL> def test_namespacedPackages ( self ) : <EOL> """<STR_LIT>""" <EOL> __import__ ( '<STR_LIT>' ) <EOL> namespaceBoilerplate = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> entry = self . pathEntryWithOnePackage ( ) <EOL> testPackagePath = entry . child ( '<STR_LIT>' ) <EOL> testPackagePath . child ( '<STR_LIT>' ) . setContent ( namespaceBoilerplate ) <EOL> nestedEntry = testPackagePath . child ( '<STR_LIT>' ) <EOL> nestedEntry . makedirs ( ) <EOL> nestedEntry . child ( '<STR_LIT>' ) . setContent ( namespaceBoilerplate ) <EOL> nestedEntry . child ( '<STR_LIT>' ) . setContent ( '<STR_LIT>' ) <EOL> anotherEntry = self . pathEntryWithOnePackage ( ) <EOL> anotherPackagePath = anotherEntry . child ( '<STR_LIT>' ) <EOL> anotherPackagePath . child ( '<STR_LIT>' ) . setContent ( namespaceBoilerplate ) <EOL> anotherNestedEntry = anotherPackagePath . child ( '<STR_LIT>' ) <EOL> anotherNestedEntry . makedirs ( ) <EOL> anotherNestedEntry . child ( '<STR_LIT>' ) . setContent ( namespaceBoilerplate ) <EOL> anotherNestedEntry . child ( '<STR_LIT>' ) . setContent ( '<STR_LIT>' ) <EOL> self . replaceSysPath ( [ entry . path , anotherEntry . path ] ) <EOL> module = modules . getModule ( '<STR_LIT>' ) <EOL> try : <EOL> walkedNames = [ <EOL> mod . name for mod in module . walkModules ( importPackages = True ) ] <EOL> finally : <EOL> for module in sys . modules . keys ( ) : <EOL> if module . startswith ( '<STR_LIT>' ) : <EOL> del sys . modules [ module ] <EOL> expected = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> self . assertEqual ( walkedNames , expected ) <EOL> def test_unimportablePackageGetItem ( self ) : <EOL> """<STR_LIT>""" <EOL> shouldNotLoad = [ ] <EOL> path = modules . PythonPath ( sysPath = [ self . pathEntryWithOnePackage ( ) . path ] , <EOL> moduleLoader = shouldNotLoad . append , <EOL> importerCache = { } , <EOL> sysPathHooks = { } , <EOL> moduleDict = { '<STR_LIT>' : None } ) <EOL> self . assertEqual ( shouldNotLoad , [ ] ) <EOL> self . assertEqual ( path [ '<STR_LIT>' ] . isLoaded ( ) , False ) <EOL> def test_unimportablePackageWalkModules ( self ) : <EOL> """<STR_LIT>""" <EOL> existentPath = self . pathEntryWithOnePackage ( ) <EOL> self . replaceSysPath ( [ existentPath . path ] ) <EOL> self . replaceSysModules ( { "<STR_LIT>" : None } ) <EOL> walked = list ( modules . walkModules ( ) ) <EOL> self . assertEqual ( [ m . name for m in walked ] , <EOL> [ "<STR_LIT>" ] ) <EOL> self . assertEqual ( walked [ <NUM_LIT:0> ] . isLoaded ( ) , False ) <EOL> def test_nonexistentPaths ( self ) : <EOL> """<STR_LIT>""" <EOL> existentPath = self . pathEntryWithOnePackage ( ) <EOL> nonexistentPath = FilePath ( self . mktemp ( ) ) <EOL> self . failIf ( nonexistentPath . exists ( ) ) <EOL> self . replaceSysPath ( [ existentPath . path ] ) <EOL> expected = [ modules . getModule ( "<STR_LIT>" ) ] <EOL> beforeModules = list ( modules . walkModules ( ) ) <EOL> sys . path . append ( nonexistentPath . path ) <EOL> afterModules = list ( modules . walkModules ( ) ) <EOL> self . assertEqual ( beforeModules , expected ) <EOL> self . assertEqual ( afterModules , expected ) <EOL> def test_nonDirectoryPaths ( self ) : <EOL> """<STR_LIT>""" <EOL> existentPath = self . pathEntryWithOnePackage ( ) <EOL> nonDirectoryPath = FilePath ( self . mktemp ( ) ) <EOL> self . failIf ( nonDirectoryPath . exists ( ) ) <EOL> nonDirectoryPath . setContent ( "<STR_LIT>" ) <EOL> self . replaceSysPath ( [ existentPath . path ] ) <EOL> beforeModules = list ( modules . walkModules ( ) ) <EOL> sys . path . append ( nonDirectoryPath . path ) <EOL> afterModules = list ( modules . walkModules ( ) ) <EOL> self . assertEqual ( beforeModules , afterModules ) <EOL> def test_twistedShowsUp ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertEqual ( modules . getModule ( '<STR_LIT>' ) , <EOL> self . findByIteration ( "<STR_LIT>" ) ) <EOL> def test_dottedNames ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertEqual ( <EOL> modules . getModule ( '<STR_LIT>' ) , <EOL> self . findByIteration ( "<STR_LIT>" , <EOL> where = modules . getModule ( '<STR_LIT>' ) ) ) <EOL> def test_onlyTopModules ( self ) : <EOL> """<STR_LIT>""" <EOL> for module in modules . iterModules ( ) : <EOL> self . failIf ( <EOL> '<STR_LIT:.>' in module . name , <EOL> "<STR_LIT>" <EOL> % ( module . filePath ) ) <EOL> def test_loadPackagesAndModules ( self ) : <EOL> """<STR_LIT>""" <EOL> for n in [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] : <EOL> m = namedAny ( n ) <EOL> self . failUnlessIdentical ( <EOL> modules . getModule ( n ) . load ( ) , <EOL> m ) <EOL> self . failUnlessIdentical ( <EOL> self . findByIteration ( n ) . load ( ) , <EOL> m ) <EOL> def test_pathEntriesOnPath ( self ) : <EOL> """<STR_LIT>""" <EOL> for n in [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] : <EOL> self . failUnlessIn ( <EOL> modules . getModule ( n ) . pathEntry . filePath . path , <EOL> sys . path ) <EOL> def test_alwaysPreferPy ( self ) : <EOL> """<STR_LIT>""" <EOL> mypath = FilePath ( self . mktemp ( ) ) <EOL> mypath . createDirectory ( ) <EOL> pp = modules . PythonPath ( sysPath = [ mypath . path ] ) <EOL> originalSmartPath = pp . _smartPath <EOL> def _evilSmartPath ( pathName ) : <EOL> o = originalSmartPath ( pathName ) <EOL> originalChildren = o . children <EOL> def evilChildren ( ) : <EOL> x = originalChildren ( ) <EOL> x . sort ( ) <EOL> x . reverse ( ) <EOL> return x <EOL> o . children = evilChildren <EOL> return o <EOL> mypath . child ( "<STR_LIT>" ) . setContent ( '<STR_LIT:\n>' ) <EOL> compileall . compile_dir ( mypath . path , quiet = True ) <EOL> self . assertEqual ( len ( mypath . children ( ) ) , <NUM_LIT:2> ) <EOL> pp . _smartPath = _evilSmartPath <EOL> self . assertEqual ( pp [ '<STR_LIT>' ] . filePath , <EOL> mypath . child ( '<STR_LIT>' ) ) <EOL> def test_packageMissingPath ( self ) : <EOL> """<STR_LIT>""" <EOL> mypath = FilePath ( self . mktemp ( ) ) <EOL> mypath . createDirectory ( ) <EOL> pp = modules . PythonPath ( sysPath = [ mypath . path ] ) <EOL> subpath = mypath . child ( "<STR_LIT>" ) <EOL> subpath . createDirectory ( ) <EOL> subpath . child ( "<STR_LIT>" ) . setContent ( '<STR_LIT>' ) <EOL> sys . path . append ( mypath . path ) <EOL> __import__ ( "<STR_LIT>" ) <EOL> try : <EOL> l = list ( pp . walkModules ( ) ) <EOL> self . assertEqual ( len ( l ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( l [ <NUM_LIT:0> ] . name , '<STR_LIT>' ) <EOL> finally : <EOL> del sys . modules [ '<STR_LIT>' ] <EOL> sys . path . remove ( mypath . path ) <EOL> class PathModificationTest ( TwistedModulesTestCase ) : <EOL> """<STR_LIT>""" <EOL> _serialnum = itertools . count ( ) . next <EOL> def setUp ( self ) : <EOL> self . pathExtensionName = self . mktemp ( ) <EOL> self . pathExtension = FilePath ( self . pathExtensionName ) <EOL> self . pathExtension . createDirectory ( ) <EOL> self . packageName = "<STR_LIT>" % ( self . _serialnum ( ) , ) <EOL> self . packagePath = self . pathExtension . child ( self . packageName ) <EOL> self . packagePath . createDirectory ( ) <EOL> self . packagePath . child ( "<STR_LIT>" ) . setContent ( "<STR_LIT>" ) <EOL> self . packagePath . child ( "<STR_LIT>" ) . setContent ( "<STR_LIT>" ) <EOL> self . packagePath . child ( "<STR_LIT>" ) . setContent ( "<STR_LIT>" ) <EOL> self . packagePath . child ( "<STR_LIT>" ) . setContent ( "<STR_LIT>" ) <EOL> self . pathSetUp = False <EOL> def _setupSysPath ( self ) : <EOL> assert not self . pathSetUp <EOL> self . pathSetUp = True <EOL> sys . path . append ( self . pathExtensionName ) <EOL> def _underUnderPathTest ( self , doImport = True ) : <EOL> moddir2 = self . mktemp ( ) <EOL> fpmd = FilePath ( moddir2 ) <EOL> fpmd . createDirectory ( ) <EOL> fpmd . child ( "<STR_LIT>" ) . setContent ( "<STR_LIT>" ) <EOL> self . packagePath . child ( "<STR_LIT>" ) . setContent ( <EOL> "<STR_LIT>" % ( moddir2 , ) ) <EOL> self . _setupSysPath ( ) <EOL> modinfo = modules . getModule ( self . packageName ) <EOL> self . assertEqual ( <EOL> self . findByIteration ( self . packageName + "<STR_LIT>" , modinfo , <EOL> importPackages = doImport ) , <EOL> modinfo [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( modinfo [ '<STR_LIT>' ] . load ( ) . x , <NUM_LIT> ) <EOL> def test_underUnderPathAlreadyImported ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _underUnderPathTest ( ) <EOL> def test_underUnderPathNotAlreadyImported ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _underUnderPathTest ( False ) <EOL> test_underUnderPathNotAlreadyImported . todo = ( <EOL> "<STR_LIT>" ) <EOL> def _listModules ( self ) : <EOL> pkginfo = modules . getModule ( self . packageName ) <EOL> nfni = [ modinfo . name . split ( "<STR_LIT:.>" ) [ - <NUM_LIT:1> ] for modinfo in <EOL> pkginfo . iterModules ( ) ] <EOL> nfni . sort ( ) <EOL> self . assertEqual ( nfni , [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT>' ] ) <EOL> def test_listingModules ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _setupSysPath ( ) <EOL> self . _listModules ( ) <EOL> def test_listingModulesAlreadyImported ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _setupSysPath ( ) <EOL> namedAny ( self . packageName ) <EOL> self . _listModules ( ) <EOL> def tearDown ( self ) : <EOL> if self . pathSetUp : <EOL> HORK = "<STR_LIT>" <EOL> assert sys . path . pop ( ) is self . pathExtensionName , HORK + "<STR_LIT>" <EOL> assert self . pathExtensionName not in sys . path , HORK + "<STR_LIT>" <EOL> class RebindingTest ( PathModificationTest ) : <EOL> """<STR_LIT>""" <EOL> def _setupSysPath ( self ) : <EOL> assert not self . pathSetUp <EOL> self . pathSetUp = True <EOL> self . savedSysPath = sys . path <EOL> sys . path = sys . path [ : ] <EOL> sys . path . append ( self . pathExtensionName ) <EOL> def tearDown ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . pathSetUp : <EOL> sys . path = self . savedSysPath <EOL> class ZipPathModificationTest ( PathModificationTest ) : <EOL> def _setupSysPath ( self ) : <EOL> assert not self . pathSetUp <EOL> zipit ( self . pathExtensionName , self . pathExtensionName + '<STR_LIT>' ) <EOL> self . pathExtensionName += '<STR_LIT>' <EOL> assert zipfile . is_zipfile ( self . pathExtensionName ) <EOL> PathModificationTest . _setupSysPath ( self ) <EOL> class PythonPathTestCase ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_unhandledImporter ( self ) : <EOL> """<STR_LIT>""" <EOL> class SecretImporter ( object ) : <EOL> pass <EOL> def hook ( name ) : <EOL> return SecretImporter ( ) <EOL> syspath = [ '<STR_LIT>' ] <EOL> sysmodules = { } <EOL> syshooks = [ hook ] <EOL> syscache = { } <EOL> def sysloader ( name ) : <EOL> return None <EOL> space = modules . PythonPath ( <EOL> syspath , sysmodules , syshooks , syscache , sysloader ) <EOL> entries = list ( space . iterEntries ( ) ) <EOL> self . assertEqual ( len ( entries ) , <NUM_LIT:1> ) <EOL> self . assertRaises ( KeyError , lambda : entries [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) <EOL> def test_inconsistentImporterCache ( self ) : <EOL> """<STR_LIT>""" <EOL> space = modules . PythonPath ( [ ] , sys . modules , [ ] , { } ) <EOL> thisModule = space [ __name__ ] <EOL> warnings = self . flushWarnings ( [ self . test_inconsistentImporterCache ] ) <EOL> self . assertEqual ( warnings [ <NUM_LIT:0> ] [ '<STR_LIT>' ] , UserWarning ) <EOL> self . assertEqual ( <EOL> warnings [ <NUM_LIT:0> ] [ '<STR_LIT:message>' ] , <EOL> FilePath ( twisted . __file__ ) . parent ( ) . dirname ( ) + <EOL> "<STR_LIT>" + __name__ + "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . assertEqual ( len ( warnings ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( thisModule . name , __name__ ) <EOL> def test_containsModule ( self ) : <EOL> """<STR_LIT>""" <EOL> thePath = modules . PythonPath ( ) <EOL> self . assertIn ( '<STR_LIT>' , thePath ) <EOL> def test_doesntContainModule ( self ) : <EOL> """<STR_LIT>""" <EOL> thePath = modules . PythonPath ( ) <EOL> self . assertNotIn ( '<STR_LIT>' , thePath ) </s>
