Skip to content

Commit cfe9f5b

Browse files
authored
Merge pull request #1 from oesteban/fix/xvfb_and_noise-patch-1
Old xvfbwrapper patch
2 parents 5b367f2 + f431447 commit cfe9f5b

File tree

2 files changed

+65
-11
lines changed

2 files changed

+65
-11
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: 58 additions & 4 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))
@@ -27,6 +35,8 @@ def test_display_config(monkeypatch, dispnum):
2735
config.set('execution', 'display_variable', dispstr)
2836
monkeypatch.delitem(os.environ, 'DISPLAY', raising=False)
2937
assert config.get_display() == config.get('execution', 'display_variable')
38+
# Test that it was correctly cached
39+
assert config.get_display() == config.get('execution', 'display_variable')
3040

3141

3242
@pytest.mark.parametrize('dispnum', range(5))
@@ -37,6 +47,8 @@ def test_display_system(monkeypatch, dispnum):
3747
dispstr = ':%d' % dispnum
3848
monkeypatch.setitem(os.environ, 'DISPLAY', dispstr)
3949
assert config.get_display() == dispstr
50+
# Test that it was correctly cached
51+
assert config.get_display() == dispstr
4052

4153

4254
def test_display_config_and_system(monkeypatch):
@@ -46,6 +58,8 @@ def test_display_config_and_system(monkeypatch):
4658
config.set('execution', 'display_variable', dispstr)
4759
monkeypatch.setitem(os.environ, 'DISPLAY', ':0')
4860
assert config.get_display() == dispstr
61+
# Test that it was correctly cached
62+
assert config.get_display() == dispstr
4963

5064

5165
def test_display_noconfig_nosystem_patched(monkeypatch):
@@ -56,6 +70,8 @@ def test_display_noconfig_nosystem_patched(monkeypatch):
5670
monkeypatch.delitem(os.environ, 'DISPLAY', raising=False)
5771
monkeypatch.setitem(sys.modules, 'xvfbwrapper', xvfbpatch)
5872
assert config.get_display() == ":2010"
73+
# Test that it was correctly cached
74+
assert config.get_display() == ':2010'
5975

6076

6177
def test_display_empty_patched(monkeypatch):
@@ -69,6 +85,38 @@ def test_display_empty_patched(monkeypatch):
6985
monkeypatch.setitem(os.environ, 'DISPLAY', '')
7086
monkeypatch.setitem(sys.modules, 'xvfbwrapper', xvfbpatch)
7187
assert config.get_display() == ':2010'
88+
# Test that it was correctly cached
89+
assert config.get_display() == ':2010'
90+
91+
92+
def test_display_noconfig_nosystem_patched_oldxvfbwrapper(monkeypatch):
93+
"""
94+
Check that when no $DISPLAY nor option are specified,
95+
a virtual Xvfb is used (with a legacy version of xvfbwrapper).
96+
"""
97+
config._display = None
98+
if config.has_option('execution', 'display_variable'):
99+
config._config.remove_option('execution', 'display_variable')
100+
monkeypatch.delitem(os.environ, 'DISPLAY', raising=False)
101+
monkeypatch.setitem(sys.modules, 'xvfbwrapper', xvfbpatch_old)
102+
assert config.get_display() == ":2010"
103+
# Test that it was correctly cached
104+
assert config.get_display() == ':2010'
105+
106+
107+
def test_display_empty_patched_oldxvfbwrapper(monkeypatch):
108+
"""
109+
Check that when $DISPLAY is empty string and no option is specified,
110+
a virtual Xvfb is used (with a legacy version of xvfbwrapper).
111+
"""
112+
config._display = None
113+
if config.has_option('execution', 'display_variable'):
114+
config._config.remove_option('execution', 'display_variable')
115+
monkeypatch.setitem(os.environ, 'DISPLAY', '')
116+
monkeypatch.setitem(sys.modules, 'xvfbwrapper', xvfbpatch_old)
117+
assert config.get_display() == ':2010'
118+
# Test that it was correctly cached
119+
assert config.get_display() == ':2010'
72120

73121

74122
def test_display_noconfig_nosystem_notinstalled(monkeypatch):
@@ -109,7 +157,10 @@ def test_display_noconfig_nosystem_installed(monkeypatch):
109157
if config.has_option('execution', 'display_variable'):
110158
config._config.remove_option('execution', 'display_variable')
111159
monkeypatch.delitem(os.environ, 'DISPLAY', raising=False)
112-
assert int(config.get_display().split(':')[-1]) > 1000
160+
newdisp = config.get_display()
161+
assert int(newdisp.split(':')[-1]) > 1000
162+
# Test that it was correctly cached
163+
assert config.get_display() == newdisp
113164

114165

115166
@pytest.mark.skipif(not has_Xvfb, reason='xvfbwrapper not installed')
@@ -122,7 +173,10 @@ def test_display_empty_installed(monkeypatch):
122173
if config.has_option('execution', 'display_variable'):
123174
config._config.remove_option('execution', 'display_variable')
124175
monkeypatch.setitem(os.environ, 'DISPLAY', '')
125-
assert int(config.get_display().split(':')[-1]) > 1000
176+
newdisp = config.get_display()
177+
assert int(newdisp.split(':')[-1]) > 1000
178+
# Test that it was correctly cached
179+
assert config.get_display() == newdisp
126180

127181

128182
def test_display_empty_macosx(monkeypatch):

0 commit comments

Comments
 (0)