33# vi: set ft=python sts=4 ts=4 sw=4 et:
44from __future__ import print_function , division , unicode_literals , absolute_import
55import os
6+ import sys
67import pytest
78from 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 ))
1023def 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 ))
1933def 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+
2742def 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