Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d22048f

Browse files
committed
Fixes for Jorgen's branch in tests to genutils.
- Make sure that tests run correctly with out write access to test dir. - Fix failure in Unix that depended on where the tests were being run from. - Cleanup: uppercase globals and reuse them rather than recomputing hardcoded paths multiple time.
1 parent 0a36f4e commit d22048f

3 files changed

Lines changed: 48 additions & 30 deletions

File tree

IPython/genutils.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,11 @@ def get_home_dir():
942942
if not isdir(homedir):
943943
# in case a user stuck some string which does NOT resolve to a
944944
# valid path, it's as good as if we hadn't foud it
945-
raise KeyError
945+
946+
#raise KeyError # dbg
947+
# dbg - figuring out what's going on here
948+
pp = os.listdir(homedir+'/..')
949+
raise ValueError('Wrong dir: %s\n%s' % (homedir,pp)) # dbg
946950
return homedir
947951
except KeyError:
948952
if os.name == 'posix':

IPython/iplib.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,6 +1371,7 @@ def init_readline(self):
13711371
# not run as the syntax for libedit is different.
13721372
if not readline.uses_libedit:
13731373
for rlcommand in self.rc.readline_parse_and_bind:
1374+
#print "loading rl:",rlcommand # dbg
13741375
readline.parse_and_bind(rlcommand)
13751376

13761377
# remove some chars from the delimiters list

IPython/tests/test_genutils.py

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,26 @@
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

2324
from os.path import join, abspath, split
24-
import os, sys, IPython
25+
26+
# third-party
2527
import 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
2938
try:
3039
import _winreg as wreg
3140
except ImportError:
@@ -36,32 +45,36 @@
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-
4660
def 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

5669
def 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

6780
def 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
130143
def 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

Comments
 (0)