Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
FSL
conda
installer
Commits
b7686d27
Commit
b7686d27
authored
Jul 15, 2021
by
Paul McCarthy
🚵
Browse files
TEST: Some basic end-to-end tests
parent
e55326eb
Changes
1
Hide whitespace changes
Inline
Side-by-side
test/test_installer.py
0 → 100644
View file @
b7686d27
#!/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'
)
profile
=
inst
.
configure_shell
.
shell_profiles
.
get
(
os
.
environ
[
'SHELL'
],
None
)
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'
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment