blob: 29286c710b577b420bac89df93d99f148e5e2070 [file] [log] [blame]
Brett Cannon0096e262004-06-05 01:12:51 +00001"""Tests for 'site'.
2
3Tests assume the initial paths in sys.path once the interpreter has begun
4executing have not been removed.
5
6"""
7import unittest
Walter Dörwaldb525e182009-04-26 21:39:21 +00008from test.support import run_unittest, TESTFN, EnvironmentVarGuard
R. David Murrayb4ca59b2010-12-26 19:54:29 +00009from test.support import captured_stderr
Georg Brandl1a3284e2007-12-02 09:40:06 +000010import builtins
Brett Cannon0096e262004-06-05 01:12:51 +000011import os
12import sys
R. David Murrayab9d8d62010-12-27 00:03:13 +000013import re
Brett Cannon0096e262004-06-05 01:12:51 +000014import encodings
Christian Heimes8dc226f2008-05-06 23:45:46 +000015import subprocess
Tarek Ziadéedacea32010-01-29 11:41:03 +000016import sysconfig
17from copy import copy
18
Brett Cannon0096e262004-06-05 01:12:51 +000019# Need to make sure to not import 'site' if someone specified ``-S`` at the
20# command-line. Detect this by just making sure 'site' has not been imported
21# already.
22if "site" in sys.modules:
23 import site
24else:
Benjamin Petersone549ead2009-03-28 21:42:05 +000025 raise unittest.SkipTest("importation of site.py suppressed")
Brett Cannon0096e262004-06-05 01:12:51 +000026
Ned Deily316f5732011-10-31 16:16:35 -070027if site.ENABLE_USER_SITE and not os.path.isdir(site.USER_SITE):
Christian Heimes8dc226f2008-05-06 23:45:46 +000028 # need to add user site directory for tests
29 os.makedirs(site.USER_SITE)
30 site.addsitedir(site.USER_SITE)
31
Brett Cannon0096e262004-06-05 01:12:51 +000032class HelperFunctionsTests(unittest.TestCase):
33 """Tests for helper functions.
Brett Cannon0096e262004-06-05 01:12:51 +000034 """
35
36 def setUp(self):
37 """Save a copy of sys.path"""
38 self.sys_path = sys.path[:]
Tarek Ziadé4a608c02009-08-20 21:28:05 +000039 self.old_base = site.USER_BASE
40 self.old_site = site.USER_SITE
41 self.old_prefixes = site.PREFIXES
Brett Cannon8ac95ee2012-04-04 17:31:16 -040042 self.original_vars = sysconfig._CONFIG_VARS
Tarek Ziadéedacea32010-01-29 11:41:03 +000043 self.old_vars = copy(sysconfig._CONFIG_VARS)
Brett Cannon0096e262004-06-05 01:12:51 +000044
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +000045 def tearDown(self):
Brett Cannon0096e262004-06-05 01:12:51 +000046 """Restore sys.path"""
Nick Coghlan6ead5522009-10-18 13:19:33 +000047 sys.path[:] = self.sys_path
Tarek Ziadé4a608c02009-08-20 21:28:05 +000048 site.USER_BASE = self.old_base
49 site.USER_SITE = self.old_site
50 site.PREFIXES = self.old_prefixes
Brett Cannon8ac95ee2012-04-04 17:31:16 -040051 sysconfig._CONFIG_VARS = self.original_vars
52 sysconfig._CONFIG_VARS.clear()
53 sysconfig._CONFIG_VARS.update(self.old_vars)
Raymond Hettingerebd95222004-06-27 03:02:18 +000054
Brett Cannon0096e262004-06-05 01:12:51 +000055 def test_makepath(self):
56 # Test makepath() have an absolute path for its first return value
57 # and a case-normalized version of the absolute path for its
58 # second value.
59 path_parts = ("Beginning", "End")
60 original_dir = os.path.join(*path_parts)
61 abs_dir, norm_dir = site.makepath(*path_parts)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000062 self.assertEqual(os.path.abspath(original_dir), abs_dir)
Brett Cannon0096e262004-06-05 01:12:51 +000063 if original_dir == os.path.normcase(original_dir):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000064 self.assertEqual(abs_dir, norm_dir)
Brett Cannon0096e262004-06-05 01:12:51 +000065 else:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000066 self.assertEqual(os.path.normcase(abs_dir), norm_dir)
Brett Cannon0096e262004-06-05 01:12:51 +000067
68 def test_init_pathinfo(self):
69 dir_set = site._init_pathinfo()
70 for entry in [site.makepath(path)[1] for path in sys.path
71 if path and os.path.isdir(path)]:
Ezio Melottib58e0bd2010-01-23 15:40:09 +000072 self.assertIn(entry, dir_set,
73 "%s from sys.path not found in set returned "
74 "by _init_pathinfo(): %s" % (entry, dir_set))
Raymond Hettingerebd95222004-06-27 03:02:18 +000075
Brett Cannonee86a662004-07-13 07:12:25 +000076 def pth_file_tests(self, pth_file):
77 """Contain common code for testing results of reading a .pth file"""
Ezio Melottib58e0bd2010-01-23 15:40:09 +000078 self.assertIn(pth_file.imported, sys.modules,
79 "%s not in sys.modules" % pth_file.imported)
Antoine Pitrou9166e6a2009-11-01 23:55:40 +000080 self.assertIn(site.makepath(pth_file.good_dir_path)[0], sys.path)
81 self.assertFalse(os.path.exists(pth_file.bad_dir_path))
Brett Cannonee86a662004-07-13 07:12:25 +000082
Brett Cannon0096e262004-06-05 01:12:51 +000083 def test_addpackage(self):
84 # Make sure addpackage() imports if the line starts with 'import',
Brett Cannon64a84702004-07-10 02:10:45 +000085 # adds directories to sys.path for any line in the file that is not a
86 # comment or import that is a valid directory name for where the .pth
87 # file resides; invalid directories are not added
88 pth_file = PthFile()
Brett Cannonee86a662004-07-13 07:12:25 +000089 pth_file.cleanup(prep=True) # to make sure that nothing is
90 # pre-existing that shouldn't be
Brett Cannon0096e262004-06-05 01:12:51 +000091 try:
Brett Cannon64a84702004-07-10 02:10:45 +000092 pth_file.create()
93 site.addpackage(pth_file.base_dir, pth_file.filename, set())
Brett Cannonee86a662004-07-13 07:12:25 +000094 self.pth_file_tests(pth_file)
Brett Cannon0096e262004-06-05 01:12:51 +000095 finally:
Brett Cannon64a84702004-07-10 02:10:45 +000096 pth_file.cleanup()
Raymond Hettingerebd95222004-06-27 03:02:18 +000097
R. David Murrayb4ca59b2010-12-26 19:54:29 +000098 def make_pth(self, contents, pth_dir='.', pth_name=TESTFN):
99 # Create a .pth file and return its (abspath, basename).
100 pth_dir = os.path.abspath(pth_dir)
101 pth_basename = pth_name + '.pth'
102 pth_fn = os.path.join(pth_dir, pth_basename)
103 pth_file = open(pth_fn, 'w', encoding='utf-8')
104 self.addCleanup(lambda: os.remove(pth_fn))
105 pth_file.write(contents)
106 pth_file.close()
107 return pth_dir, pth_basename
108
109 def test_addpackage_import_bad_syntax(self):
110 # Issue 10642
111 pth_dir, pth_fn = self.make_pth("import bad)syntax\n")
112 with captured_stderr() as err_out:
113 site.addpackage(pth_dir, pth_fn, set())
114 self.assertRegex(err_out.getvalue(), "line 1")
R. David Murrayab9d8d62010-12-27 00:03:13 +0000115 self.assertRegex(err_out.getvalue(),
116 re.escape(os.path.join(pth_dir, pth_fn)))
R. David Murrayb4ca59b2010-12-26 19:54:29 +0000117 # XXX: the previous two should be independent checks so that the
118 # order doesn't matter. The next three could be a single check
119 # but my regex foo isn't good enough to write it.
120 self.assertRegex(err_out.getvalue(), 'Traceback')
121 self.assertRegex(err_out.getvalue(), r'import bad\)syntax')
122 self.assertRegex(err_out.getvalue(), 'SyntaxError')
123
124 def test_addpackage_import_bad_exec(self):
125 # Issue 10642
126 pth_dir, pth_fn = self.make_pth("randompath\nimport nosuchmodule\n")
127 with captured_stderr() as err_out:
128 site.addpackage(pth_dir, pth_fn, set())
129 self.assertRegex(err_out.getvalue(), "line 2")
R. David Murrayab9d8d62010-12-27 00:03:13 +0000130 self.assertRegex(err_out.getvalue(),
131 re.escape(os.path.join(pth_dir, pth_fn)))
R. David Murrayb4ca59b2010-12-26 19:54:29 +0000132 # XXX: ditto previous XXX comment.
133 self.assertRegex(err_out.getvalue(), 'Traceback')
134 self.assertRegex(err_out.getvalue(), 'ImportError')
135
R. David Murrayad4ccfd2010-12-27 04:31:48 +0000136 @unittest.skipIf(sys.platform == "win32", "Windows does not raise an "
137 "error for file paths containing null characters")
R. David Murrayb4ca59b2010-12-26 19:54:29 +0000138 def test_addpackage_import_bad_pth_file(self):
139 # Issue 5258
140 pth_dir, pth_fn = self.make_pth("abc\x00def\n")
141 with captured_stderr() as err_out:
142 site.addpackage(pth_dir, pth_fn, set())
143 self.assertRegex(err_out.getvalue(), "line 1")
R. David Murrayab9d8d62010-12-27 00:03:13 +0000144 self.assertRegex(err_out.getvalue(),
145 re.escape(os.path.join(pth_dir, pth_fn)))
R. David Murrayb4ca59b2010-12-26 19:54:29 +0000146 # XXX: ditto previous XXX comment.
147 self.assertRegex(err_out.getvalue(), 'Traceback')
148 self.assertRegex(err_out.getvalue(), 'TypeError')
149
Brett Cannon0096e262004-06-05 01:12:51 +0000150 def test_addsitedir(self):
Brett Cannon64a84702004-07-10 02:10:45 +0000151 # Same tests for test_addpackage since addsitedir() essentially just
152 # calls addpackage() for every .pth file in the directory
153 pth_file = PthFile()
Brett Cannonee86a662004-07-13 07:12:25 +0000154 pth_file.cleanup(prep=True) # Make sure that nothing is pre-existing
155 # that is tested for
Brett Cannon0096e262004-06-05 01:12:51 +0000156 try:
Brett Cannonee86a662004-07-13 07:12:25 +0000157 pth_file.create()
Brett Cannon64a84702004-07-10 02:10:45 +0000158 site.addsitedir(pth_file.base_dir, set())
Brett Cannonee86a662004-07-13 07:12:25 +0000159 self.pth_file_tests(pth_file)
Brett Cannon0096e262004-06-05 01:12:51 +0000160 finally:
Brett Cannon64a84702004-07-10 02:10:45 +0000161 pth_file.cleanup()
Brett Cannon0096e262004-06-05 01:12:51 +0000162
Ned Deily316f5732011-10-31 16:16:35 -0700163 @unittest.skipUnless(site.ENABLE_USER_SITE, "requires access to PEP 370 "
164 "user-site (site.ENABLE_USER_SITE)")
Christian Heimes8dc226f2008-05-06 23:45:46 +0000165 def test_s_option(self):
166 usersite = site.USER_SITE
Antoine Pitrou9166e6a2009-11-01 23:55:40 +0000167 self.assertIn(usersite, sys.path)
Christian Heimes8dc226f2008-05-06 23:45:46 +0000168
Éric Araujo63ebe1c2011-01-03 17:51:11 +0000169 env = os.environ.copy()
Christian Heimes8dc226f2008-05-06 23:45:46 +0000170 rc = subprocess.call([sys.executable, '-c',
Éric Araujo63ebe1c2011-01-03 17:51:11 +0000171 'import sys; sys.exit(%r in sys.path)' % usersite],
172 env=env)
Christian Heimes8dc226f2008-05-06 23:45:46 +0000173 self.assertEqual(rc, 1)
174
Éric Araujo63ebe1c2011-01-03 17:51:11 +0000175 env = os.environ.copy()
Christian Heimes8dc226f2008-05-06 23:45:46 +0000176 rc = subprocess.call([sys.executable, '-s', '-c',
Éric Araujo63ebe1c2011-01-03 17:51:11 +0000177 'import sys; sys.exit(%r in sys.path)' % usersite],
178 env=env)
Christian Heimes8dc226f2008-05-06 23:45:46 +0000179 self.assertEqual(rc, 0)
180
181 env = os.environ.copy()
182 env["PYTHONNOUSERSITE"] = "1"
183 rc = subprocess.call([sys.executable, '-c',
Benjamin Petersonfea6a942008-07-02 16:11:42 +0000184 'import sys; sys.exit(%r in sys.path)' % usersite],
Christian Heimes8dc226f2008-05-06 23:45:46 +0000185 env=env)
186 self.assertEqual(rc, 0)
187
188 env = os.environ.copy()
189 env["PYTHONUSERBASE"] = "/tmp"
190 rc = subprocess.call([sys.executable, '-c',
191 'import sys, site; sys.exit(site.USER_BASE.startswith("/tmp"))'],
192 env=env)
193 self.assertEqual(rc, 1)
194
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000195 def test_getuserbase(self):
196 site.USER_BASE = None
197 user_base = site.getuserbase()
198
199 # the call sets site.USER_BASE
Ezio Melottib3aedd42010-11-20 19:04:17 +0000200 self.assertEqual(site.USER_BASE, user_base)
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000201
202 # let's set PYTHONUSERBASE and see if it uses it
203 site.USER_BASE = None
Tarek Ziadéedacea32010-01-29 11:41:03 +0000204 import sysconfig
205 sysconfig._CONFIG_VARS = None
206
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000207 with EnvironmentVarGuard() as environ:
208 environ['PYTHONUSERBASE'] = 'xoxo'
Antoine Pitrou9166e6a2009-11-01 23:55:40 +0000209 self.assertTrue(site.getuserbase().startswith('xoxo'),
210 site.getuserbase())
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000211
212 def test_getusersitepackages(self):
213 site.USER_SITE = None
214 site.USER_BASE = None
215 user_site = site.getusersitepackages()
216
217 # the call sets USER_BASE *and* USER_SITE
Ezio Melottib3aedd42010-11-20 19:04:17 +0000218 self.assertEqual(site.USER_SITE, user_site)
Antoine Pitrou9166e6a2009-11-01 23:55:40 +0000219 self.assertTrue(user_site.startswith(site.USER_BASE), user_site)
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000220
221 def test_getsitepackages(self):
222 site.PREFIXES = ['xoxo']
223 dirs = site.getsitepackages()
224
225 if sys.platform in ('os2emx', 'riscos'):
Antoine Pitrou9166e6a2009-11-01 23:55:40 +0000226 self.assertEqual(len(dirs), 1)
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000227 wanted = os.path.join('xoxo', 'Lib', 'site-packages')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000228 self.assertEqual(dirs[0], wanted)
Ned Deilyd531b292012-02-06 00:58:18 +0100229 elif (sys.platform == "darwin" and
230 sysconfig.get_config_var("PYTHONFRAMEWORK")):
231 # OS X framework builds
232 site.PREFIXES = ['Python.framework']
233 dirs = site.getsitepackages()
234 self.assertEqual(len(dirs), 3)
235 wanted = os.path.join('/Library',
236 sysconfig.get_config_var("PYTHONFRAMEWORK"),
237 sys.version[:3],
238 'site-packages')
239 self.assertEqual(dirs[2], wanted)
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000240 elif os.sep == '/':
Ned Deilyd531b292012-02-06 00:58:18 +0100241 # OS X non-framwework builds, Linux, FreeBSD, etc
Ezio Melottifc8b2052010-08-17 08:35:41 +0000242 self.assertEqual(len(dirs), 2)
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000243 wanted = os.path.join('xoxo', 'lib', 'python' + sys.version[:3],
244 'site-packages')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000245 self.assertEqual(dirs[0], wanted)
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000246 wanted = os.path.join('xoxo', 'lib', 'site-python')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000247 self.assertEqual(dirs[1], wanted)
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000248 else:
Ned Deilyd531b292012-02-06 00:58:18 +0100249 # other platforms
Ezio Melottifc8b2052010-08-17 08:35:41 +0000250 self.assertEqual(len(dirs), 2)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000251 self.assertEqual(dirs[0], 'xoxo')
Tarek Ziadé8c0e2172009-10-27 21:24:21 +0000252 wanted = os.path.join('xoxo', 'lib', 'site-packages')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000253 self.assertEqual(dirs[1], wanted)
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000254
Brett Cannon64a84702004-07-10 02:10:45 +0000255class PthFile(object):
256 """Helper class for handling testing of .pth files"""
Brett Cannon0096e262004-06-05 01:12:51 +0000257
Brett Cannon64a84702004-07-10 02:10:45 +0000258 def __init__(self, filename_base=TESTFN, imported="time",
259 good_dirname="__testdir__", bad_dirname="__bad"):
260 """Initialize instance variables"""
261 self.filename = filename_base + ".pth"
262 self.base_dir = os.path.abspath('')
263 self.file_path = os.path.join(self.base_dir, self.filename)
Brett Cannonee86a662004-07-13 07:12:25 +0000264 self.imported = imported
Brett Cannon64a84702004-07-10 02:10:45 +0000265 self.good_dirname = good_dirname
266 self.bad_dirname = bad_dirname
267 self.good_dir_path = os.path.join(self.base_dir, self.good_dirname)
268 self.bad_dir_path = os.path.join(self.base_dir, self.bad_dirname)
Brett Cannon0096e262004-06-05 01:12:51 +0000269
Brett Cannon64a84702004-07-10 02:10:45 +0000270 def create(self):
271 """Create a .pth file with a comment, blank lines, an ``import
272 <self.imported>``, a line with self.good_dirname, and a line with
273 self.bad_dirname.
Tim Peters182b5ac2004-07-18 06:16:08 +0000274
Brett Cannon64a84702004-07-10 02:10:45 +0000275 Creation of the directory for self.good_dir_path (based off of
276 self.good_dirname) is also performed.
Brett Cannon0096e262004-06-05 01:12:51 +0000277
Brett Cannon64a84702004-07-10 02:10:45 +0000278 Make sure to call self.cleanup() to undo anything done by this method.
Tim Peters182b5ac2004-07-18 06:16:08 +0000279
Brett Cannon64a84702004-07-10 02:10:45 +0000280 """
Michael W. Hudsonff522862005-05-27 14:58:06 +0000281 FILE = open(self.file_path, 'w')
Brett Cannon64a84702004-07-10 02:10:45 +0000282 try:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000283 print("#import @bad module name", file=FILE)
284 print("\n", file=FILE)
285 print("import %s" % self.imported, file=FILE)
286 print(self.good_dirname, file=FILE)
287 print(self.bad_dirname, file=FILE)
Brett Cannon64a84702004-07-10 02:10:45 +0000288 finally:
289 FILE.close()
290 os.mkdir(self.good_dir_path)
291
Brett Cannonee86a662004-07-13 07:12:25 +0000292 def cleanup(self, prep=False):
Brett Cannon64a84702004-07-10 02:10:45 +0000293 """Make sure that the .pth file is deleted, self.imported is not in
294 sys.modules, and that both self.good_dirname and self.bad_dirname are
295 not existing directories."""
Brett Cannonee86a662004-07-13 07:12:25 +0000296 if os.path.exists(self.file_path):
Brett Cannon64a84702004-07-10 02:10:45 +0000297 os.remove(self.file_path)
Brett Cannonee86a662004-07-13 07:12:25 +0000298 if prep:
299 self.imported_module = sys.modules.get(self.imported)
300 if self.imported_module:
301 del sys.modules[self.imported]
302 else:
303 if self.imported_module:
304 sys.modules[self.imported] = self.imported_module
305 if os.path.exists(self.good_dir_path):
Brett Cannon64a84702004-07-10 02:10:45 +0000306 os.rmdir(self.good_dir_path)
Brett Cannonee86a662004-07-13 07:12:25 +0000307 if os.path.exists(self.bad_dir_path):
Brett Cannon64a84702004-07-10 02:10:45 +0000308 os.rmdir(self.bad_dir_path)
Brett Cannon0096e262004-06-05 01:12:51 +0000309
310class ImportSideEffectTests(unittest.TestCase):
311 """Test side-effects from importing 'site'."""
312
313 def setUp(self):
314 """Make a copy of sys.path"""
315 self.sys_path = sys.path[:]
316
317 def tearDown(self):
318 """Restore sys.path"""
Nick Coghlan6ead5522009-10-18 13:19:33 +0000319 sys.path[:] = self.sys_path
Brett Cannon0096e262004-06-05 01:12:51 +0000320
Barry Warsaw28a691b2010-04-17 00:19:56 +0000321 def test_abs_paths(self):
322 # Make sure all imported modules have their __file__ and __cached__
323 # attributes as absolute paths. Arranging to put the Lib directory on
324 # PYTHONPATH would cause the os module to have a relative path for
325 # __file__ if abs_paths() does not get run. sys and builtins (the
326 # only other modules imported before site.py runs) do not have
327 # __file__ or __cached__ because they are built-in.
328 parent = os.path.relpath(os.path.dirname(os.__file__))
329 env = os.environ.copy()
330 env['PYTHONPATH'] = parent
Victor Stinnerf3bc2582010-04-18 07:59:53 +0000331 code = ('import os, sys',
332 # use ASCII to avoid locale issues with non-ASCII directories
333 'os_file = os.__file__.encode("ascii", "backslashreplace")',
334 r'sys.stdout.buffer.write(os_file + b"\n")',
335 'os_cached = os.__cached__.encode("ascii", "backslashreplace")',
336 r'sys.stdout.buffer.write(os_cached + b"\n")')
337 command = '\n'.join(code)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000338 # First, prove that with -S (no 'import site'), the paths are
339 # relative.
340 proc = subprocess.Popen([sys.executable, '-S', '-c', command],
341 env=env,
Victor Stinnerf3bc2582010-04-18 07:59:53 +0000342 stdout=subprocess.PIPE)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000343 stdout, stderr = proc.communicate()
Victor Stinnerf3bc2582010-04-18 07:59:53 +0000344
Barry Warsaw28a691b2010-04-17 00:19:56 +0000345 self.assertEqual(proc.returncode, 0)
Victor Stinnerf3bc2582010-04-18 07:59:53 +0000346 os__file__, os__cached__ = stdout.splitlines()[:2]
Barry Warsaw28a691b2010-04-17 00:19:56 +0000347 self.assertFalse(os.path.isabs(os__file__))
348 self.assertFalse(os.path.isabs(os__cached__))
349 # Now, with 'import site', it works.
350 proc = subprocess.Popen([sys.executable, '-c', command],
351 env=env,
Victor Stinnerf3bc2582010-04-18 07:59:53 +0000352 stdout=subprocess.PIPE)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000353 stdout, stderr = proc.communicate()
354 self.assertEqual(proc.returncode, 0)
Victor Stinnerf3bc2582010-04-18 07:59:53 +0000355 os__file__, os__cached__ = stdout.splitlines()[:2]
Barry Warsaw28a691b2010-04-17 00:19:56 +0000356 self.assertTrue(os.path.isabs(os__file__))
357 self.assertTrue(os.path.isabs(os__cached__))
Brett Cannon0096e262004-06-05 01:12:51 +0000358
359 def test_no_duplicate_paths(self):
360 # No duplicate paths should exist in sys.path
361 # Handled by removeduppaths()
362 site.removeduppaths()
363 seen_paths = set()
364 for path in sys.path:
Benjamin Peterson577473f2010-01-19 00:09:57 +0000365 self.assertNotIn(path, seen_paths)
Brett Cannon0096e262004-06-05 01:12:51 +0000366 seen_paths.add(path)
367
368 def test_add_build_dir(self):
369 # Test that the build directory's Modules directory is used when it
370 # should be.
371 # XXX: implement
372 pass
373
Brett Cannon0096e262004-06-05 01:12:51 +0000374 def test_setting_quit(self):
Georg Brandl1a3284e2007-12-02 09:40:06 +0000375 # 'quit' and 'exit' should be injected into builtins
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000376 self.assertTrue(hasattr(builtins, "quit"))
377 self.assertTrue(hasattr(builtins, "exit"))
Brett Cannon0096e262004-06-05 01:12:51 +0000378
379 def test_setting_copyright(self):
Georg Brandl1a3284e2007-12-02 09:40:06 +0000380 # 'copyright' and 'credits' should be in builtins
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000381 self.assertTrue(hasattr(builtins, "copyright"))
382 self.assertTrue(hasattr(builtins, "credits"))
Brett Cannon0096e262004-06-05 01:12:51 +0000383
384 def test_setting_help(self):
Georg Brandl1a3284e2007-12-02 09:40:06 +0000385 # 'help' should be set in builtins
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000386 self.assertTrue(hasattr(builtins, "help"))
Brett Cannon0096e262004-06-05 01:12:51 +0000387
388 def test_aliasing_mbcs(self):
389 if sys.platform == "win32":
390 import locale
391 if locale.getdefaultlocale()[1].startswith('cp'):
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000392 for value in encodings.aliases.aliases.values():
Brett Cannon0096e262004-06-05 01:12:51 +0000393 if value == "mbcs":
394 break
395 else:
396 self.fail("did not alias mbcs")
397
Brett Cannon0096e262004-06-05 01:12:51 +0000398 def test_sitecustomize_executed(self):
399 # If sitecustomize is available, it should have been imported.
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000400 if "sitecustomize" not in sys.modules:
Brett Cannon0096e262004-06-05 01:12:51 +0000401 try:
402 import sitecustomize
403 except ImportError:
404 pass
405 else:
406 self.fail("sitecustomize not imported automatically")
407
Brett Cannon0096e262004-06-05 01:12:51 +0000408def test_main():
409 run_unittest(HelperFunctionsTests, ImportSideEffectTests)
410
Brett Cannon0096e262004-06-05 01:12:51 +0000411if __name__ == "__main__":
412 test_main()