test_installer.py 6.56 KB
Newer Older
1
2
3
#!/usr/bin/env python

import os
4
import os.path  as op
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import contextlib
import shutil

import fslinstaller as inst

try:
    from unittest import mock
except ImportError:
    import mock

import pytest

from . import (server,
               CaptureStdout,
               indir,
               mock_input,
               strip_ansi_escape_sequences)

# manifest.json
# miniconda.sh
# environment file

mock_miniconda_sh = """
#!/usr/bin/env bash

#called like <script> -b -p <prefix>
prefix=$3

mkdir -p $prefix/bin/
mkdir -p $prefix/etc/

prefix=$(cd $prefix && pwd)

38
39
40
41
# called like
#  - conda install -y -n base fslbase=1234.0
#  - conda env update -n base -f <envfile>
#  - conda clean -y --all
42
echo "#!/usr/bin/env bash"  >> $3/bin/conda
43
44
45
46
47
48
49
echo 'if   [ "$1" = "clean" ]; then '      >> $3/bin/conda
echo "    touch $prefix/cleaned"           >> $3/bin/conda
echo 'elif [ "$1" = "install" ]; then '    >> $3/bin/conda
echo '    echo "$5"' "> $prefix/installed" >> $3/bin/conda
echo "else"                                >> $3/bin/conda
echo "    cp "'$6'" $prefix/"              >> $3/bin/conda
echo "fi"                                  >> $3/bin/conda
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
chmod a+x $prefix/bin/conda
""".strip()


mock_manifest = """
{{
    "installer" : {{
        "version" : "{version}",
        "url"     : "na",
        "sha256"  : "na"
    }},
    "miniconda" : {{
        "{platform}" : {{
            "url"    : "{url}/miniconda.sh",
            "sha256" : "{conda_sha256}"
        }}
    }},
    "versions" : {{
        "latest" : "6.2.0",
        "6.2.0"  : [
            {{
                "platform"    : "{platform}",
                "environment" : "{url}/env-6.2.0.yml",
                "sha256"      : "{env620_sha256}"
            }}
        ],
        "6.1.0"  : [
            {{
                "platform"    : "{platform}",
                "environment" : "{url}/env-6.1.0.yml",
                "sha256"      : "{env610_sha256}"
            }}
        ]
    }}
}}
""".strip()
# Format vars: version platform url conda_sha256 env610_sha256 env620_sha256


89
90
91
92
93
94
95
96
mock_env_yml_template = """
{version}
packages:
 - fsl-base 1234.0
""".strip()



97
98
99
100
101
102
103
@contextlib.contextmanager
def installer_server(cwd=None):
    if cwd is None:
        cwd = '.'
    cwd = op.abspath(cwd)

    with indir(cwd), server(cwd) as srv:
104
105
106
107
108
109
        with open('miniconda.sh', 'wt') as f:
            f.write(mock_miniconda_sh)
        with open('env-6.1.0.yml', 'wt') as f:
            f.write(mock_env_yml_template.format(version='6.1.0'))
        with open('env-6.2.0.yml', 'wt') as f:
            f.write(mock_env_yml_template.format(version='6.2.0'))
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132

        conda_sha256  = inst.sha256('miniconda.sh')
        env610_sha256 = inst.sha256('env-6.1.0.yml')
        env620_sha256 = inst.sha256('env-6.2.0.yml')

        manifest = mock_manifest.format(
            version=inst.__version__,
            platform=inst.Context.identify_platform(),
            url=srv.url,
            conda_sha256=conda_sha256,
            env610_sha256=env610_sha256,
            env620_sha256=env620_sha256)

        with open('manifest.json', 'wt') as f:
            f.write(manifest)

        yield srv


def check_install(homedir, destdir, version):

    destdir = op.abspath(destdir)
    etc     = op.join(destdir, 'etc')
Paul McCarthy's avatar
Paul McCarthy committed
133
134
    shell   = os.environ.get('SHELL', 'sh')
    profile = inst.configure_shell.shell_profiles.get(shell, None)
135
136
137
138

    with indir(destdir):
        # added by our mock conda env creeate call
        with open(op.join(destdir, 'env-{}.yml'.format(version)), 'rt') as f:
139
140
141
142
143
144
            exp = mock_env_yml_template.format(version=version)
            assert f.read().strip() == exp

        # added by our mock conda install call
        with open(op.join(destdir, 'installed'), 'rt') as f:
            assert f.read().strip() == 'fsl-base=1234.0'
145

146
147
148
149
150
151
152
153
        # added by our mock conda clean call
        assert op.exists(op.join(destdir, 'cleaned'))

        # added by the fslinstaller
        with open(op.join(etc, 'fslversion'), 'rt') as f:
            assert f.read().strip() == version
        assert op.exists(op.join(etc,     'fslinstaller.py'))
        assert op.exists(op.join(etc,     'env-{}.yml'.format(version)))
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
        assert op.exists(op.join(homedir, 'Documents', 'MATLAB'))

        if profile is not None:
            assert any([op.exists(op.join(homedir, p)) for p in profile])


def test_installer_normal_interactive_usage():
    with installer_server() as srv:
        with mock.patch('fslinstaller.FSL_INSTALLER_MANIFEST',
                        '{}/manifest.json'.format(srv.url)):
            # accept rel/abs paths
            for i in range(3):
                with inst.tempdir() as cwd:
                    dests = ['fsl', op.join('.', 'fsl'), op.abspath('fsl')]
                    dest  = dests[i]
                    with mock_input(dest):
                        inst.main(['--homedir', cwd])
                    check_install(cwd, dest, '6.2.0')
                    shutil.rmtree(dest)


def test_installer_list_versions():
    platform = inst.Context.identify_platform()
    with installer_server() as srv:
        with mock.patch('fslinstaller.FSL_INSTALLER_MANIFEST',
                        '{}/manifest.json'.format(srv.url)):
            with inst.tempdir() as cwd:
                with CaptureStdout() as cap:
                    with pytest.raises(SystemExit) as e:
                        inst.main(['--listversions'])
                    assert e.value.code == 0

                out   = strip_ansi_escape_sequences(cap.stdout)
                lines = out.split('\n')

                assert '6.1.0' in lines
                assert '6.2.0' in lines
                assert '  {} {}/env-6.1.0.yml'.format(platform, srv.url) in lines
                assert '  {} {}/env-6.2.0.yml'.format(platform, srv.url) in lines


def test_installer_normal_cli_usage():
    with installer_server() as srv:
        with mock.patch('fslinstaller.FSL_INSTALLER_MANIFEST',
                        '{}/manifest.json'.format(srv.url)):

            # accept rel/abs paths
            for i in range(3):
                with inst.tempdir() as cwd:
                    dests = ['fsl', op.join('.', 'fsl'), op.abspath('fsl')]
                    dest  = dests[i]
                    inst.main(['--homedir', cwd, '--dest', dest])
                    check_install(cwd, dest, '6.2.0')
                    shutil.rmtree(dest)

            # install specific version
            with inst.tempdir() as cwd:
                inst.main(['--homedir', cwd,
                           '--dest', 'fsl',
                           '--fslversion', '6.1.0'])
                check_install(cwd, 'fsl', '6.1.0')
                shutil.rmtree('fsl')