3
3
# vi: set ft=python sts=4 ts=4 sw=4 et:
4
4
from __future__ import print_function , division , unicode_literals , absolute_import
5
5
import os
6
+ import sys
6
7
import pytest
7
8
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
+
8
21
9
22
@pytest .mark .parametrize ('dispnum' , range (5 ))
10
23
def test_display_config (monkeypatch , dispnum ):
@@ -15,6 +28,7 @@ def test_display_config(monkeypatch, dispnum):
15
28
monkeypatch .delitem (os .environ , 'DISPLAY' , raising = False )
16
29
assert config .get_display () == config .get ('execution' , 'display_variable' )
17
30
31
+
18
32
@pytest .mark .parametrize ('dispnum' , range (5 ))
19
33
def test_display_system (monkeypatch , dispnum ):
20
34
"""Check that when only a $DISPLAY is defined, it is used"""
@@ -24,6 +38,7 @@ def test_display_system(monkeypatch, dispnum):
24
38
monkeypatch .setitem (os .environ , 'DISPLAY' , dispstr )
25
39
assert config .get_display () == dispstr
26
40
41
+
27
42
def test_display_config_and_system (monkeypatch ):
28
43
"""Check that when only both config and $DISPLAY are defined, the config takes precedence"""
29
44
config ._display = None
@@ -32,18 +47,64 @@ def test_display_config_and_system(monkeypatch):
32
47
monkeypatch .setitem (os .environ , 'DISPLAY' , dispstr )
33
48
assert config .get_display () == dispstr
34
49
35
- def test_display_noconfig_nosystem (monkeypatch ):
50
+
51
+ def test_display_noconfig_nosystem_patched (monkeypatch ):
36
52
"""Check that when no display is specified, a virtual Xvfb is used"""
37
53
config ._display = None
38
54
if config .has_option ('execution' , 'display_variable' ):
39
55
config ._config .remove_option ('execution' , 'display_variable' )
40
56
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
+
42
102
43
- def test_display_empty (monkeypatch ):
103
+ @pytest .mark .skipif (not has_Xvfb , reason = 'xvfbwrapper not installed' )
104
+ def test_display_empty_installed (monkeypatch ):
44
105
"""Check that when no display is specified, a virtual Xvfb is used"""
45
106
config ._display = None
46
107
if config .has_option ('execution' , 'display_variable' ):
47
108
config ._config .remove_option ('execution' , 'display_variable' )
48
109
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