Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
test.test_site.HelperFunctionsTests Class Reference

List of all members.

Public Member Functions

def setUp
def tearDown
def test_makepath
def test_init_pathinfo
def pth_file_tests
def test_addpackage
def make_pth
def test_addpackage_import_bad_syntax
def test_addpackage_import_bad_exec
def test_addpackage_import_bad_pth_file
def test_addsitedir
def test_s_option
def test_getuserbase
def test_getusersitepackages
def test_getsitepackages

Public Attributes

 sys_path
 old_base
 old_site
 old_prefixes
 old_vars

Detailed Description

Tests for helper functions.

Definition at line 32 of file test_site.py.


Member Function Documentation

def test.test_site.HelperFunctionsTests.make_pth (   self,
  contents,
  pth_dir = '.',
  pth_name = TESTFN 
)

Definition at line 95 of file test_site.py.

00095 
00096     def make_pth(self, contents, pth_dir='.', pth_name=TESTFN):
00097         # Create a .pth file and return its (abspath, basename).
00098         pth_dir = os.path.abspath(pth_dir)
00099         pth_basename = pth_name + '.pth'
00100         pth_fn = os.path.join(pth_dir, pth_basename)
00101         pth_file = open(pth_fn, 'w', encoding='utf-8')
00102         self.addCleanup(lambda: os.remove(pth_fn))
00103         pth_file.write(contents)
00104         pth_file.close()
00105         return pth_dir, pth_basename

Here is the call graph for this function:

Here is the caller graph for this function:

Contain common code for testing results of reading a .pth file

Definition at line 73 of file test_site.py.

00073 
00074     def pth_file_tests(self, pth_file):
00075         """Contain common code for testing results of reading a .pth file"""
00076         self.assertIn(pth_file.imported, sys.modules,
00077                       "%s not in sys.modules" % pth_file.imported)
00078         self.assertIn(site.makepath(pth_file.good_dir_path)[0], sys.path)
00079         self.assertFalse(os.path.exists(pth_file.bad_dir_path))

Here is the call graph for this function:

Here is the caller graph for this function:

Save a copy of sys.path

Definition at line 36 of file test_site.py.

00036 
00037     def setUp(self):
00038         """Save a copy of sys.path"""
00039         self.sys_path = sys.path[:]
00040         self.old_base = site.USER_BASE
00041         self.old_site = site.USER_SITE
00042         self.old_prefixes = site.PREFIXES
00043         self.old_vars = copy(sysconfig._CONFIG_VARS)

Here is the caller graph for this function:

Restore sys.path

Definition at line 44 of file test_site.py.

00044 
00045     def tearDown(self):
00046         """Restore sys.path"""
00047         sys.path[:] = self.sys_path
00048         site.USER_BASE = self.old_base
00049         site.USER_SITE = self.old_site
00050         site.PREFIXES = self.old_prefixes
00051         sysconfig._CONFIG_VARS = self.old_vars

Here is the caller graph for this function:

Definition at line 80 of file test_site.py.

00080 
00081     def test_addpackage(self):
00082         # Make sure addpackage() imports if the line starts with 'import',
00083         # adds directories to sys.path for any line in the file that is not a
00084         # comment or import that is a valid directory name for where the .pth
00085         # file resides; invalid directories are not added
00086         pth_file = PthFile()
00087         pth_file.cleanup(prep=True)  # to make sure that nothing is
00088                                       # pre-existing that shouldn't be
00089         try:
00090             pth_file.create()
00091             site.addpackage(pth_file.base_dir, pth_file.filename, set())
00092             self.pth_file_tests(pth_file)
00093         finally:
00094             pth_file.cleanup()

Here is the call graph for this function:

Definition at line 121 of file test_site.py.

00121 
00122     def test_addpackage_import_bad_exec(self):
00123         # Issue 10642
00124         pth_dir, pth_fn = self.make_pth("randompath\nimport nosuchmodule\n")
00125         with captured_stderr() as err_out:
00126             site.addpackage(pth_dir, pth_fn, set())
00127         self.assertRegex(err_out.getvalue(), "line 2")
00128         self.assertRegex(err_out.getvalue(),
00129             re.escape(os.path.join(pth_dir, pth_fn)))
00130         # XXX: ditto previous XXX comment.
00131         self.assertRegex(err_out.getvalue(), 'Traceback')
00132         self.assertRegex(err_out.getvalue(), 'ImportError')

