Skip to content

Commit 0a718c1

Browse files
committed
tests on new config
1 parent 8a16650 commit 0a718c1

File tree

2 files changed

+69
-8
lines changed

2 files changed

+69
-8
lines changed

nipype/utils/config.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,8 @@ def _mock():
217217
self._display.start()
218218

219219
# Older versions of Xvfb used vdisplay_num
220-
if hasattr(self._display, 'new_display'):
221-
setattr(self._display, 'vdisplay_num',
222-
self._display.new_display)
220+
if hasattr(self._display, 'vdisplay_num'):
221+
return ':%d' % self._display.vdisplay_num
223222

224-
return ':%d' % self._display.vdisplay_num
223+
if hasattr(self._display, 'new_display'):
224+
return ':%d' % self._display.new_display

nipype/utils/tests/test_config.py

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,21 @@
33
# vi: set ft=python sts=4 ts=4 sw=4 et:
44
from __future__ import print_function, division, unicode_literals, absolute_import
55
import os
6+
import sys
67
import pytest
78
from nipype import config
9+
from mock import MagicMock
10+
from builtins import object
11+
12+
try:
13+
import xvfbwrapper
14+
has_Xvfb = True
15+
except ImportError:
16+
has_Xvfb = False
17+
18+
xvfbpatch = MagicMock()
19+
xvfbpatch.Xvfb.return_value = MagicMock(vdisplay_num=2010)
20+
821

922
@pytest.mark.parametrize('dispnum', range(5))
1023
def test_display_config(monkeypatch, dispnum):
@@ -15,6 +28,7 @@ def test_display_config(monkeypatch, dispnum):
1528
monkeypatch.delitem(os.environ, 'DISPLAY', raising=False)
1629
assert config.get_display() == config.get('execution', 'display_variable')
1730

31+
1832
@pytest.mark.parametrize('dispnum', range(5))
1933
def test_display_system(monkeypatch, dispnum):
2034
"""Check that when only a $DISPLAY is defined, it is used"""
@@ -24,6 +38,7 @@ def test_display_system(monkeypatch, dispnum):
2438
monkeypatch.setitem(os.environ, 'DISPLAY', dispstr)
2539
assert config.get_display() == dispstr
2640

41+
2742
def test_display_config_and_system(monkeypatch):
2843
"""Check that when only both config and $DISPLAY are defined, the config takes precedence"""
2944
config._display = None
@@ -32,18 +47,64 @@ def test_display_config_and_system(monkeypatch):
3247
monkeypatch.setitem(os.environ, 'DISPLAY', dispstr)
3348
assert config.get_display() == dispstr
3449

35-
def test_display_noconfig_nosystem(monkeypatch):
50+
51+
def test_display_noconfig_nosystem_patched(monkeypatch):
3652
"""Check that when no display is specified, a virtual Xvfb is used"""
3753
config._display = None
3854
if config.has_option('execution', 'display_variable'):
3955
config._config.remove_option('execution', 'display_variable')
4056
monkeypatch.delitem(os.environ, 'DISPLAY', raising=False)
41-
assert int(config.get_display().split(':')[-1]) > 80
57+
monkeypatch.setitem(sys.modules, 'xvfbwrapper', xvfbpatch)
58+
assert config.get_display() == ":2010"
59+
60+
61+
def test_display_empty_patched(monkeypatch):
62+
"""Check that when no display is specified, a virtual Xvfb is used"""
63+
config._display = None
64+
if config.has_option('execution', 'display_variable'):
65+
config._config.remove_option('execution', 'display_variable')
66+
monkeypatch.setitem(os.environ, 'DISPLAY', '')
67+
monkeypatch.setitem(sys.modules, 'xvfbwrapper', xvfbpatch)
68+
assert config.get_display() == ':2010'
69+
70+
71+
def test_display_noconfig_nosystem_notinstalled(monkeypatch):
72+
"""Check that when no display is specified, a virtual Xvfb is used"""
73+
config._display = None
74+
if config.has_option('execution', 'display_variable'):
75+
config._config.remove_option('execution', 'display_variable')
76+
monkeypatch.delitem(os.environ, 'DISPLAY', raising=False)
77+
monkeypatch.setitem(sys.modules, 'xvfbwrapper', None)
78+
with pytest.raises(RuntimeError):
79+
config.get_display()
80+
81+
82+
def test_display_empty_notinstalled(monkeypatch):
83+
"""Check that when no display is specified, a virtual Xvfb is used"""
84+
config._display = None
85+
if config.has_option('execution', 'display_variable'):
86+
config._config.remove_option('execution', 'display_variable')
87+
monkeypatch.setitem(os.environ, 'DISPLAY', '')
88+
monkeypatch.setitem(sys.modules, 'xvfbwrapper', None)
89+
with pytest.raises(RuntimeError):
90+
config.get_display()
91+
92+
93+
@pytest.mark.skipif(not has_Xvfb, reason='xvfbwrapper not installed')
94+
def test_display_noconfig_nosystem_installed(monkeypatch):
95+
"""Check that when no display is specified, a virtual Xvfb is used"""
96+
config._display = None
97+
if config.has_option('execution', 'display_variable'):
98+
config._config.remove_option('execution', 'display_variable')
99+
monkeypatch.delitem(os.environ, 'DISPLAY', raising=False)
100+
assert int(config.get_display().split(':')[-1]) > 1000
101+
42102

43-
def test_display_empty(monkeypatch):
103+
@pytest.mark.skipif(not has_Xvfb, reason='xvfbwrapper not installed')
104+
def test_display_empty_installed(monkeypatch):
44105
"""Check that when no display is specified, a virtual Xvfb is used"""
45106
config._display = None
46107
if config.has_option('execution', 'display_variable'):
47108
config._config.remove_option('execution', 'display_variable')
48109
monkeypatch.setitem(os.environ, 'DISPLAY', '')
49-
assert int(config.get_display().split(':')[-1]) > 80
110+
assert int(config.get_display().split(':')[-1]) > 1000

0 commit comments

Comments
 (0)