Commit 9b918c3b authored by Paul McCarthy's avatar Paul McCarthy 🚵
Browse files

TEST: working on unit/integration tests

parent ee498441
Pipeline #5696 passed with stage
in 25 seconds
#!/usr/bin/env python
import os
import os.path as op
import json
from unittest import mock
import pytest
import fsl.scripts.fsl_add_module as fam
from . import make_archive, tempdir, server, mock_input
def test_loadManifest_different_sources():
manifest = [
{ 'name' : 'abc', 'url' : 'http://abc.com/abc.zip' },
{ 'name' : 'def', 'url' : 'http://abc.com/def.zip' },
]
# test
# - manifest from a URL
# - manifest from a file
# - invalid manifest
# - default manifest
with tempdir(), server() as srv:
with open('manifest.json', 'wt') as f:
f.write(json.dumps(manifest))
url = f'http://localhost:{srv.port}/manifest.json'
path = './manifest.json'
# manifest from URL
m, p = fam.loadManifest(fam.parseArgs(['-m', url]))
assert len(p) == 0
assert list(m.keys()) == ['abc', 'def']
# manifest from file
m, p = fam.loadManifest(fam.parseArgs(['-m', path]))
assert len(p) == 0
assert list(m.keys()) == ['abc', 'def']
# invalid manifest - if no plugins
# specified, an error should be raised
with pytest.raises(RuntimeError):
fam.loadManifest(fam.parseArgs(['-m', 'nomanifest']))
# otherwise should work ok
m, p = fam.loadManifest(
fam.parseArgs(['-m', 'nomanifest', 'http://abc.com/plugin.zip']))
assert p == ['plugin.zip']
assert list(m.keys()) == ['plugin.zip']
# default manifest
with mock.patch('fsl.scripts.fsl_add_module.MANIFEST_URL', url):
m, p = fam.loadManifest(fam.parseArgs([]))
assert len(p) == 0
assert list(m.keys()) == ['abc', 'def']
def test_loadManifest_merge_plugins_from_command_line():
manifest = [
{ 'name' : 'abc', 'url' : 'http://abc.com/abc.zip' },
{ 'name' : 'def', 'url' : 'http://abc.com/def.zip' },
]
with tempdir():
with open('manifest.json', 'wt') as f:
f.write(json.dumps(manifest))
args = fam.parseArgs(['-m', 'manifest.json',
'abc',
'ghi.zip',
'http://plugins.com/jkl.zip'])
m, p = fam.loadManifest(args)
assert p == ['abc', 'ghi.zip', 'jkl.zip']
assert sorted(m.keys()) == ['abc', 'def', 'ghi.zip', 'jkl.zip']
def test_loadManifest_destination_specified():
manifest = [
{ 'name' : 'abc', 'url' : 'http://abc.com/abc.zip' },
{ 'name' : 'def',
'url' : 'http://abc.com/def.zip',
'destination' : '~/defdest', },
]
# clear env incase FSLDIR is set
with tempdir(), mock.patch.dict(os.environ, clear=True):
with open('manifest.json', 'wt') as f:
f.write(json.dumps(manifest))
# no dest - plugins get their default
args = fam.parseArgs(['-m', 'manifest.json'])
m = fam.loadManifest(args)[0]
assert m['abc'].destination == os.getcwd()
assert m['def'].destination == op.join(op.expanduser('~'), 'defdest')
# one dest - all plugins get set to it
dest = 'onedest'
args = fam.parseArgs(['-m', 'manifest.json', '-d', dest])
m = fam.loadManifest(args)[0]
assert m['abc'].destination == op.abspath(dest)
assert m['def'].destination == op.abspath(dest)
# one dest and one plugin - only that plugin gets set to it
dest = 'onedest'
args = fam.parseArgs(['-m', 'manifest.json', '-d', dest, 'def'])
m = fam.loadManifest(args)[0]
assert m['abc'].destination == os.getcwd()
assert m['def'].destination == op.abspath(dest)
# one dest for each plugin - only valid
# when plugins are requested on command-line
dest1 = 'dest1'
dest2 = 'dest2'
with pytest.raises(SystemExit):
args = fam.parseArgs(['-m', 'manifest.json',
'-d', dest1, '-d', dest2])
args = fam.parseArgs(['-m', 'manifest.json',
'-d', dest1, '-d', dest2,
'abc', 'def'])
m = fam.loadManifest(args)[0]
assert m['abc'].destination == op.abspath(dest1)
assert m['def'].destination == op.abspath(dest2)
def test_selectPlugins_from_filepath_and_url():
with mock.patch('fsl.scripts.fsl_add_module.MANIFEST_URL', None):
args = fam.parseArgs('abc.zip def.zip -d dest'.split())
m, p = fam.loadManifest(args)
plugins = fam.selectPlugins(args, m, p)
assert plugins == [m['abc.zip'], m['def.zip']]
args = fam.parseArgs('abc.zip http://plugins.com/def.zip '
'-d dest'.split())
m, p = fam.loadManifest(args)
plugins = fam.selectPlugins(args, m, p)
assert plugins == [m['abc.zip'], m['def.zip']]
def test_selectPlugins_name_from_manifest():
manifest = [
{ 'name' : 'abc', 'url' : 'http://abc.com/abc.zip' },
{ 'name' : 'def', 'url' : 'http://abc.com/def.zip' },
]
with tempdir():
with open('manifest.json', 'wt') as f:
f.write(json.dumps(manifest))
args = fam.parseArgs('-m manifest.json -d dest '
'abc def ghi.zip'.split())
m, p = fam.loadManifest(args)
plugins = fam.selectPlugins(args, m, p)
assert plugins == [m['abc'], m['def'], m['ghi.zip']]
def test_selectPlugins_no_destination_specified():
with mock.patch('fsl.scripts.fsl_add_module.MANIFEST_URL', None):
args = fam.parseArgs('abc.zip def.zip'.split())
m, p = fam.loadManifest(args)
with mock_input(''):
plugins = fam.selectPlugins(args, m, p)
assert plugins == [m['abc.zip'], m['def.zip']]
def test_selectPlugins_prompt_user():
pass
def test_download():
pass
def test_install():
pass
def test_main():
pass
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment