Commit 047e5bae authored by Paul McCarthy's avatar Paul McCarthy 🚵
Browse files

TEST: test findConfigFile/Dirs

parent 8d7c7ec0
......@@ -105,6 +105,13 @@ def touch(path):
f.write(path)
def mktree(filepaths):
for path in filepaths:
os.makedirs(op.dirname(path), exist_ok=True)
touch(path)
def gen_test_data(num_vars,
num_subjs,
out_file,
......
......@@ -5,14 +5,71 @@
# Author: Paul McCarthy <pauldmccarthy@gmail.com>
#
import os
import os.path as op
import multiprocessing as mp
from unittest import mock
import contextlib as ctxlib
import pytest
import funpack.util as util
from . import tempdir, patch_logging
from . import tempdir, patch_logging, mktree
def test_findConfigFile():
# cfgdirs are (in order of precedence):
# FUNPACK_CONFIG_DIR
# site.getsitepackages()
# dirname(funpack.util.__file__)
env = os.environ.copy()
env.pop('FUNPACK_CONFIG_DIR')
with tempdir() as td, mock.patch.dict('os.environ', env, clear=True):
cfgdir = op.join(td, 'cfgdir')
sitedir = op.join(td, 'sitepack')
fpdir = op.join(td, 'fpdir')
cfgdircfg = op.join(cfgdir, 'myconfig.cfg')
sitedircfg = op.join(sitedir, 'funpack', 'configs', 'myconfig.cfg')
fpdircfg = op.join(fpdir, 'configs', 'myconfig.cfg')
cfgdirplg = op.join(cfgdir, 'myplugin.py')
sitedirplg = op.join(sitedir, 'funpack', 'plugins', 'myplugin.py')
fpdirplg = op.join(fpdir, 'plugins', 'myplugin.py')
mktree((cfgdircfg, sitedircfg, fpdircfg,
cfgdirplg, sitedirplg, fpdirplg))
@ctxlib.contextmanager
def mock_cfgdir():
with mock.patch.dict(os.environ, FUNPACK_CONFIG_DIR=cfgdir):
yield
@ctxlib.contextmanager
def mock_sitepackages():
with mock.patch('site.getsitepackages', return_value=[sitedir]):
yield
@ctxlib.contextmanager
def mock_funpack_dir():
utilfile = op.join(fpdir, 'util.py')
with mock.patch('funpack.util.__file__', utilfile):
yield
with mock_funpack_dir():
assert util.findConfigFile('myconfig.cfg') == fpdircfg
assert util.findConfigFile('myconfig') == fpdircfg
assert util.findPluginFile('myplugin.py') == fpdirplg
assert util.findPluginFile('myplugin') == fpdirplg
with mock_funpack_dir(), mock_sitepackages():
assert util.findConfigFile('myconfig.cfg') == sitedircfg
assert util.findConfigFile('myconfig') == sitedircfg
assert util.findPluginFile('myplugin.py') == sitedirplg
assert util.findPluginFile('myplugin') == sitedirplg
with mock_funpack_dir(), mock_sitepackages(), mock_cfgdir():
assert util.findConfigFile('myconfig.cfg') == cfgdircfg
assert util.findConfigFile('myconfig') == cfgdircfg
assert util.findPluginFile('myplugin.py') == cfgdirplg
assert util.findPluginFile('myplugin') == cfgdirplg
def test_parseMatlabRange():
......
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