test_installer.py 5.83 KB
Newer Older
1
2
3
4
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
38
39
40
41
42
43
44
45
46
47
48
49
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
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
#!/usr/bin/env python

import os
import os.path as op
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)

# called like  conda env update -n base -f <envfile>
# and like     conda clean -y --all
echo "#!/usr/bin/env bash"  >> $3/bin/conda
echo 'if [ "$1" = "clean" ]; then '  >> $3/bin/conda
echo "    touch $prefix/cleaned"     >> $3/bin/conda
echo "else"                          >> $3/bin/conda
echo "  cp "'$6'" $prefix/"          >> $3/bin/conda
echo "fi"                            >> $3/bin/conda
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


@contextlib.contextmanager
def installer_server(cwd=None):
    if cwd is None:
        cwd = '.'
    cwd = op.abspath(cwd)

    with indir(cwd), server(cwd) as srv:
        with open('miniconda.sh', 'wt') as f: f.write(mock_miniconda_sh)
        with open('env-6.1.0.yml', 'wt')   as f: f.write('6.1.0')
        with open('env-6.2.0.yml', 'wt')   as f: f.write('6.2.0')

        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
118
119
    shell   = os.environ.get('SHELL', 'sh')
    profile = inst.configure_shell.shell_profiles.get(shell, None)
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
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

    with indir(destdir):
        with open(op.join(etc, 'fslversion'), 'rt') as f:
            assert f.read().strip() == version

        # added by our mock conda env creeate call
        with open(op.join(destdir, 'env-{}.yml'.format(version)), '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)))
        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')