1515# Imports
1616#-----------------------------------------------------------------------------
1717
18- from IPython import genutils
19- from IPython .testing .decorators import skipif , skip_if_not_win32
20- from nose import with_setup
21- from nose .tools import raises
18+ # stdlib
19+ import os
20+ import shutil
21+ import sys
22+ import tempfile
2223
2324from os .path import join , abspath , split
24- import os , sys , IPython
25+
26+ # third-party
2527import nose .tools as nt
2628
27- env = os .environ
29+ from nose import with_setup
30+ from nose .tools import raises
2831
32+ # Our own
33+ import IPython
34+ from IPython import genutils
35+ from IPython .testing .decorators import skipif , skip_if_not_win32
36+
37+ # Platform-dependent imports
2938try :
3039 import _winreg as wreg
3140except ImportError :
3645 #Add entries that needs to be stubbed by the testing code
3746 (wreg .OpenKey , wreg .QueryValueEx ,) = (None , None )
3847
39- test_file_path = split (abspath (__file__ ))[0 ]
40-
48+ #-----------------------------------------------------------------------------
49+ # Globals
50+ #-----------------------------------------------------------------------------
51+ env = os .environ
52+ TEST_FILE_PATH = split (abspath (__file__ ))[0 ]
53+ TMP_TEST_DIR = tempfile .mkdtemp ()
54+ HOME_TEST_DIR = join (TMP_TEST_DIR , "home_test_dir" )
55+ IP_TEST_DIR = join (HOME_TEST_DIR ,'_ipython' )
4156#
4257# Setup/teardown functions/decorators
4358#
4459
45-
4660def setup ():
4761 """Setup testenvironment for the module:
4862
4963 - Adds dummy home dir tree
5064 """
51- try :
52- os .makedirs ("home_test_dir/_ipython" )
53- except WindowsError :
54- pass #Or should we complain that the test directory already exists??
65+ # Do not mask exceptions here. In particular, catching WindowsError is a
66+ # problem because that exception is only defined on Windows...
67+ os .makedirs (IP_TEST_DIR )
5568
5669def teardown ():
5770 """Teardown testenvironment for the module:
5871
5972 - Remove dummy home dir tree
6073 """
61- try :
62- os .removedirs ( "home_test_dir/_ipython" )
63- except WindowsError :
64- pass #Or should we complain that the test directory already exists??
74+ # Note: we remove the parent test dir, which is the root of all test
75+ # subdirs we may have created. Use shutil instead of os.removedirs, so
76+ # that non-empty directories are all recursively removed.
77+ shutil . rmtree ( TMP_TEST_DIR )
6578
6679
6780def setup_environment ():
@@ -109,10 +122,10 @@ def test_get_home_dir_1():
109122 sys .frozen = True
110123
111124 #fake filename for IPython.__init__
112- IPython .__file__ = abspath (join (test_file_path , "home_test_dir/Lib/IPython/__init__.py" ))
125+ IPython .__file__ = abspath (join (TEST_FILE_PATH , "home_test_dir/Lib/IPython/__init__.py" ))
113126
114127 home_dir = genutils .get_home_dir ()
115- nt .assert_equal (home_dir , abspath (join ( test_file_path , "home_test_dir" ) ))
128+ nt .assert_equal (home_dir , abspath (HOME_TEST_DIR ))
116129
117130@skip_if_not_win32
118131@with_enivronment
@@ -121,15 +134,15 @@ def test_get_home_dir_2():
121134 """
122135 sys .frozen = True
123136 #fake filename for IPython.__init__
124- IPython .__file__ = abspath (join (test_file_path , "home_test_dir/Library.zip/IPython/__init__.py" )).lower ()
137+ IPython .__file__ = abspath (join (TEST_FILE_PATH , "home_test_dir/Library.zip/IPython/__init__.py" )).lower ()
125138
126139 home_dir = genutils .get_home_dir ()
127- nt .assert_equal (home_dir , abspath (join ( test_file_path , "home_test_dir" ) ).lower ())
140+ nt .assert_equal (home_dir , abspath (HOME_TEST_DIR ).lower ())
128141
129142@with_enivronment
130143def test_get_home_dir_3 ():
131144 """Testcase $HOME is set, then use its value as home directory."""
132- env ["HOME" ] = join ( test_file_path , "home_test_dir" )
145+ env ["HOME" ] = HOME_TEST_DIR
133146 home_dir = genutils .get_home_dir ()
134147 nt .assert_equal (home_dir , env ["HOME" ])
135148
@@ -150,10 +163,10 @@ def test_get_home_dir_5():
150163
151164 os .name = 'nt'
152165 if 'HOME' in env : del env ['HOME' ]
153- env ['HOMEDRIVE' ], env ['HOMEPATH' ] = os .path .abspath (test_file_path ), "home_test_dir"
166+ env ['HOMEDRIVE' ], env ['HOMEPATH' ] = os .path .abspath (TEST_FILE_PATH ), "home_test_dir"
154167
155168 home_dir = genutils .get_home_dir ()
156- nt .assert_equal (home_dir , abspath (join ( test_file_path , "home_test_dir" ) ))
169+ nt .assert_equal (home_dir , abspath (HOME_TEST_DIR ))
157170
158171@skip_if_not_win32
159172@with_enivronment
@@ -165,11 +178,11 @@ def test_get_home_dir_6():
165178
166179 os .name = 'nt'
167180 if 'HOME' in env : del env ['HOME' ]
168- env ['HOMEDRIVE' ], env ['HOMEPATH' ] = os .path .abspath (test_file_path ), "DOES NOT EXIST"
169- env ["USERPROFILE" ] = abspath (join ( test_file_path , "home_test_dir" ) )
181+ env ['HOMEDRIVE' ], env ['HOMEPATH' ] = os .path .abspath (TEST_FILE_PATH ), "DOES NOT EXIST"
182+ env ["USERPROFILE" ] = abspath (HOME_TEST_DIR )
170183
171184 home_dir = genutils .get_home_dir ()
172- nt .assert_equal (home_dir , abspath (join ( test_file_path , "home_test_dir" ) ))
185+ nt .assert_equal (home_dir , abspath (HOME_TEST_DIR ))
173186
174187# Should we stub wreg fully so we can run the test on all platforms?
175188@skip_if_not_win32
@@ -189,13 +202,13 @@ def Close(self):
189202 pass
190203 return key ()
191204 def QueryValueEx (x , y ):
192- return [abspath (join ( test_file_path , "home_test_dir" ) )]
205+ return [abspath (HOME_TEST_DIR )]
193206
194207 wreg .OpenKey = OpenKey
195208 wreg .QueryValueEx = QueryValueEx
196209
197210 home_dir = genutils .get_home_dir ()
198- nt .assert_equal (home_dir , abspath (join ( test_file_path , "home_test_dir" ) ))
211+ nt .assert_equal (home_dir , abspath (HOME_TEST_DIR ))
199212
200213
201214#
0 commit comments