Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(21)

Side by Side Diff: Lib/test/test_site.py

Issue 11591: "python -S" should be robust against e.g. "from site import addsitedir"
Patch Set: Created 8 years, 7 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 """Tests for 'site'. 1 """Tests for 'site'.
2 2
3 Tests assume the initial paths in sys.path once the interpreter has begun 3 Tests assume the initial paths in sys.path once the interpreter has begun
4 executing have not been removed. 4 executing have not been removed.
5 5
6 """ 6 """
7 import unittest 7 import unittest
8 from test.support import run_unittest, TESTFN, EnvironmentVarGuard 8 from test.support import run_unittest, TESTFN, EnvironmentVarGuard
9 from test.support import captured_stderr 9 from test.support import captured_stderr
10 import builtins 10 import builtins
11 import os 11 import os
12 import sys 12 import sys
13 import re 13 import re
14 import site
14 import encodings 15 import encodings
15 import subprocess 16 import subprocess
16 import sysconfig 17 import sysconfig
17 from copy import copy 18 from copy import copy
18 19
19 # Need to make sure to not import 'site' if someone specified ``-S`` at the 20 site.getusersitepackages()
20 # command-line. Detect this by just making sure 'site' has not been imported 21 skip_unless_user_site = unittest.skipUnless(
21 # already. 22 site.ENABLE_USER_SITE, 'user site-packages disabled by -s or -S')
22 if "site" in sys.modules:
23 import site
24 else:
25 raise unittest.SkipTest("importation of site.py suppressed")
26 23
27 if not os.path.isdir(site.USER_SITE): 24 if not os.path.isdir(site.USER_SITE):
28 # need to add user site directory for tests 25 # need to add user site directory for tests
29 os.makedirs(site.USER_SITE) 26 os.makedirs(site.USER_SITE)
30 site.addsitedir(site.USER_SITE) 27 site.addsitedir(site.USER_SITE)
31 28
32 class HelperFunctionsTests(unittest.TestCase): 29 class HelperFunctionsTests(unittest.TestCase):
33 """Tests for helper functions. 30 """Tests for helper functions.
34 """ 31 """
35 32
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 # that is tested for 149 # that is tested for
153 try: 150 try:
154 pth_file.create() 151 pth_file.create()
155 site.addsitedir(pth_file.base_dir, set()) 152 site.addsitedir(pth_file.base_dir, set())
156 self.pth_file_tests(pth_file) 153 self.pth_file_tests(pth_file)
157 finally: 154 finally:
158 pth_file.cleanup() 155 pth_file.cleanup()
159 156
160 def test_s_option(self): 157 def test_s_option(self):
161 usersite = site.USER_SITE 158 usersite = site.USER_SITE
162 self.assertIn(usersite, sys.path) 159 if site.ENABLE_USER_SITE:
160 self.assertIn(usersite, sys.path)
161 else:
162 self.assertNotIn(usersite, sys.path)
163 163
164 env = os.environ.copy() 164 env = os.environ.copy()
165 rc = subprocess.call([sys.executable, '-c', 165 rc = subprocess.call([sys.executable, '-c',
166 'import sys; sys.exit(%r in sys.path)' % usersite], 166 'import sys; sys.exit(%r in sys.path)' % usersite],
167 env=env) 167 env=env)
168 self.assertEqual(rc, 1) 168 self.assertEqual(rc, 1)
169 169
170 env = os.environ.copy() 170 env = os.environ.copy()
171 rc = subprocess.call([sys.executable, '-s', '-c', 171 rc = subprocess.call([sys.executable, '-s', '-c',
172 'import sys; sys.exit(%r in sys.path)' % usersite], 172 'import sys; sys.exit(%r in sys.path)' % usersite],
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
356 for path in sys.path: 356 for path in sys.path:
357 self.assertNotIn(path, seen_paths) 357 self.assertNotIn(path, seen_paths)
358 seen_paths.add(path) 358 seen_paths.add(path)
359 359
360 def test_add_build_dir(self): 360 def test_add_build_dir(self):
361 # Test that the build directory's Modules directory is used when it 361 # Test that the build directory's Modules directory is used when it
362 # should be. 362 # should be.
363 # XXX: implement 363 # XXX: implement
364 pass 364 pass
365 365
366 @skip_unless_user_site
366 def test_setting_quit(self): 367 def test_setting_quit(self):
367 # 'quit' and 'exit' should be injected into builtins 368 # 'quit' and 'exit' should be injected into builtins
368 self.assertTrue(hasattr(builtins, "quit")) 369 self.assertTrue(hasattr(builtins, "quit"))
369 self.assertTrue(hasattr(builtins, "exit")) 370 self.assertTrue(hasattr(builtins, "exit"))
370 371
372 @skip_unless_user_site
371 def test_setting_copyright(self): 373 def test_setting_copyright(self):
372 # 'copyright' and 'credits' should be in builtins 374 # 'copyright' and 'credits' should be in builtins
373 self.assertTrue(hasattr(builtins, "copyright")) 375 self.assertTrue(hasattr(builtins, "copyright"))
374 self.assertTrue(hasattr(builtins, "credits")) 376 self.assertTrue(hasattr(builtins, "credits"))
375 377
378 @skip_unless_user_site
376 def test_setting_help(self): 379 def test_setting_help(self):
377 # 'help' should be set in builtins 380 # 'help' should be set in builtins
378 self.assertTrue(hasattr(builtins, "help")) 381 self.assertTrue(hasattr(builtins, "help"))
379 382
380 def test_aliasing_mbcs(self): 383 def test_aliasing_mbcs(self):
381 if sys.platform == "win32": 384 if sys.platform == "win32":
382 import locale 385 import locale
383 if locale.getdefaultlocale()[1].startswith('cp'): 386 if locale.getdefaultlocale()[1].startswith('cp'):
384 for value in encodings.aliases.aliases.values(): 387 for value in encodings.aliases.aliases.values():
385 if value == "mbcs": 388 if value == "mbcs":
386 break 389 break
387 else: 390 else:
388 self.fail("did not alias mbcs") 391 self.fail("did not alias mbcs")
389 392
390 def test_sitecustomize_executed(self): 393 def test_sitecustomize_executed(self):
391 # If sitecustomize is available, it should have been imported. 394 # If sitecustomize is available, it should have been imported.
392 if "sitecustomize" not in sys.modules: 395 if "sitecustomize" not in sys.modules:
393 try: 396 try:
394 import sitecustomize 397 import sitecustomize
395 except ImportError: 398 except ImportError:
396 pass 399 pass
397 else: 400 else:
398 self.fail("sitecustomize not imported automatically") 401 self.fail("sitecustomize not imported automatically")
399 402
400 def test_main(): 403 def test_main():
401 run_unittest(HelperFunctionsTests, ImportSideEffectTests) 404 run_unittest(HelperFunctionsTests, ImportSideEffectTests)
402 405
403 if __name__ == "__main__": 406 if __name__ == "__main__":
404 test_main() 407 test_main()
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+