Skip to content

Commit b099e32

Browse files
oestebaneffigies
authored andcommitted
make code work with current xvfbwrapper and make it bwd compatible. add tests for this
1 parent 2e16f83 commit b099e32

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

nipype/utils/config.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ def get_display(self):
266266
# shell=True, stdout=sp.DEVNULL))
267267

268268
if self._display is not None:
269-
return ':%d' % self._display.vdisplay_num
269+
return ':%d' % self._display.new_display
270270

271271
sysdisplay = None
272272
if self._config.has_option('execution', 'display_variable'):
@@ -281,7 +281,7 @@ def _mock():
281281

282282
# Store a fake Xvfb object
283283
ndisp = int(sysdisplay.split(':')[-1])
284-
Xvfb = namedtuple('Xvfb', ['vdisplay_num', 'stop'])
284+
Xvfb = namedtuple('Xvfb', ['new_display', 'stop'])
285285
self._display = Xvfb(ndisp, _mock)
286286
return sysdisplay
287287
else:
@@ -306,12 +306,12 @@ def _mock():
306306
self._display = Xvfb(nolisten='tcp')
307307
self._display.start()
308308

309-
# Older versions of Xvfb used vdisplay_num
310-
if hasattr(self._display, 'vdisplay_num'):
311-
return ':%d' % self._display.vdisplay_num
309+
# Older versions of xvfbwrapper used vdisplay_num
310+
if not hasattr(self._display, 'new_display'):
311+
setattr(self._display, 'new_display',
312+
self._display.vdisplay_num)
312313

313-
if hasattr(self._display, 'new_display'):
314-
return ':%d' % self._display.new_display
314+
return ':%d' % self._display.new_display
315315

316316
def stop_display(self):
317317
"""Closes the display if started"""

nipype/utils/tests/test_config.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,16 @@
1515
except ImportError:
1616
has_Xvfb = False
1717

18-
xvfbpatch = MagicMock()
19-
xvfbpatch.Xvfb.return_value = MagicMock(vdisplay_num=2010)
18+
# Define mocks for xvfbwrapper. Do not forget the spec to ensure that
19+
# hasattr() checks return False with missing attributes.
20+
xvfbpatch = MagicMock(spec=['Xvfb'])
21+
xvfbpatch.Xvfb.return_value = MagicMock(spec=['new_display', 'start', 'stop'],
22+
new_display=2010)
23+
24+
# Mock the legacy xvfbwrapper.Xvfb class (changed display attribute name)
25+
xvfbpatch_old = MagicMock(spec=['Xvfb'])
26+
xvfbpatch_old.Xvfb.return_value = MagicMock(spec=['vdisplay_num', 'start', 'stop'],
27+
vdisplay_num=2010)
2028

2129

2230
@pytest.mark.parametrize('dispnum', range(5))
@@ -71,6 +79,32 @@ def test_display_empty_patched(monkeypatch):
7179
assert config.get_display() == ':2010'
7280

7381

82+
def test_display_noconfig_nosystem_patched_oldxvfbwrapper(monkeypatch):
83+
"""
84+
Check that when no $DISPLAY nor option are specified,
85+
a virtual Xvfb is used (with a legacy version of xvfbwrapper).
86+
"""
87+
config._display = None
88+
if config.has_option('execution', 'display_variable'):
89+
config._config.remove_option('execution', 'display_variable')
90+
monkeypatch.delitem(os.environ, 'DISPLAY', raising=False)
91+
monkeypatch.setitem(sys.modules, 'xvfbwrapper', xvfbpatch_old)
92+
assert config.get_display() == ":2010"
93+
94+
95+
def test_display_empty_patched_oldxvfbwrapper(monkeypatch):
96+
"""
97+
Check that when $DISPLAY is empty string and no option is specified,
98+
a virtual Xvfb is used (with a legacy version of xvfbwrapper).
99+
"""
100+
config._display = None
101+
if config.has_option('execution', 'display_variable'):
102+
config._config.remove_option('execution', 'display_variable')
103+
monkeypatch.setitem(os.environ, 'DISPLAY', '')
104+
monkeypatch.setitem(sys.modules, 'xvfbwrapper', xvfbpatch_old)
105+
assert config.get_display() == ':2010'
106+
107+
74108
def test_display_noconfig_nosystem_notinstalled(monkeypatch):
75109
"""
76110
Check that an exception is raised if xvfbwrapper is not installed

0 commit comments

Comments
 (0)