Commit f4f4c5fb authored by Paul McCarthy's avatar Paul McCarthy 🚵
Browse files

TEST: update tests

parent dadf3877
#!/usr/bin/env python
import os
import os.path as op
import shutil
import subprocess as sp
import sys
import textwrap as tw
# py3
try:
from unittest import mock
# py2
except ImportError:
import mock
import pytest
from . import onpath, server
import fslinstaller as inst
def test_Context_identify_plaform():
tests = [
[('linux', 'x86_64'), 'linux-64'],
[('darwin', 'x86_64'), 'macos-64'],
[('darwin', 'arm64'), 'macos-64'],
]
for info, expected in tests:
sys, cpu = info
with mock.patch('platform.system', return_value=sys), \
mock.patch('platform.machine', return_value=cpu):
assert inst.Context.identify_platform() == expected
def test_Context_get_admin_password():
sudo = tw.dedent("""
#!/usr/bin/env bash
echo -n "Password: "
read -e password
if [ "$password" = "password" ]; then exit 0
else exit 1
fi
""").strip()
with inst.tempdir() as cwd:
path = op.pathsep.join((cwd, os.environ['PATH']))
with open('sudo', 'wt') as f:
f.write(sudo)
os.chmod('sudo', 0o755)
# right password first time
with mock.patch.dict(os.environ, PATH=path), \
mock.patch('getpass.getpass', return_value='password'):
assert inst.Context.get_admin_password() == 'password'
# wrong, then right
returnvals = ['wrong', 'password']
def getpass(*a):
return returnvals.pop(0)
with mock.patch.dict(os.environ, PATH=path), \
mock.patch('getpass.getpass', getpass):
assert inst.Context.get_admin_password() == 'password'
# wrong wrong wrong
returnvals = ['wrong', 'bad', 'no']
def getpass(*a):
return returnvals.pop(0)
with mock.patch.dict(os.environ, PATH=path), \
mock.patch('getpass.getpass', getpass):
with pytest.raises(Exception):
inst.Context.get_admin_password()
......@@ -6,7 +6,7 @@ import contextlib
import shutil
import json
import fslinstaller as inst
import fsl.fslinstaller as inst
try:
from unittest import mock
......@@ -129,7 +129,7 @@ def installer_server(cwd=None):
manifest = mock_manifest.format(
version=inst.__version__,
platform=inst.Context.identify_platform(),
platform=inst.identify_platform(),
url=srv.url,
conda_sha256=conda_sha256,
env610_sha256=env610_sha256,
......@@ -187,7 +187,7 @@ def check_install(homedir, destdir, version, envver=None):
def test_installer_normal_interactive_usage():
with inst.tempdir():
with installer_server() as srv:
with mock.patch('fslinstaller.FSL_RELEASE_MANIFEST',
with mock.patch('fsl.fslinstaller.FSL_RELEASE_MANIFEST',
'{}/manifest.json'.format(srv.url)):
# accept rel/abs paths
for i in range(3):
......@@ -203,10 +203,10 @@ def test_installer_normal_interactive_usage():
def test_installer_list_versions():
platform = inst.Context.identify_platform()
platform = inst.identify_platform()
with inst.tempdir():
with installer_server() as srv:
with mock.patch('fslinstaller.FSL_RELEASE_MANIFEST',
with mock.patch('fsl.fslinstaller.FSL_RELEASE_MANIFEST',
'{}/manifest.json'.format(srv.url)):
with inst.tempdir() as cwd:
with CaptureStdout() as cap:
......@@ -226,7 +226,7 @@ def test_installer_list_versions():
def test_installer_normal_cli_usage():
with inst.tempdir():
with installer_server() as srv:
with mock.patch('fslinstaller.FSL_RELEASE_MANIFEST',
with mock.patch('fsl.fslinstaller.FSL_RELEASE_MANIFEST',
'{}/manifest.json'.format(srv.url)):
# accept rel/abs paths
......@@ -250,7 +250,7 @@ def test_installer_normal_cli_usage():
def test_installer_devrelease():
with inst.tempdir():
with installer_server() as srv:
with mock.patch('fslinstaller.FSL_DEV_RELEASES',
with mock.patch('fsl.fslinstaller.FSL_DEV_RELEASES',
'{}/devreleases.txt'.format(srv.url)):
patch_manifest('manifest.json',
'manifest-6.1.0.20220518.abcdefg.master.json',
......
......@@ -15,7 +15,7 @@ import pytest
from . import onpath, server
import fslinstaller as inst
import fsl.fslinstaller as inst
SUDO = """
......
......@@ -18,7 +18,21 @@ import pytest
from . import onpath, server
import fslinstaller as inst
import fsl.fslinstaller as inst
def test_identify_plaform():
tests = [
[('linux', 'x86_64'), 'linux-64'],
[('darwin', 'x86_64'), 'macos-64'],
[('darwin', 'arm64'), 'macos-64'],
]
for info, expected in tests:
sys, cpu = info
with mock.patch('platform.system', return_value=sys), \
mock.patch('platform.machine', return_value=cpu):
assert inst.identify_platform() == expected
def test_Version():
......@@ -46,6 +60,47 @@ def test_read_fslversion():
assert inst.read_fslversion(cwd) == 'abcde'
def test_get_admin_password():
sudo = tw.dedent("""
#!/usr/bin/env bash
echo -n "Password: "
read -e password
if [ "$password" = "password" ]; then exit 0
else exit 1
fi
""").strip()
with inst.tempdir() as cwd:
path = op.pathsep.join((cwd, os.environ['PATH']))
with open('sudo', 'wt') as f:
f.write(sudo)
os.chmod('sudo', 0o755)
# right password first time
with mock.patch.dict(os.environ, PATH=path), \
mock.patch('getpass.getpass', return_value='password'):
assert inst.get_admin_password() == 'password'
# wrong, then right
returnvals = ['wrong', 'password']
def getpass(*a):
return returnvals.pop(0)
with mock.patch.dict(os.environ, PATH=path), \
mock.patch('getpass.getpass', getpass):
assert inst.get_admin_password() == 'password'
# wrong wrong wrong
returnvals = ['wrong', 'bad', 'no']
def getpass(*a):
return returnvals.pop(0)
with mock.patch.dict(os.environ, PATH=path), \
mock.patch('getpass.getpass', getpass):
with pytest.raises(Exception):
inst.get_admin_password()
def test_download_file():
with inst.tempdir() as cwd:
......
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