Skip to content

Commit 9cc40da

Browse files
committed
revise tests
1 parent 28e2cd6 commit 9cc40da

File tree

1 file changed

+36
-14
lines changed

1 file changed

+36
-14
lines changed

nipype/utils/tests/test_config.py

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,15 @@
2828
spec=['vdisplay_num', 'start', 'stop'], vdisplay_num=2010)
2929

3030

31-
@pytest.mark.parametrize('disp_var', [':0', 'localhost:0', 'localhost:0.1'])
32-
def test_display_parse(monkeypatch, disp_var):
31+
@pytest.mark.parametrize('dispvar', [':12', 'localhost:12', 'localhost:12.1'])
32+
def test_display_parse(monkeypatch, dispvar):
3333
"""Check that when $DISPLAY is defined, the display is correctly parsed"""
3434
config._display = None
35-
dispstr = ':0'
36-
old_disp = os.getenv('DISPLAY')
37-
os.environ['DISPLAY'] = disp_var
38-
assert config.get_display() == dispstr
35+
config._config.remove_option('execution', 'display_variable')
36+
monkeypatch.setenv('DISPLAY', dispvar)
37+
assert config.get_display() == ':12'
3938
# Test that it was correctly cached
40-
assert config.get_display() == dispstr
41-
if old_disp is not None:
42-
os.environ['DISPLAY'] = old_disp
43-
else:
44-
del os.environ['DISPLAY']
39+
assert config.get_display() == ':12'
4540

4641

4742
@pytest.mark.parametrize('dispnum', range(5))
@@ -88,10 +83,17 @@ def test_display_noconfig_nosystem_patched(monkeypatch):
8883
config._config.remove_option('execution', 'display_variable')
8984
monkeypatch.delitem(os.environ, 'DISPLAY', raising=False)
9085
monkeypatch.setitem(sys.modules, 'xvfbwrapper', xvfbpatch)
86+
monkeypatch.setattr(sys, 'platform', value='linux')
9187
assert config.get_display() == ":2010"
9288
# Test that it was correctly cached
9389
assert config.get_display() == ':2010'
9490

91+
# Check that raises in Mac
92+
config._display = None
93+
monkeypatch.setattr(sys, 'platform', value='darwin')
94+
with pytest.raises(RuntimeError):
95+
config.get_display()
96+
9597

9698
def test_display_empty_patched(monkeypatch):
9799
"""
@@ -103,10 +105,16 @@ def test_display_empty_patched(monkeypatch):
103105
config._config.remove_option('execution', 'display_variable')
104106
monkeypatch.setenv('DISPLAY', '')
105107
monkeypatch.setitem(sys.modules, 'xvfbwrapper', xvfbpatch)
108+
monkeypatch.setattr(sys, 'platform', value='linux')
106109
assert config.get_display() == ':2010'
107110
# Test that it was correctly cached
108111
assert config.get_display() == ':2010'
109112

113+
# Check that raises in Mac
114+
config._display = None
115+
monkeypatch.setattr(sys, 'platform', value='darwin')
116+
with pytest.raises(RuntimeError):
117+
config.get_display()
110118

111119
def test_display_noconfig_nosystem_patched_oldxvfbwrapper(monkeypatch):
112120
"""
@@ -118,10 +126,16 @@ def test_display_noconfig_nosystem_patched_oldxvfbwrapper(monkeypatch):
118126
config._config.remove_option('execution', 'display_variable')
119127
monkeypatch.delitem(os.environ, 'DISPLAY', raising=False)
120128
monkeypatch.setitem(sys.modules, 'xvfbwrapper', xvfbpatch_old)
129+
monkeypatch.setattr(sys, 'platform', value='linux')
121130
assert config.get_display() == ":2010"
122131
# Test that it was correctly cached
123132
assert config.get_display() == ':2010'
124133

134+
# Check that raises in Mac
135+
config._display = None
136+
monkeypatch.setattr(sys, 'platform', value='darwin')
137+
with pytest.raises(RuntimeError):
138+
config.get_display()
125139

126140
def test_display_empty_patched_oldxvfbwrapper(monkeypatch):
127141
"""
@@ -133,10 +147,16 @@ def test_display_empty_patched_oldxvfbwrapper(monkeypatch):
133147
config._config.remove_option('execution', 'display_variable')
134148
monkeypatch.setenv('DISPLAY', '')
135149
monkeypatch.setitem(sys.modules, 'xvfbwrapper', xvfbpatch_old)
150+
monkeypatch.setattr(sys, 'platform', value='linux')
136151
assert config.get_display() == ':2010'
137152
# Test that it was correctly cached
138153
assert config.get_display() == ':2010'
139154

155+
# Check that raises in Mac
156+
config._display = None
157+
monkeypatch.setattr(sys, 'platform', value='darwin')
158+
with pytest.raises(RuntimeError):
159+
config.get_display()
140160

141161
def test_display_noconfig_nosystem_notinstalled(monkeypatch):
142162
"""
@@ -146,7 +166,7 @@ def test_display_noconfig_nosystem_notinstalled(monkeypatch):
146166
config._display = None
147167
if config.has_option('execution', 'display_variable'):
148168
config._config.remove_option('execution', 'display_variable')
149-
monkeypatch.delitem(os.environ, 'DISPLAY', raising=False)
169+
monkeypatch.delenv('DISPLAY', raising=False)
150170
monkeypatch.setitem(sys.modules, 'xvfbwrapper', None)
151171
with pytest.raises(RuntimeError):
152172
config.get_display()
@@ -167,6 +187,7 @@ def test_display_empty_notinstalled(monkeypatch):
167187

168188

169189
@pytest.mark.skipif(not has_Xvfb, reason='xvfbwrapper not installed')
190+
@pytest.mark.skipif('darwin' in sys.platform, reason='macosx requires root for Xvfb')
170191
def test_display_noconfig_nosystem_installed(monkeypatch):
171192
"""
172193
Check that actually uses xvfbwrapper when installed (not mocked)
@@ -175,14 +196,15 @@ def test_display_noconfig_nosystem_installed(monkeypatch):
175196
config._display = None
176197
if config.has_option('execution', 'display_variable'):
177198
config._config.remove_option('execution', 'display_variable')
178-
monkeypatch.delitem(os.environ, 'DISPLAY', raising=False)
199+
monkeypatch.delenv('DISPLAY', raising=False)
179200
newdisp = config.get_display()
180201
assert int(newdisp.split(':')[-1]) > 1000
181202
# Test that it was correctly cached
182203
assert config.get_display() == newdisp
183204

184205

185206
@pytest.mark.skipif(not has_Xvfb, reason='xvfbwrapper not installed')
207+
@pytest.mark.skipif('darwin' in sys.platform, reason='macosx requires root for Xvfb')
186208
def test_display_empty_installed(monkeypatch):
187209
"""
188210
Check that actually uses xvfbwrapper when installed (not mocked)
@@ -207,7 +229,7 @@ def test_display_empty_macosx(monkeypatch):
207229
config._display = None
208230
if config.has_option('execution', 'display_variable'):
209231
config._config.remove_option('execution', 'display_variable')
210-
monkeypatch.delitem(os.environ, 'DISPLAY', '')
232+
monkeypatch.delenv('DISPLAY', '')
211233

212234
monkeypatch.setattr(sys, 'platform', 'darwin')
213235
with pytest.raises(RuntimeError):

0 commit comments

Comments
 (0)