Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 1 | """Tests for 'site'. |
| 2 | |
| 3 | Tests assume the initial paths in sys.path once the interpreter has begun |
| 4 | executing have not been removed. |
| 5 | |
| 6 | """ |
| 7 | import unittest |
Walter Dörwald | b525e18 | 2009-04-26 21:39:21 +0000 | [diff] [blame] | 8 | from test.support import run_unittest, TESTFN, EnvironmentVarGuard |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 9 | import builtins |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 10 | import os |
| 11 | import sys |
| 12 | import encodings |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 13 | import subprocess |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 14 | import sysconfig |
| 15 | from copy import copy |
| 16 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 17 | # Need to make sure to not import 'site' if someone specified ``-S`` at the |
| 18 | # command-line. Detect this by just making sure 'site' has not been imported |
| 19 | # already. |
| 20 | if "site" in sys.modules: |
| 21 | import site |
| 22 | else: |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 23 | raise unittest.SkipTest("importation of site.py suppressed") |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 24 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 25 | if not os.path.isdir(site.USER_SITE): |
| 26 | # need to add user site directory for tests |
| 27 | os.makedirs(site.USER_SITE) |
| 28 | site.addsitedir(site.USER_SITE) |
| 29 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 30 | class HelperFunctionsTests(unittest.TestCase): |
| 31 | """Tests for helper functions. |
Raymond Hettinger | ebd9522 | 2004-06-27 03:02:18 +0000 | [diff] [blame] | 32 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 33 | The setting of the encoding (set using sys.setdefaultencoding) used by |
| 34 | the Unicode implementation is not tested. |
Raymond Hettinger | ebd9522 | 2004-06-27 03:02:18 +0000 | [diff] [blame] | 35 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 36 | """ |
| 37 | |
| 38 | def setUp(self): |
| 39 | """Save a copy of sys.path""" |
| 40 | self.sys_path = sys.path[:] |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 41 | self.old_base = site.USER_BASE |
| 42 | self.old_site = site.USER_SITE |
| 43 | self.old_prefixes = site.PREFIXES |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 44 | self.old_vars = copy(sysconfig._CONFIG_VARS) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 45 | |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 46 | def tearDown(self): |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 47 | """Restore sys.path""" |
Nick Coghlan | 6ead552 | 2009-10-18 13:19:33 +0000 | [diff] [blame] | 48 | sys.path[:] = self.sys_path |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 49 | site.USER_BASE = self.old_base |
| 50 | site.USER_SITE = self.old_site |
| 51 | site.PREFIXES = self.old_prefixes |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 52 | sysconfig._CONFIG_VARS = self.old_vars |
Raymond Hettinger | ebd9522 | 2004-06-27 03:02:18 +0000 | [diff] [blame] | 53 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 54 | def test_makepath(self): |
| 55 | # Test makepath() have an absolute path for its first return value |
| 56 | # and a case-normalized version of the absolute path for its |
| 57 | # second value. |
| 58 | path_parts = ("Beginning", "End") |
| 59 | original_dir = os.path.join(*path_parts) |
| 60 | abs_dir, norm_dir = site.makepath(*path_parts) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 61 | self.assertEqual(os.path.abspath(original_dir), abs_dir) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 62 | if original_dir == os.path.normcase(original_dir): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 63 | self.assertEqual(abs_dir, norm_dir) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 64 | else: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 65 | self.assertEqual(os.path.normcase(abs_dir), norm_dir) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 66 | |
| 67 | def test_init_pathinfo(self): |
| 68 | dir_set = site._init_pathinfo() |
| 69 | for entry in [site.makepath(path)[1] for path in sys.path |
| 70 | if path and os.path.isdir(path)]: |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 71 | self.assertIn(entry, dir_set, |
| 72 | "%s from sys.path not found in set returned " |
| 73 | "by _init_pathinfo(): %s" % (entry, dir_set)) |
Raymond Hettinger | ebd9522 | 2004-06-27 03:02:18 +0000 | [diff] [blame] | 74 | |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 75 | def pth_file_tests(self, pth_file): |
| 76 | """Contain common code for testing results of reading a .pth file""" |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 77 | self.assertIn(pth_file.imported, sys.modules, |
| 78 | "%s not in sys.modules" % pth_file.imported) |
Antoine Pitrou | 9166e6a | 2009-11-01 23:55:40 +0000 | [diff] [blame] | 79 | self.assertIn(site.makepath(pth_file.good_dir_path)[0], sys.path) |
| 80 | self.assertFalse(os.path.exists(pth_file.bad_dir_path)) |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 81 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 82 | def test_addpackage(self): |
| 83 | # Make sure addpackage() imports if the line starts with 'import', |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 84 | # adds directories to sys.path for any line in the file that is not a |
| 85 | # comment or import that is a valid directory name for where the .pth |
| 86 | # file resides; invalid directories are not added |
| 87 | pth_file = PthFile() |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 88 | pth_file.cleanup(prep=True) # to make sure that nothing is |
| 89 | # pre-existing that shouldn't be |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 90 | try: |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 91 | pth_file.create() |
| 92 | site.addpackage(pth_file.base_dir, pth_file.filename, set()) |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 93 | self.pth_file_tests(pth_file) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 94 | finally: |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 95 | pth_file.cleanup() |
Raymond Hettinger | ebd9522 | 2004-06-27 03:02:18 +0000 | [diff] [blame] | 96 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 97 | def test_addsitedir(self): |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 98 | # Same tests for test_addpackage since addsitedir() essentially just |
| 99 | # calls addpackage() for every .pth file in the directory |
| 100 | pth_file = PthFile() |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 101 | pth_file.cleanup(prep=True) # Make sure that nothing is pre-existing |
| 102 | # that is tested for |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 103 | try: |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 104 | pth_file.create() |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 105 | site.addsitedir(pth_file.base_dir, set()) |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 106 | self.pth_file_tests(pth_file) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 107 | finally: |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 108 | pth_file.cleanup() |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 109 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 110 | def test_s_option(self): |
| 111 | usersite = site.USER_SITE |
Antoine Pitrou | 9166e6a | 2009-11-01 23:55:40 +0000 | [diff] [blame] | 112 | self.assertIn(usersite, sys.path) |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 113 | |
| 114 | rc = subprocess.call([sys.executable, '-c', |
Benjamin Peterson | fea6a94 | 2008-07-02 16:11:42 +0000 | [diff] [blame] | 115 | 'import sys; sys.exit(%r in sys.path)' % usersite]) |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 116 | self.assertEqual(rc, 1) |
| 117 | |
| 118 | rc = subprocess.call([sys.executable, '-s', '-c', |
Benjamin Peterson | fea6a94 | 2008-07-02 16:11:42 +0000 | [diff] [blame] | 119 | 'import sys; sys.exit(%r in sys.path)' % usersite]) |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 120 | self.assertEqual(rc, 0) |
| 121 | |
| 122 | env = os.environ.copy() |
| 123 | env["PYTHONNOUSERSITE"] = "1" |
| 124 | rc = subprocess.call([sys.executable, '-c', |
Benjamin Peterson | fea6a94 | 2008-07-02 16:11:42 +0000 | [diff] [blame] | 125 | 'import sys; sys.exit(%r in sys.path)' % usersite], |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 126 | env=env) |
| 127 | self.assertEqual(rc, 0) |
| 128 | |
| 129 | env = os.environ.copy() |
| 130 | env["PYTHONUSERBASE"] = "/tmp" |
| 131 | rc = subprocess.call([sys.executable, '-c', |
| 132 | 'import sys, site; sys.exit(site.USER_BASE.startswith("/tmp"))'], |
| 133 | env=env) |
| 134 | self.assertEqual(rc, 1) |
| 135 | |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 136 | def test_getuserbase(self): |
| 137 | site.USER_BASE = None |
| 138 | user_base = site.getuserbase() |
| 139 | |
| 140 | # the call sets site.USER_BASE |
| 141 | self.assertEquals(site.USER_BASE, user_base) |
| 142 | |
| 143 | # let's set PYTHONUSERBASE and see if it uses it |
| 144 | site.USER_BASE = None |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 145 | import sysconfig |
| 146 | sysconfig._CONFIG_VARS = None |
| 147 | |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 148 | with EnvironmentVarGuard() as environ: |
| 149 | environ['PYTHONUSERBASE'] = 'xoxo' |
Antoine Pitrou | 9166e6a | 2009-11-01 23:55:40 +0000 | [diff] [blame] | 150 | self.assertTrue(site.getuserbase().startswith('xoxo'), |
| 151 | site.getuserbase()) |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 152 | |
| 153 | def test_getusersitepackages(self): |
| 154 | site.USER_SITE = None |
| 155 | site.USER_BASE = None |
| 156 | user_site = site.getusersitepackages() |
| 157 | |
| 158 | # the call sets USER_BASE *and* USER_SITE |
| 159 | self.assertEquals(site.USER_SITE, user_site) |
Antoine Pitrou | 9166e6a | 2009-11-01 23:55:40 +0000 | [diff] [blame] | 160 | self.assertTrue(user_site.startswith(site.USER_BASE), user_site) |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 161 | |
| 162 | def test_getsitepackages(self): |
| 163 | site.PREFIXES = ['xoxo'] |
| 164 | dirs = site.getsitepackages() |
| 165 | |
| 166 | if sys.platform in ('os2emx', 'riscos'): |
Antoine Pitrou | 9166e6a | 2009-11-01 23:55:40 +0000 | [diff] [blame] | 167 | self.assertEqual(len(dirs), 1) |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 168 | wanted = os.path.join('xoxo', 'Lib', 'site-packages') |
| 169 | self.assertEquals(dirs[0], wanted) |
| 170 | elif os.sep == '/': |
| 171 | self.assertTrue(len(dirs), 2) |
| 172 | wanted = os.path.join('xoxo', 'lib', 'python' + sys.version[:3], |
| 173 | 'site-packages') |
| 174 | self.assertEquals(dirs[0], wanted) |
| 175 | wanted = os.path.join('xoxo', 'lib', 'site-python') |
| 176 | self.assertEquals(dirs[1], wanted) |
| 177 | else: |
| 178 | self.assertTrue(len(dirs), 2) |
| 179 | self.assertEquals(dirs[0], 'xoxo') |
Tarek Ziadé | 8c0e217 | 2009-10-27 21:24:21 +0000 | [diff] [blame] | 180 | wanted = os.path.join('xoxo', 'lib', 'site-packages') |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 181 | self.assertEquals(dirs[1], wanted) |
| 182 | |
| 183 | # let's try the specific Apple location |
| 184 | if sys.platform == "darwin": |
| 185 | site.PREFIXES = ['Python.framework'] |
| 186 | dirs = site.getsitepackages() |
Antoine Pitrou | 9166e6a | 2009-11-01 23:55:40 +0000 | [diff] [blame] | 187 | self.assertEqual(len(dirs), 4) |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 188 | wanted = os.path.join('~', 'Library', 'Python', |
| 189 | sys.version[:3], 'site-packages') |
| 190 | self.assertEquals(dirs[2], os.path.expanduser(wanted)) |
| 191 | wanted = os.path.join('/Library', 'Python', sys.version[:3], |
| 192 | 'site-packages') |
| 193 | self.assertEquals(dirs[3], wanted) |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 194 | |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 195 | class PthFile(object): |
| 196 | """Helper class for handling testing of .pth files""" |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 197 | |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 198 | def __init__(self, filename_base=TESTFN, imported="time", |
| 199 | good_dirname="__testdir__", bad_dirname="__bad"): |
| 200 | """Initialize instance variables""" |
| 201 | self.filename = filename_base + ".pth" |
| 202 | self.base_dir = os.path.abspath('') |
| 203 | self.file_path = os.path.join(self.base_dir, self.filename) |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 204 | self.imported = imported |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 205 | self.good_dirname = good_dirname |
| 206 | self.bad_dirname = bad_dirname |
| 207 | self.good_dir_path = os.path.join(self.base_dir, self.good_dirname) |
| 208 | self.bad_dir_path = os.path.join(self.base_dir, self.bad_dirname) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 209 | |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 210 | def create(self): |
| 211 | """Create a .pth file with a comment, blank lines, an ``import |
| 212 | <self.imported>``, a line with self.good_dirname, and a line with |
| 213 | self.bad_dirname. |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 214 | |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 215 | Creation of the directory for self.good_dir_path (based off of |
| 216 | self.good_dirname) is also performed. |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 217 | |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 218 | Make sure to call self.cleanup() to undo anything done by this method. |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 219 | |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 220 | """ |
Michael W. Hudson | ff52286 | 2005-05-27 14:58:06 +0000 | [diff] [blame] | 221 | FILE = open(self.file_path, 'w') |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 222 | try: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 223 | print("#import @bad module name", file=FILE) |
| 224 | print("\n", file=FILE) |
| 225 | print("import %s" % self.imported, file=FILE) |
| 226 | print(self.good_dirname, file=FILE) |
| 227 | print(self.bad_dirname, file=FILE) |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 228 | finally: |
| 229 | FILE.close() |
| 230 | os.mkdir(self.good_dir_path) |
| 231 | |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 232 | def cleanup(self, prep=False): |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 233 | """Make sure that the .pth file is deleted, self.imported is not in |
| 234 | sys.modules, and that both self.good_dirname and self.bad_dirname are |
| 235 | not existing directories.""" |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 236 | if os.path.exists(self.file_path): |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 237 | os.remove(self.file_path) |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 238 | if prep: |
| 239 | self.imported_module = sys.modules.get(self.imported) |
| 240 | if self.imported_module: |
| 241 | del sys.modules[self.imported] |
| 242 | else: |
| 243 | if self.imported_module: |
| 244 | sys.modules[self.imported] = self.imported_module |
| 245 | if os.path.exists(self.good_dir_path): |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 246 | os.rmdir(self.good_dir_path) |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 247 | if os.path.exists(self.bad_dir_path): |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 248 | os.rmdir(self.bad_dir_path) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 249 | |
| 250 | class ImportSideEffectTests(unittest.TestCase): |
| 251 | """Test side-effects from importing 'site'.""" |
| 252 | |
| 253 | def setUp(self): |
| 254 | """Make a copy of sys.path""" |
| 255 | self.sys_path = sys.path[:] |
| 256 | |
| 257 | def tearDown(self): |
| 258 | """Restore sys.path""" |
Nick Coghlan | 6ead552 | 2009-10-18 13:19:33 +0000 | [diff] [blame] | 259 | sys.path[:] = self.sys_path |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 260 | |
| 261 | def test_abs__file__(self): |
| 262 | # Make sure all imported modules have their __file__ attribute |
| 263 | # as an absolute path. |
| 264 | # Handled by abs__file__() |
| 265 | site.abs__file__() |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 266 | for module in (sys, os, builtins): |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 267 | try: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 268 | self.assertTrue(os.path.isabs(module.__file__), repr(module)) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 269 | except AttributeError: |
| 270 | continue |
Raymond Hettinger | ebd9522 | 2004-06-27 03:02:18 +0000 | [diff] [blame] | 271 | # We could try everything in sys.modules; however, when regrtest.py |
| 272 | # runs something like test_frozen before test_site, then we will |
| 273 | # be testing things loaded *after* test_site did path normalization |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 274 | |
| 275 | def test_no_duplicate_paths(self): |
| 276 | # No duplicate paths should exist in sys.path |
| 277 | # Handled by removeduppaths() |
| 278 | site.removeduppaths() |
| 279 | seen_paths = set() |
| 280 | for path in sys.path: |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 281 | self.assertNotIn(path, seen_paths) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 282 | seen_paths.add(path) |
| 283 | |
| 284 | def test_add_build_dir(self): |
| 285 | # Test that the build directory's Modules directory is used when it |
| 286 | # should be. |
| 287 | # XXX: implement |
| 288 | pass |
| 289 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 290 | def test_setting_quit(self): |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 291 | # 'quit' and 'exit' should be injected into builtins |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 292 | self.assertTrue(hasattr(builtins, "quit")) |
| 293 | self.assertTrue(hasattr(builtins, "exit")) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 294 | |
| 295 | def test_setting_copyright(self): |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 296 | # 'copyright' and 'credits' should be in builtins |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 297 | self.assertTrue(hasattr(builtins, "copyright")) |
| 298 | self.assertTrue(hasattr(builtins, "credits")) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 299 | |
| 300 | def test_setting_help(self): |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 301 | # 'help' should be set in builtins |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 302 | self.assertTrue(hasattr(builtins, "help")) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 303 | |
| 304 | def test_aliasing_mbcs(self): |
| 305 | if sys.platform == "win32": |
| 306 | import locale |
| 307 | if locale.getdefaultlocale()[1].startswith('cp'): |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 308 | for value in encodings.aliases.aliases.values(): |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 309 | if value == "mbcs": |
| 310 | break |
| 311 | else: |
| 312 | self.fail("did not alias mbcs") |
| 313 | |
| 314 | def test_setdefaultencoding_removed(self): |
| 315 | # Make sure sys.setdefaultencoding is gone |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 316 | self.assertTrue(not hasattr(sys, "setdefaultencoding")) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 317 | |
| 318 | def test_sitecustomize_executed(self): |
| 319 | # If sitecustomize is available, it should have been imported. |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 320 | if "sitecustomize" not in sys.modules: |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 321 | try: |
| 322 | import sitecustomize |
| 323 | except ImportError: |
| 324 | pass |
| 325 | else: |
| 326 | self.fail("sitecustomize not imported automatically") |
| 327 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 328 | def test_main(): |
| 329 | run_unittest(HelperFunctionsTests, ImportSideEffectTests) |
| 330 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 331 | if __name__ == "__main__": |
| 332 | test_main() |