3 Tests assume the initial paths in sys.path once the interpreter has begun
4 executing have not been removed.
8 from test
.test_support
import TestSkipped
, TestFailed
, run_unittest
, TESTFN
14 # Need to make sure to not import 'site' if someone specified ``-S`` at the
15 # command-line. Detect this by just making sure 'site' has not been imported
17 if "site" in sys
.modules
:
20 raise TestSkipped("importation of site.py suppressed")
22 class HelperFunctionsTests(unittest
.TestCase
):
23 """Tests for helper functions.
25 The setting of the encoding (set using sys.setdefaultencoding) used by
26 the Unicode implementation is not tested.
31 """Save a copy of sys.path"""
32 self
.sys_path
= sys
.path
[:]
35 """Restore sys.path"""
36 sys
.path
= self
.sys_path
38 def test_makepath(self
):
39 # Test makepath() have an absolute path for its first return value
40 # and a case-normalized version of the absolute path for its
42 path_parts
= ("Beginning", "End")
43 original_dir
= os
.path
.join(*path_parts
)
44 abs_dir
, norm_dir
= site
.makepath(*path_parts
)
45 self
.failUnlessEqual(os
.path
.abspath(original_dir
), abs_dir
)
46 if original_dir
== os
.path
.normcase(original_dir
):
47 self
.failUnlessEqual(abs_dir
, norm_dir
)
49 self
.failUnlessEqual(os
.path
.normcase(abs_dir
), norm_dir
)
51 def test_init_pathinfo(self
):
52 dir_set
= site
._init
_pathinfo
()
53 for entry
in [site
.makepath(path
)[1] for path
in sys
.path
54 if path
and os
.path
.isdir(path
)]:
55 self
.failUnless(entry
in dir_set
,
56 "%s from sys.path not found in set returned "
57 "by _init_pathinfo(): %s" % (entry
, dir_set
))
59 def pth_file_tests(self
, pth_file
):
60 """Contain common code for testing results of reading a .pth file"""
61 self
.failUnless(pth_file
.imported
in sys
.modules
,
62 "%s not in sys.path" % pth_file
.imported
)
63 self
.failUnless(site
.makepath(pth_file
.good_dir_path
)[0] in sys
.path
)
64 self
.failUnless(not os
.path
.exists(pth_file
.bad_dir_path
))
66 def test_addpackage(self
):
67 # Make sure addpackage() imports if the line starts with 'import',
68 # adds directories to sys.path for any line in the file that is not a
69 # comment or import that is a valid directory name for where the .pth
70 # file resides; invalid directories are not added
72 pth_file
.cleanup(prep
=True) # to make sure that nothing is
73 # pre-existing that shouldn't be
76 site
.addpackage(pth_file
.base_dir
, pth_file
.filename
, set())
77 self
.pth_file_tests(pth_file
)
81 def test_addsitedir(self
):
82 # Same tests for test_addpackage since addsitedir() essentially just
83 # calls addpackage() for every .pth file in the directory
85 pth_file
.cleanup(prep
=True) # Make sure that nothing is pre-existing
89 site
.addsitedir(pth_file
.base_dir
, set())
90 self
.pth_file_tests(pth_file
)
94 class PthFile(object):
95 """Helper class for handling testing of .pth files"""
97 def __init__(self
, filename_base
=TESTFN
, imported
="time",
98 good_dirname
="__testdir__", bad_dirname
="__bad"):
99 """Initialize instance variables"""
100 self
.filename
= filename_base
+ ".pth"
101 self
.base_dir
= os
.path
.abspath('')
102 self
.file_path
= os
.path
.join(self
.base_dir
, self
.filename
)
103 self
.imported
= imported
104 self
.good_dirname
= good_dirname
105 self
.bad_dirname
= bad_dirname
106 self
.good_dir_path
= os
.path
.join(self
.base_dir
, self
.good_dirname
)
107 self
.bad_dir_path
= os
.path
.join(self
.base_dir
, self
.bad_dirname
)
110 """Create a .pth file with a comment, blank lines, an ``import
111 <self.imported>``, a line with self.good_dirname, and a line with
114 Creation of the directory for self.good_dir_path (based off of
115 self.good_dirname) is also performed.
117 Make sure to call self.cleanup() to undo anything done by this method.
120 FILE
= open(self
.file_path
, 'w')
122 print>>FILE
, "#import @bad module name"
124 print>>FILE
, "import %s" % self
.imported
125 print>>FILE
, self
.good_dirname
126 print>>FILE
, self
.bad_dirname
129 os
.mkdir(self
.good_dir_path
)
131 def cleanup(self
, prep
=False):
132 """Make sure that the .pth file is deleted, self.imported is not in
133 sys.modules, and that both self.good_dirname and self.bad_dirname are
134 not existing directories."""
135 if os
.path
.exists(self
.file_path
):
136 os
.remove(self
.file_path
)
138 self
.imported_module
= sys
.modules
.get(self
.imported
)
139 if self
.imported_module
:
140 del sys
.modules
[self
.imported
]
142 if self
.imported_module
:
143 sys
.modules
[self
.imported
] = self
.imported_module
144 if os
.path
.exists(self
.good_dir_path
):
145 os
.rmdir(self
.good_dir_path
)
146 if os
.path
.exists(self
.bad_dir_path
):
147 os
.rmdir(self
.bad_dir_path
)
149 class ImportSideEffectTests(unittest
.TestCase
):
150 """Test side-effects from importing 'site'."""
153 """Make a copy of sys.path"""
154 self
.sys_path
= sys
.path
[:]
157 """Restore sys.path"""
158 sys
.path
= self
.sys_path
160 def test_abs__file__(self
):
161 # Make sure all imported modules have their __file__ attribute
162 # as an absolute path.
163 # Handled by abs__file__()
165 for module
in (sys
, os
, __builtin__
):
167 self
.failUnless(os
.path
.isabs(module
.__file
__), `module`
)
168 except AttributeError:
170 # We could try everything in sys.modules; however, when regrtest.py
171 # runs something like test_frozen before test_site, then we will
172 # be testing things loaded *after* test_site did path normalization
174 def test_no_duplicate_paths(self
):
175 # No duplicate paths should exist in sys.path
176 # Handled by removeduppaths()
177 site
.removeduppaths()
179 for path
in sys
.path
:
180 self
.failUnless(path
not in seen_paths
)
183 def test_add_build_dir(self
):
184 # Test that the build directory's Modules directory is used when it
189 def test_setting_quit(self
):
190 # 'quit' and 'exit' should be injected into __builtin__
191 self
.failUnless(hasattr(__builtin__
, "quit"))
192 self
.failUnless(hasattr(__builtin__
, "exit"))
194 def test_setting_copyright(self
):
195 # 'copyright' and 'credits' should be in __builtin__
196 self
.failUnless(hasattr(__builtin__
, "copyright"))
197 self
.failUnless(hasattr(__builtin__
, "credits"))
199 def test_setting_help(self
):
200 # 'help' should be set in __builtin__
201 self
.failUnless(hasattr(__builtin__
, "help"))
203 def test_aliasing_mbcs(self
):
204 if sys
.platform
== "win32":
206 if locale
.getdefaultlocale()[1].startswith('cp'):
207 for value
in encodings
.aliases
.aliases
.itervalues():
211 self
.fail("did not alias mbcs")
213 def test_setdefaultencoding_removed(self
):
214 # Make sure sys.setdefaultencoding is gone
215 self
.failUnless(not hasattr(sys
, "setdefaultencoding"))
217 def test_sitecustomize_executed(self
):
218 # If sitecustomize is available, it should have been imported.
219 if not sys
.modules
.has_key("sitecustomize"):
225 self
.fail("sitecustomize not imported automatically")
231 run_unittest(HelperFunctionsTests
, ImportSideEffectTests
)
235 if __name__
== "__main__":