Commit 104b1b8c authored by Paul McCarthy's avatar Paul McCarthy 🚵
Browse files

TEST: Test main script. More to come

parent b3bfd868
Pipeline #5697 passed with stage
in 26 seconds
......@@ -131,3 +131,11 @@ def make_archive(fname, *contents):
archive = op.join(dirname, basename)
archive = shutil.make_archive(archive, fmt)
shutil.move(archive, fname)
def check_dir(dirname, should_exist=None, should_not_exist=None):
if should_exist is None: should_exist = []
if should_not_exist is None: should_not_exist = []
for f in should_exist: assert op.exists(op.join(dirname, f))
for f in should_not_exist: assert not op.exists(op.join(dirname, f))
......@@ -7,9 +7,38 @@ from unittest import mock
import pytest
import fsl.add_module.routines as routines
import fsl.scripts.fsl_add_module as fam
from . import make_archive, tempdir, server, mock_input
from . import make_archive, tempdir, server, mock_input, check_dir
def test_parseArgs():
# force can only be used when arguments are specified
fam.parseArgs('--force a.zip b.zip'.split())
with pytest.raises(SystemExit):
fam.parseArgs(['--force'])
# either one destination is specified, or
# <nplugins> destinationsx are specified
fam.parseArgs('-d dest'.split())
fam.parseArgs('-d dest a.zip'.split())
fam.parseArgs('-d dest a.zip b.zip c.zip'.split())
fam.parseArgs('-d dest1 -d dest2 -d dest3 a.zip b.zip c.zip'.split())
with pytest.raises(SystemExit):
fam.parseArgs('-d dest1 -d dest2 -d dest3 a.zip b.zip'.split())
with pytest.raises(SystemExit):
fam.parseArgs('-d dest1 -d dest2 -d dest3'.split())
# destination should be expanded
args = fam.parseArgs('-d ~/dest'.split())
assert args.destination[0] == op.join(op.expanduser('~'), 'dest')
with mock.patch.dict(os.environ, {'GROT' : 'somedir'}):
args = fam.parseArgs('-d $GROT/dest'.split())
assert args.destination[0] == op.abspath(op.join('somedir', 'dest'))
def test_loadManifest_different_sources():
......@@ -171,16 +200,336 @@ def test_selectPlugins_no_destination_specified():
def test_selectPlugins_prompt_user():
pass
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'.split())
m, p = fam.loadManifest(args)
with mock_input('all'):
plugins = fam.selectPlugins(args, m, p)
assert plugins == [m['abc'], m['def']]
def test_selectPlugins_category():
manifest = [
{ 'name' : 'abc', 'url' : 'http://abc.com/abc.zip', 'category' : 'a' },
{ 'name' : 'def', 'url' : 'http://abc.com/def.zip', 'category' : 'a' },
{ 'name' : 'ghi', 'url' : 'http://abc.com/ghi.zip', 'category' : 'b' },
{ 'name' : 'jkl', 'url' : 'http://abc.com/jkl.zip', 'category' : 'b' },
]
with tempdir():
with open('manifest.json', 'wt') as f:
f.write(json.dumps(manifest))
args = fam.parseArgs('-m manifest.json -d dest -c a'.split())
m, p = fam.loadManifest(args)
with mock_input('all'):
plugins = fam.selectPlugins(args, m, p)
assert plugins == m.getCategory('a')
args = fam.parseArgs('-m manifest.json -d dest -c b'.split())
m, p = fam.loadManifest(args)
with mock_input('all'):
plugins = fam.selectPlugins(args, m, p)
assert plugins == m.getCategory('b')
def test_download():
pass
# download from URL with good checksum
# download from URL with bad checksum
# download from local file
# failed download
with tempdir() as cwd, server() as srv:
make_archive('abc.zip', 'a/b', 'c/d')
make_archive('def.zip', 'e/f', 'g/h')
make_archive('jkl.zip', 'i/j', 'k/l')
archivedir = op.join(cwd, 'archives')
abcshasum = routines.calcChecksum('abc.zip')
manifest = [
{ 'name' : 'abc',
'url' : f'http://localhost:{srv.port}/abc.zip',
'checksum' : f'{abcshasum}' },
{ 'name' : 'def',
'url' : f'http://localhost:{srv.port}/def.zip',
'checksum' : 'badchecksum', },
{ 'name' : 'ghi',
'url' : f'http://localhost:{srv.port}/no_exist.zip', },
{ 'name' : 'jkl',
'url' : f'{cwd}/jkl.zip', },
{ 'name' : 'mno',
'url' : f'{cwd}/no_exist.zip', },
]
with open('manifest.json', 'wt') as f:
f.write(json.dumps(manifest))
os.mkdir(archivedir)
with mock.patch('fsl.scripts.fsl_add_module.ARCHIVE_DIR', archivedir):
args = fam.parseArgs('-m manifest.json'.split())
m = fam.loadManifest(args)[0]
plugins = list(m.values())
fam.download(args, plugins)
assert op.exists(op.join(archivedir, 'abc.zip'))
assert op.exists(op.join(archivedir, 'def.zip'))
assert not op.exists(op.join(archivedir, 'ghi.zip'))
assert not op.exists(op.join(archivedir, 'jkl.zip'))
assert not op.exists(op.join(archivedir, 'mno.zip'))
assert m['abc'].archiveFile == op.join(archivedir, 'abc.zip')
assert m['def'].archiveFile == op.join(archivedir, 'def.zip')
assert m['ghi'].archiveFile is None
assert m['jkl'].archiveFile == op.join(cwd, 'jkl.zip')
assert m['mno'].archiveFile is None
assert m['abc'].checksumPassed
assert m['def'].checksumPassed is False
assert m['ghi'].checksumPassed is None
assert m['jkl'].checksumPassed is None
assert m['mno'].checksumPassed is None
def test_download_archiveDir():
# - default archive dir
# - user-specified archive dir
with tempdir() as cwd, server() as srv:
make_archive('abc.zip', 'a/b', 'c/d')
make_archive('def.zip', 'e/f', 'g/h')
argv = [f'http://localhost:{srv.port}/abc.zip',
f'http://localhost:{srv.port}/def.zip',
'-d', 'dest']
adir1 = op.join(cwd, 'archives1')
adir2 = op.join(cwd, 'archives2')
os.mkdir(adir1)
os.mkdir(adir2)
with mock.patch('fsl.scripts.fsl_add_module.ARCHIVE_DIR', adir1):
args = fam.parseArgs(argv)
m, p = fam.loadManifest(args)
plugins = fam.selectPlugins(args, m, p)
fam.download(args, plugins)
assert op.exists(op.join(adir1, 'abc.zip'))
assert op.exists(op.join(adir1, 'def.zip'))
assert m['abc.zip'].archiveFile == op.join(adir1, 'abc.zip')
assert m['def.zip'].archiveFile == op.join(adir1, 'def.zip')
args = fam.parseArgs(['-a', adir2] + argv)
m, p = fam.loadManifest(args)
plugins = fam.selectPlugins(args, m, p)
fam.download(args, plugins)
assert op.exists(op.join(adir2, 'abc.zip'))
assert op.exists(op.join(adir2, 'def.zip'))
assert m['abc.zip'].archiveFile == op.join(adir2, 'abc.zip')
assert m['def.zip'].archiveFile == op.join(adir2, 'def.zip')
def test_install():
pass
with tempdir() as cwd:
manifest = [
{ 'name' : 'abc',
'url' : f'{cwd}/abc.zip',
'destination' : f'{cwd}/abcdest/'},
{ 'name' : 'def',
'url' : f'{cwd}/def.zip',
'destination' : f'{cwd}/defdest/'},
]
os.mkdir('abcdest')
os.mkdir('defdest')
make_archive('abc.zip', 'a/b', 'c/d')
make_archive('def.zip', 'e/f', 'g/h')
with open('manifest.json', 'wt') as f:
f.write(json.dumps(manifest))
args = fam.parseArgs('-m manifest.json abc.zip def.zip'.split())
manifest = fam.loadManifest(args)[0]
plugins = list(manifest.values())
fam.install(args, plugins)
assert op.exists(op.join('abcdest', 'a', 'b'))
assert op.exists(op.join('abcdest', 'c', 'd'))
assert op.exists(op.join('defdest', 'e', 'f'))
assert op.exists(op.join('defdest', 'g', 'h'))
def test_main_noargs():
# with no args, should download
# the default manifest, and ask
# the user what plugins they want
with tempdir() as cwd, server() as srv:
manifest = [
{'name' : 'abc', 'url' : f'{cwd}/abc.zip'},
{'name' : 'def', 'url' : f'http://localhost:{srv.port}/def.zip'},
]
make_archive('abc.zip', 'a/b', 'c/d')
make_archive('def.zip', 'e/f', 'g/h')
archiveDir = op.join(cwd, 'archives')
with open('manifest.json', 'wt') as f:
f.write(json.dumps(manifest))
# patch os.environ in case FSLDIR is set
fammod = 'fsl.scripts.fsl_add_module'
with mock.patch(f'{fammod}.MANIFEST_URL', f'{cwd}/manifest.json'), \
mock.patch(f'{fammod}.ARCHIVE_DIR', f'{cwd}/archives'), \
mock.patch.dict(os.environ, clear=True):
# user will be asked what plugins they want,
# and will then be asked where they want to
# install them
with mock_input('all', ''):
fam.main([])
check_dir(cwd,
[f'{archiveDir}/def.zip', 'a/b', 'c/d', 'e/f', 'g/h'],
[f'{archiveDir}/abc.zip'])
def test_main_manifest_archiveDir():
with tempdir() as cwd, server() as srv:
manifest = [
{'name' : 'abc', 'url' : f'{cwd}/abc.zip'},
{'name' : 'def', 'url' : f'http://localhost:{srv.port}/def.zip'},
]
make_archive('abc.zip', 'a/b', 'c/d')
make_archive('def.zip', 'e/f', 'g/h')
archiveDir = op.join(cwd, 'archives')
with open('manifest.json', 'wt') as f:
f.write(json.dumps(manifest))
# patch os.environ in case FSLDIR is set
with mock.patch.dict(os.environ, clear=True):
with mock_input('all', ''):
fam.main('-m manifest.json -a archives'.split())
check_dir(cwd,
[f'{archiveDir}/def.zip', 'a/b', 'c/d', 'e/f', 'g/h'],
[f'{archiveDir}/abc.zip'])
def test_main_manifest_force():
with tempdir() as cwd, server() as srv:
manifest = [
{'name' : 'abc', 'url' : f'{cwd}/abc.zip'},
{'name' : 'def', 'url' : f'http://localhost:{srv.port}/def.zip'},
]
make_archive('abc.zip', 'a/b', 'c/d')
make_archive('def.zip', 'e/f', 'g/h')
with open('manifest.json', 'wt') as f:
f.write(json.dumps(manifest))
with mock.patch.dict(os.environ, clear=True):
fam.main('-m manifest.json -a archives -f def'.split())
check_dir(cwd,
['archives/def.zip', 'e/f', 'g/h'],
['archives/abc.zip', 'a/b', 'c/d'])
def test_main_category():
with tempdir() as cwd, server() as srv:
manifest = [
{'name' : 'abc', 'url' : f'{cwd}/abc.zip', 'category' : 'a'},
{'name' : 'def', 'url' : f'{cwd}/def.zip', 'category' : 'b'},
]
make_archive('abc.zip', 'a/b', 'c/d')
make_archive('def.zip', 'e/f', 'g/h')
with open('manifest.json', 'wt') as f:
f.write(json.dumps(manifest))
with mock.patch.dict(os.environ, clear=True):
with mock_input('y', ''):
fam.main('-m manifest.json -a archives -c a'.split())
check_dir(cwd, ['a/b', 'c/d'], ['e/f', 'g/h'])
def test_main_plugin_paths():
with tempdir() as cwd, server() as srv:
make_archive('abc.zip', 'a/b', 'c/d')
make_archive('def.zip', 'e/f', 'g/h')
args = f'./abc.zip http://localhost:{srv.port}/def.zip'.split()
fammod = 'fsl.scripts.fsl_add_module'
with mock.patch(f'{fammod}.MANIFEST_URL', None), \
mock.patch(f'{fammod}.ARCHIVE_DIR', f'{cwd}/archives'), \
mock.patch.dict(os.environ, clear=True):
with mock_input(''):
fam.main(args)
check_dir(cwd,
['a/b', 'c/d', 'e/f', 'g/h', 'archives/def.zip'],
['archives/abc.zip'])
# force install
os.mkdir('dest')
fam.main(args + '-d dest -f'.split())
check_dir('dest', ['a/b', 'c/d', 'e/f', 'g/h'])
def test_main_plugins_and_manifest():
with tempdir() as cwd, server() as srv:
manifest = [
{'name' : 'abc', 'url' : f'{cwd}/abc.zip'},
{'name' : 'def', 'url' : f'http://localhost:{srv.port}/def.zip'},
]
with open('manifest.json', 'wt') as f:
f.write(json.dumps(manifest))
make_archive('abc.zip', 'a/b', 'c/d')
make_archive('def.zip', 'e/f', 'g/h')
make_archive('ghi.zip', 'i/j', 'k/l')
make_archive('jkl.zip', 'm/n', 'o/p')
args = '-m manifest.json -a archives abc def ghi.zip '\
f'http://localhost:{srv.port}/jkl.zip'.split()
with mock.patch.dict(os.environ, clear=True):
with mock_input(''):
fam.main(args)
check_dir(cwd,
['a/b', 'c/d', 'e/f', 'g/h',
'i/j', 'k/l', 'm/n', 'o/p',
'archives/def.zip', 'archives/jkl.zip'],
['archives/abc.zip', 'archives/ghi.zip'])
# force install
os.mkdir('dest')
fam.main(args + '-d dest -f'.split())
check_dir('dest',
['a/b', 'c/d', 'e/f', 'g/h',
'i/j', 'k/l', 'm/n', 'o/p'])
def test_main():
pass
......@@ -94,6 +94,42 @@ def test_selectPlugins():
assert selected == plugins
def test_selectPlugins_oneAvailable():
manifest = plgman.Manifest()
manifest.addPlugin('http://abc.com/123')
plugins = list(manifest.values())
with mock_input(''):
assert ui.selectPlugins(manifest) == plugins
with mock_input('y'):
assert ui.selectPlugins(manifest) == plugins
with mock_input('n'):
assert ui.selectPlugins(manifest) == []
def test_selectPlugins_category():
manifest = plgman.Manifest()
manifest.addPlugin('http://abc.com/123', category='a')
manifest.addPlugin('http://abc.com/456', category='b')
manifest.addPlugin('http://abc.com/789', category='b')
manifest.addPlugin('http://abc.com/abc', category='a')
manifest.addPlugin('http://abc.com/def', category='a')
manifest.addPlugin('http://abc.com/ghi', category='b')
aplugins = manifest.getCategory('a')
bplugins = manifest.getCategory('b')
with mock_input('all'):
assert ui.selectPlugins(manifest, 'a') == aplugins
with mock_input('all'):
assert ui.selectPlugins(manifest, 'b') == bplugins
with mock_input('1 3'):
assert ui.selectPlugins(manifest, 'a') == [aplugins[0], aplugins[2]]
with mock_input('1 3'):
assert ui.selectPlugins(manifest, 'b') == [bplugins[0], bplugins[2]]
def test_confirmDestination():
with tempdir() as tdir:
......
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