test_platform.py 5.72 KB
Newer Older
1
2
3
4
5
6
7
8
#!/usr/bin/env python
#
# test_platform.py -
#
# Author: Paul McCarthy <pauldmccarthy@gmail.com>
#


9
10
11
12
import               os
import               gc
import os.path    as op
import               sys
13
import               time
14
15
16
17
import               shutil
import               tempfile
import               pytest
from unittest import mock
18

19
20
21
22
23

import fsl.utils.platform as fslplatform


def test_atts():
24

25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
    p = fslplatform.platform
    p.os
    p.frozen
    p.haveGui
    p.canHaveGui
    p.inSSHSession
    p.wxPlatform
    p.wxFlavour
    p.fsldir
    p.fslVersion
    p.glVersion
    p.glRenderer
    p.glIsSoftwareRenderer


40
@pytest.mark.wxtest
41
def test_haveGui():
42
43

    import wx
44

45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
    p = fslplatform.Platform()

    # We can get weird conflicts w.r.t. access to
    # the display when multiple tests are running
    # simultaneously within docker on the same
    # machine.
    app = None
    for _ in range(5):
        try:
            app = wx.App()
            break
        except Exception:
            time.sleep(1)
    if app is None:
        assert False

61
62
63
64
65
66
67
    frame  = wx.Frame(None)
    passed = [False]
    frame.Show()

    def runtest():

        try:
68
            assert p.haveGui
69
70
71
72
73
74
75
76
            passed[0] = True
        finally:
            frame.Destroy()
            app.ExitMainLoop()

    wx.CallLater(500, runtest)

    app.MainLoop()
Paul McCarthy's avatar
Paul McCarthy committed
77
    del app
78
79
80
81

    assert passed[0]


82
@pytest.mark.wxtest
83
84
def test_wxatts():

85
86
    gc.collect()

87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
    with mock.patch.dict('sys.modules', wx=None):
        p = fslplatform.Platform()
        assert not p.canHaveGui
        assert not p.haveGui
        assert p.wxFlavour  == fslplatform.WX_UNKNOWN
        assert p.wxPlatform == fslplatform.WX_UNKNOWN

    with mock.patch('wx.App.IsDisplayAvailable', return_value=False):

        p = fslplatform.Platform()
        assert not p.canHaveGui
        assert not p.haveGui
        assert p.wxFlavour  == fslplatform.WX_UNKNOWN
        assert p.wxPlatform == fslplatform.WX_UNKNOWN

    with mock.patch('wx.App.IsDisplayAvailable', return_value=True), \
         mock.patch('wx.PlatformInfo', ('gtk', 'phoenix')):

        p = fslplatform.Platform()
        assert     p.canHaveGui
        assert not p.haveGui
        assert     p.wxFlavour  == fslplatform.WX_PHOENIX
        assert     p.wxPlatform == fslplatform.WX_GTK


    # (wx.PlatformInfo, expected platform, expected flavour)
    platflavtests = [
        (('__WXMAC__',
          'wxMac',
          'unicode',
          'unicode-wchar',
          'wxOSX',
          'wxOSX-cocoa',
          'wx-assertions-on',
          'phoenix',
          'wxWidgets 3.0.4'),
         fslplatform.WX_MAC_COCOA,
         fslplatform.WX_PHOENIX),
        (('__WXMAC__',
          'wxMac',
          'unicode',
          'wxOSX',
          'wxOSX-cocoa',
          'wx-assertions-on',
          'SWIG-1.3.29'),
         fslplatform.WX_MAC_COCOA,
         fslplatform.WX_PYTHON),
        (('__WXGTK__',
          'wxGTK',
          'unicode',
          'unicode-wchar',
          'gtk2',
          'wx-assertions-on',
          'phoenix',
          'wxWidgets 3.0.4'),
         fslplatform.WX_GTK,
         fslplatform.WX_PHOENIX),
        (('__WXGTK__',
          'wxGTK',
          'unicode',
          'gtk2',
          'wx-assertions-on',
          'SWIG-1.3.29'),
         fslplatform.WX_GTK,
         fslplatform.WX_PYTHON)]

    for platinfo, expplatform, expflavour in platflavtests:
        with mock.patch('wx.PlatformInfo', platinfo):

            p = fslplatform.Platform()
            assert p.wxFlavour  == expflavour
            assert p.wxPlatform == expplatform


161
def test_gl():
162
163
164

    p = fslplatform.Platform()

165
166
167
    p.glVersion  = '2.1'
    p.glRenderer = 'Fake renderer'

168
169
170
171
172

    assert p.glVersion  == '2.1'
    assert p.glRenderer == 'Fake renderer'


173
174
175
176
177
178
179
180
181
def test_fsldir():

    # We have to make a dummy directory that looks like FSL
    testdir = tempfile.mkdtemp()
    fsldir  = op.join(testdir, 'fsl')

    def makeFSL():
        os.makedirs(op.join(fsldir, 'etc'))
        with open(op.join(fsldir, 'etc', 'fslversion'), 'wt') as f:
182
            f.write('6.0.2:7606e0d8\n')
183
184
185
186
187

    try:

        makeFSL()

188
        p         = fslplatform.Platform()
189
190
191
192
193
194
195
196
197
198
199
200
201
202
        newFSLDir = [None]

        def fsldirChanged(p, t, val):
            newFSLDir[0] = val

        p.register('callback', fsldirChanged)

        p.fsldir = fsldir

        p.deregister('callback')

        assert os.environ['FSLDIR'] == fsldir
        assert newFSLDir[0]         == fsldir
        assert p.fsldir             == fsldir
203
        assert p.fslVersion         == '6.0.2'
204
205
206
207

    finally:
        shutil.rmtree(testdir)

208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228

def test_detect_ssh():

    sshVars = ['SSH_CLIENT', 'SSH_TTY']
    vncVars = ['VNCDESKTOP', 'X2GO_SESSION', 'NXSESSIONID']

    for sv in sshVars:
        with mock.patch.dict('os.environ', **{ sv : '1'}):
            p = fslplatform.Platform()
            assert p.inSSHSession


    for vv in vncVars:
        with mock.patch.dict('os.environ', **{ vv : '1'}):
            p = fslplatform.Platform()
            assert p.inVNCSession

    with mock.patch('os.environ', {}):
        p = fslplatform.Platform()
        assert not p.inSSHSession
        assert not p.inVNCSession
229
230

def test_fslwsl():
231
232
    """
    Note that ``Platform.fsldir`` requires the directory in ``FSLDIR`` to exist and
233
    sets ``FSLDIR`` to ``None`` if it doesn't. So we create a ``Platform`` first
234
235
236
237
238
    and then overwrite ``FSLDIR``. This is a bit of a hack but the logic we are testing
    here is whether ``Platform.fslwsl`` recognizes a WSL ``FSLDIR`` string
    """
    p = fslplatform.Platform()
    with mock.patch.dict('os.environ', **{ 'FSLDIR' : '\\\\wsl$\\my cool linux distro v1.0\\usr\\local\\fsl'}):
239
240
        assert p.fslwsl

241
    with mock.patch.dict('os.environ', **{ 'FSLDIR' : '/usr/local/fsl'}):
242
        assert not p.fslwsl