Here is the call graph for this function:

Definition at line 135 of file test_site.py.

00135 
00136     def test_addpackage_import_bad_pth_file(self):
00137         # Issue 5258
00138         pth_dir, pth_fn = self.make_pth("abc\x00def\n")
00139         with captured_stderr() as err_out:
00140             site.addpackage(pth_dir, pth_fn, set())
00141         self.assertRegex(err_out.getvalue(), "line 1")
00142         self.assertRegex(err_out.getvalue(),
00143             re.escape(os.path.join(pth_dir, pth_fn)))
00144         # XXX: ditto previous XXX comment.
00145         self.assertRegex(err_out.getvalue(), 'Traceback')
00146         self.assertRegex(err_out.getvalue(), 'TypeError')

Here is the call graph for this function:

Definition at line 106 of file test_site.py.

00106 
00107     def test_addpackage_import_bad_syntax(self):
00108         # Issue 10642
00109         pth_dir, pth_fn = self.make_pth("import bad)syntax\n")
00110         with captured_stderr() as err_out:
00111             site.addpackage(pth_dir, pth_fn, set())
00112         self.assertRegex(err_out.getvalue(), "line 1")
00113         self.assertRegex(err_out.getvalue(),
00114             re.escape(os.path.join(pth_dir, pth_fn)))
00115         # XXX: the previous two should be independent checks so that the
00116         # order doesn't matter.  The next three could be a single check
00117         # but my regex foo isn't good enough to write it.
00118         self.assertRegex(err_out.getvalue(), 'Traceback')
00119         self.assertRegex(err_out.getvalue(), r'import bad\)syntax')
00120         self.assertRegex(err_out.getvalue(), 'SyntaxError')

Here is the call graph for this function:

Definition at line 147 of file test_site.py.

00147 
00148     def test_addsitedir(self):
00149         # Same tests for test_addpackage since addsitedir() essentially just
00150         # calls addpackage() for every .pth file in the directory
00151         pth_file = PthFile()
00152         pth_file.cleanup(prep=True) # Make sure that nothing is pre-existing
00153                                     # that is tested for
00154         try:
00155             pth_file.create()
00156             site.addsitedir(pth_file.base_dir, set())
00157             self.pth_file_tests(pth_file)
00158         finally:
00159             pth_file.cleanup()

Here is the call graph for this function:

Definition at line 216 of file test_site.py.

00216 
00217     def test_getsitepackages(self):
00218         site.PREFIXES = ['xoxo']
00219         dirs = site.getsitepackages()
00220 
00221         if sys.platform in ('os2emx', 'riscos'):
00222             self.assertEqual(len(dirs), 1)
00223             wanted = os.path.join('xoxo', 'Lib', 'site-packages')
00224             self.assertEqual(dirs[0], wanted)
00225         elif os.sep == '/':
00226             self.assertEqual(len(dirs), 2)
00227             wanted = os.path.join('xoxo', 'lib', 'python' + sys.version[:3],
00228                                   'site-packages')
00229             self.assertEqual(dirs[0], wanted)
00230             wanted = os.path.join('xoxo', 'lib', 'site-python')
00231             self.assertEqual(dirs[1], wanted)
00232         else:
00233             self.assertEqual(len(dirs), 2)
00234             self.assertEqual(dirs[0], 'xoxo')
00235             wanted = os.path.join('xoxo', 'lib', 'site-packages')
00236             self.assertEqual(dirs[1], wanted)
00237 
00238         # let's try the specific Apple location
00239         if (sys.platform == "darwin" and
00240             sysconfig.get_config_var("PYTHONFRAMEWORK")):
00241             site.PREFIXES = ['Python.framework']
00242             dirs = site.getsitepackages()
00243             self.assertEqual(len(dirs), 3)
00244             wanted = os.path.join('/Library', 'Python', sys.version[:3],
00245                                   'site-packages')
00246             self.assertEqual(dirs[2], wanted)

Here is the call graph for this function:

Definition at line 190 of file test_site.py.

00190 
00191     def test_getuserbase(self):
00192         site.USER_BASE = None
00193         user_base = site.getuserbase()
00194 
00195         # the call sets site.USER_BASE
00196         self.assertEqual(site.USER_BASE, user_base)
00197 
00198         # let's set PYTHONUSERBASE and see if it uses it
00199         site.USER_BASE = None
00200         import sysconfig
00201         sysconfig._CONFIG_VARS = None
00202 
00203         with EnvironmentVarGuard() as environ:
00204             environ['PYTHONUSERBASE'] = 'xoxo'
00205             self.assertTrue(site.getuserbase().startswith('xoxo'),
00206                             site.getuserbase())

Here is the call graph for this function:

Definition at line 207 of file test_site.py.

00207 
00208     def test_getusersitepackages(self):
00209         site.USER_SITE = None
00210         site.USER_BASE = None
00211         user_site = site.getusersitepackages()
00212 
00213         # the call sets USER_BASE *and* USER_SITE
00214         self.assertEqual(site.USER_SITE, user_site)
00215         self.assertTrue(user_site.startswith(site.USER_BASE), user_site)

Here is the call graph for this function:

Definition at line 65 of file test_site.py.

00065 
00066     def test_init_pathinfo(self):
00067         dir_set = site._init_pathinfo()
00068         for entry in [site.makepath(path)[1] for path in sys.path
00069                         if path and os.path.isdir(path)]:
00070             self.assertIn(entry, dir_set,
00071                           "%s from sys.path not found in set returned "
00072                           "by _init_pathinfo(): %s" % (entry, dir_set))

Here is the call graph for this function:

Definition at line 52 of file test_site.py.

00052 
00053     def test_makepath(self):
00054         # Test makepath() have an absolute path for its first return value
00055         # and a case-normalized version of the absolute path for its
00056         # second value.
00057         path_parts = ("Beginning", "End")
00058         original_dir = os.path.join(*path_parts)
00059         abs_dir, norm_dir = site.makepath(*path_parts)
00060         self.assertEqual(os.path.abspath(original_dir), abs_dir)
00061         if original_dir == os.path.normcase(original_dir):
00062             self.assertEqual(abs_dir, norm_dir)
00063         else:
00064             self.assertEqual(os.path.normcase(abs_dir), norm_dir)

Here is the call graph for this function:

Definition at line 160 of file test_site.py.

00160 
00161     def test_s_option(self):
00162         usersite = site.USER_SITE
00163         self.assertIn(usersite, sys.path)
00164 
00165         env = os.environ.copy()
00166         rc = subprocess.call([sys.executable, '-c',
00167             'import sys; sys.exit(%r in sys.path)' % usersite],
00168             env=env)
00169         self.assertEqual(rc, 1)
00170 
00171         env = os.environ.copy()
00172         rc = subprocess.call([sys.executable, '-s', '-c',
00173             'import sys; sys.exit(%r in sys.path)' % usersite],
00174             env=env)
00175         self.assertEqual(rc, 0)
00176 
00177         env = os.environ.copy()
00178         env["PYTHONNOUSERSITE"] = "1"
00179         rc = subprocess.call([sys.executable, '-c',
00180             'import sys; sys.exit(%r in sys.path)' % usersite],
00181             env=env)
00182         self.assertEqual(rc, 0)
00183 
00184         env = os.environ.copy()
00185         env["PYTHONUSERBASE"] = "/tmp"
00186         rc = subprocess.call([sys.executable, '-c',
00187             'import sys, site; sys.exit(site.USER_BASE.startswith("/tmp"))'],
00188             env=env)
00189         self.assertEqual(rc, 1)

Here is the call graph for this function:


Member Data Documentation

Definition at line 39 of file test_site.py.

Definition at line 41 of file test_site.py.

Definition at line 40 of file test_site.py.

Definition at line 42 of file test_site.py.

Definition at line 38 of file test_site.py.


The documentation for this class was generated from the following file: