Skip to content
Snippets Groups Projects
Commit 661241c8 authored by Paul McCarthy's avatar Paul McCarthy :mountain_bicyclist:
Browse files

CI: Basic unit tests for wrapper creation/removal

parent 0fa112b4
No related branches found
No related tags found
1 merge request!1Enh/create wrapper
#!/usr/bin/env python
#
# test_create_remove_wrapper.py - Test the createFSLWrapper.sh and
# removeFSLWrapper.sh scripts. Requires Python 3.7.
#
# Author: Paul McCarthy <pauldmccarthy@gmail.com>
#
import os.path as op
import subprocess as sp
import textwrap as tw
import os
import shlex
import shutil
import tempfile
import contextlib
from unittest import mock
BASE_DIR = op.abspath(op.join(op.dirname(__file__), '..'))
CREATE_WRAPPER = op.join(BASE_DIR, 'etc', 'fslconf', 'createFSLWrapper.sh')
REMOVE_WRAPPER = op.join(BASE_DIR, 'etc', 'fslconf', 'removeFSLWrapper.sh')
def run(cmd, **kwargs):
return sp.run(shlex.split(cmd), check=True, **kwargs)
@contextlib.contextmanager
def temp_fsldir():
testdir = tempfile.mkdtemp()
prevdir = os.getcwd()
fsldir = op.join(testdir, 'fsl')
wrapperdir = op.join(testdir, 'fsl', 'share', 'fsl', 'bin')
try:
os.chdir(testdir)
os.mkdir(fsldir)
with mock.patch.dict(os.environ, {
'FSLDIR' : fsldir,
'PREFIX' : fsldir,
'FSL_CREATE_WRAPPER_SCRIPTS' : '1'}):
yield fsldir, wrapperdir
finally:
os.chdir(prevdir)
shutil.rmtree(testdir)
def touch(path):
dirname = op.dirname(path)
if not op.exists(dirname):
os.makedirs(dirname)
with open(path, 'wt') as f:
f.write('.')
def test_env_vars_not_set():
"""Test that wrapper scripts are not created if the
FSL_CREATE_WRAPPER_SCRIPTS, FSLDIR, or PREFIX environment variables
are not set.
"""
with temp_fsldir() as (fsldir, wrapperdir):
touch(op.join(fsldir, 'bin', 'test_script'))
env = os.environ.copy()
env.pop('FSL_CREATE_WRAPPER_SCRIPTS')
run(f'{CREATE_WRAPPER} test_script', env=env)
assert not op.exists(op.join(wrapperdir, 'test_script1'))
env = os.environ.copy()
env.pop('FSLDIR')
run(f'{CREATE_WRAPPER} test_script', env=env)
assert not op.exists(op.join(wrapperdir, 'test_script1'))
env = os.environ.copy()
env.pop('PREFIX')
run(f'{CREATE_WRAPPER} test_script', env=env)
assert not op.exists(op.join(wrapperdir, 'test_script1'))
# FSLDIR invalid
env = os.environ.copy()
env['FSLDIR'] = '/some/non-existent/path'
run(f'{CREATE_WRAPPER} test_script', env=env)
assert not op.exists(op.join(wrapperdir, 'test_script1'))
# FSLDIR != PREFIX
env = os.environ.copy()
env['FSLDIR'] = op.join(env['PREFIX'], 'other_fsl')
run(f'{CREATE_WRAPPER} test_script', env=env)
assert not op.exists(op.join(wrapperdir, 'test_script1'))
def test_create_python_wrapper():
"""Test creation of a wrapper script for a python executable"""
with temp_fsldir() as (fsldir, wrapperdir):
script_path = op.join(fsldir, 'bin', 'test_script')
wrapper_path = op.join(wrapperdir, 'test_script')
touch(script_path)
with open(script_path, 'wt') as f:
f.write('#!/usr/bin/env python\n')
f.write('print("hello")\n')
expect = tw.dedent(f"""
#!/usr/bin/env bash
{fsldir}/bin/python -I {script_path} "$@"
""").strip()
run(f'{CREATE_WRAPPER} test_script')
assert op.exists(wrapper_path)
with open(wrapper_path, 'rt') as f:
got = f.read().strip()
assert got == expect
def test_create_other_wrapper():
"""Test creation of a wrapper script for a non-python executable."""
with temp_fsldir() as (fsldir, wrapperdir):
script_path = op.join(fsldir, 'bin', 'test_script')
wrapper_path = op.join(wrapperdir, 'test_script')
touch(script_path)
with open(op.join(fsldir, 'bin', 'test_script'), 'wt') as f:
f.write('#!/usr/bin/env bash\n')
f.write('echo "hello"\n')
expect = tw.dedent(f"""
#!/usr/bin/env bash
{script_path} "$@"
""").strip()
run(f'{CREATE_WRAPPER} test_script')
assert op.exists(wrapper_path)
with open(wrapper_path, 'rt') as f:
got = f.read().strip()
assert got == expect
def test_permissions_preserved():
"""Test that wrapper script has same permissions as wrapped script."""
with temp_fsldir() as (fsldir, wrapperdir):
perms = [0o777, 0o755, 0o644, 0o600, 0o755, 0o700]
script_path = op.join(fsldir, 'bin', 'test_script')
wrapper_path = op.join(wrapperdir, 'test_script')
touch(script_path)
for perm in perms:
os.chmod(script_path, perm)
run(f'{CREATE_WRAPPER} test_script')
stat = os.stat(wrapper_path)
assert (stat.st_mode & 0o777) == perm
def test_create_remove_wrappers():
"""Tests normal usage. """
with temp_fsldir() as (fsldir, wrapperdir):
touch(op.join(fsldir, 'bin', 'test_script1'))
touch(op.join(fsldir, 'bin', 'test_script2'))
run(f'{CREATE_WRAPPER} test_script1 test_script2')
assert op.exists(op.join(wrapperdir, 'test_script1'))
assert op.exists(op.join(wrapperdir, 'test_script2'))
run(f'{REMOVE_WRAPPER} test_script1 test_script2')
assert not op.exists(op.join(wrapperdir, 'test_script1'))
assert not op.exists(op.join(wrapperdir, 'test_script2'))
include:
- project: fsl/fsl-ci-rules
file: .gitlab-ci.yml
stages:
- test
- fsl-ci-pre
- fsl-ci-build
- fsl-ci-deploy
test:
stage: test
image: python:3.7
tags:
- docker
script:
- pip install pytest
- pytest -v .ci
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment