15
15
except ImportError :
16
16
has_Xvfb = False
17
17
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 )
20
28
21
29
22
30
@pytest .mark .parametrize ('dispnum' , range (5 ))
@@ -27,6 +35,8 @@ def test_display_config(monkeypatch, dispnum):
27
35
config .set ('execution' , 'display_variable' , dispstr )
28
36
monkeypatch .delitem (os .environ , 'DISPLAY' , raising = False )
29
37
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' )
30
40
31
41
32
42
@pytest .mark .parametrize ('dispnum' , range (5 ))
@@ -37,6 +47,8 @@ def test_display_system(monkeypatch, dispnum):
37
47
dispstr = ':%d' % dispnum
38
48
monkeypatch .setitem (os .environ , 'DISPLAY' , dispstr )
39
49
assert config .get_display () == dispstr
50
+ # Test that it was correctly cached
51
+ assert config .get_display () == dispstr
40
52
41
53
42
54
def test_display_config_and_system (monkeypatch ):
@@ -46,6 +58,8 @@ def test_display_config_and_system(monkeypatch):
46
58
config .set ('execution' , 'display_variable' , dispstr )
47
59
monkeypatch .setitem (os .environ , 'DISPLAY' , ':0' )
48
60
assert config .get_display () == dispstr
61
+ # Test that it was correctly cached
62
+ assert config .get_display () == dispstr
49
63
50
64
51
65
def test_display_noconfig_nosystem_patched (monkeypatch ):
@@ -56,6 +70,8 @@ def test_display_noconfig_nosystem_patched(monkeypatch):
56
70
monkeypatch .delitem (os .environ , 'DISPLAY' , raising = False )
57
71
monkeypatch .setitem (sys .modules , 'xvfbwrapper' , xvfbpatch )
58
72
assert config .get_display () == ":2010"
73
+ # Test that it was correctly cached
74
+ assert config .get_display () == ':2010'
59
75
60
76
61
77
def test_display_empty_patched (monkeypatch ):
@@ -69,6 +85,38 @@ def test_display_empty_patched(monkeypatch):
69
85
monkeypatch .setitem (os .environ , 'DISPLAY' , '' )
70
86
monkeypatch .setitem (sys .modules , 'xvfbwrapper' , xvfbpatch )
71
87
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'
72
120
73
121
74
122
def test_display_noconfig_nosystem_notinstalled (monkeypatch ):
@@ -109,7 +157,10 @@ def test_display_noconfig_nosystem_installed(monkeypatch):
109
157
if config .has_option ('execution' , 'display_variable' ):
110
158
config ._config .remove_option ('execution' , 'display_variable' )
111
159
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
113
164
114
165
115
166
@pytest .mark .skipif (not has_Xvfb , reason = 'xvfbwrapper not installed' )
@@ -122,7 +173,10 @@ def test_display_empty_installed(monkeypatch):
122
173
if config .has_option ('execution' , 'display_variable' ):
123
174
config ._config .remove_option ('execution' , 'display_variable' )
124
175
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
126
180
127
181
128
182
def test_display_empty_macosx (monkeypatch ):
0 commit comments