11"""Tests for distutils.command.install."""
22
33import os
4+ import imp
45import sys
56import unittest
67import site
@@ -67,10 +68,7 @@ def check_path(got, expected):
6768 check_path (cmd .install_data , destination )
6869
6970 def test_user_site (self ):
70- # site.USER_SITE was introduced in 2.6
71- if sys .version < '2.6' :
72- return
73-
71+ # test install with --user
7472 # preparing the environment for the test
7573 self .old_user_base = site .USER_BASE
7674 self .old_user_site = site .USER_SITE
@@ -87,34 +85,32 @@ def _expanduser(path):
8785 self .old_expand = os .path .expanduser
8886 os .path .expanduser = _expanduser
8987
90- try :
91- # this is the actual test
92- self ._test_user_site ()
93- finally :
88+ def cleanup ():
9489 site .USER_BASE = self .old_user_base
9590 site .USER_SITE = self .old_user_site
9691 install_module .USER_BASE = self .old_user_base
9792 install_module .USER_SITE = self .old_user_site
9893 os .path .expanduser = self .old_expand
9994
100- def _test_user_site (self ):
95+ self .addCleanup (cleanup )
96+
10197 for key in ('nt_user' , 'unix_user' , 'os2_home' ):
102- self .assertTrue (key in INSTALL_SCHEMES )
98+ self .assertIn (key , INSTALL_SCHEMES )
10399
104100 dist = Distribution ({'name' : 'xx' })
105101 cmd = install (dist )
106102
107103 # making sure the user option is there
108104 options = [name for name , short , lable in
109105 cmd .user_options ]
110- self .assertTrue ('user' in options )
106+ self .assertIn ('user' , options )
111107
112108 # setting a value
113109 cmd .user = 1
114110
115111 # user base and site shouldn't be created yet
116- self .assertTrue ( not os .path .exists (self .user_base ))
117- self .assertTrue ( not os .path .exists (self .user_site ))
112+ self .assertFalse ( os .path .exists (self .user_base ))
113+ self .assertFalse ( os .path .exists (self .user_site ))
118114
119115 # let's run finalize
120116 cmd .ensure_finalized ()
@@ -123,8 +119,8 @@ def _test_user_site(self):
123119 self .assertTrue (os .path .exists (self .user_base ))
124120 self .assertTrue (os .path .exists (self .user_site ))
125121
126- self .assertTrue ('userbase' in cmd .config_vars )
127- self .assertTrue ('usersite' in cmd .config_vars )
122+ self .assertIn ('userbase' , cmd .config_vars )
123+ self .assertIn ('usersite' , cmd .config_vars )
128124
129125 def test_handle_extra_path (self ):
130126 dist = Distribution ({'name' : 'xx' , 'extra_path' : 'path,dirs' })
@@ -177,15 +173,16 @@ def test_finalize_options(self):
177173
178174 def test_record (self ):
179175 install_dir = self .mkdtemp ()
180- project_dir , dist = self .create_dist (scripts = ['hello' ])
181- self . addCleanup ( os . chdir , os . getcwd () )
176+ project_dir , dist = self .create_dist (py_modules = ['hello' ],
177+ scripts = [ 'sayhi' ] )
182178 os .chdir (project_dir )
183- self .write_file ('hello' , "print('o hai')" )
179+ self .write_file ('hello.py' , "def main(): print('o hai')" )
180+ self .write_file ('sayhi' , 'from hello import main; main()' )
184181
185182 cmd = install (dist )
186183 dist .command_obj ['install' ] = cmd
187184 cmd .root = install_dir
188- cmd .record = os .path .join (project_dir , 'RECORD ' )
185+ cmd .record = os .path .join (project_dir , 'filelist ' )
189186 cmd .ensure_finalized ()
190187 cmd .run ()
191188
@@ -196,15 +193,14 @@ def test_record(self):
196193 f .close ()
197194
198195 found = [os .path .basename (line ) for line in content .splitlines ()]
199- expected = ['hello' ,
196+ expected = ['hello.py' , 'hello.%s.pyc' % imp . get_tag (), 'sayhi ' ,
200197 'UNKNOWN-0.0.0-py%s.%s.egg-info' % sys .version_info [:2 ]]
201198 self .assertEqual (found , expected )
202199
203200 def test_record_extensions (self ):
204201 install_dir = self .mkdtemp ()
205202 project_dir , dist = self .create_dist (ext_modules = [
206203 Extension ('xx' , ['xxmodule.c' ])])
207- self .addCleanup (os .chdir , os .getcwd ())
208204 os .chdir (project_dir )
209205 support .copy_xxmodule_c (project_dir )
210206
@@ -216,7 +212,7 @@ def test_record_extensions(self):
216212 dist .command_obj ['install' ] = cmd
217213 dist .command_obj ['build_ext' ] = buildextcmd
218214 cmd .root = install_dir
219- cmd .record = os .path .join (project_dir , 'RECORD ' )
215+ cmd .record = os .path .join (project_dir , 'filelist ' )
220216 cmd .ensure_finalized ()
221217 cmd .run ()
222218
@@ -242,6 +238,7 @@ def test_debug_mode(self):
242238 install_module .DEBUG = False
243239 self .assertTrue (len (self .logs ) > old_logs_len )
244240
241+
245242def test_suite ():
246243 return unittest .makeSuite (InstallTestCase )
247244
0 commit comments