def test_use(): with tempdir.tempdir(changeto=False) as td1,\ tempdir.tempdir(changeto=False) as td2: s1 = settings.Settings(cfgdir=td1, writeOnExit=False) s2 = settings.Settings(cfgdir=td2, writeOnExit=False) with settings.use(s1): with settings.writeFile('file1') as f: f.write('hi!') assert os.listdir(td1) == ['file1'] assert os.listdir(td2) == [] with settings.use(s2): with settings.writeFile('file2') as f: f.write('hi!') settings.set(s1) with settings.writeFile('file3') as f: f.write('hi!') with settings.use(s2): with settings.writeFile('file4') as f: f.write('hi!') assert sorted(os.listdir(td1)) == ['file1', 'file3'] assert sorted(os.listdir(td2)) == ['file2', 'file4'] # should go back to s1 with settings.writeFile('file5') as f: f.write('hi!') assert sorted(os.listdir(td1)) == ['file1', 'file3', 'file5'] assert sorted(os.listdir(td2)) == ['file2', 'file4'] try: with settings.use(s2): raise Exception('hur') except Exception: pass # should go back to s1 with settings.writeFile('file6') as f: f.write('hi!') assert sorted(os.listdir(td1)) == ['file1', 'file3', 'file5', 'file6'] assert sorted(os.listdir(td2)) == ['file2', 'file4']
def mockSettings(): with tempdir() as td: fakesettings = fslsettings.Settings('fsleyes', cfgdir=td, writeOnExit=False) os.makedirs(op.join(td, 'colourmaps')) os.makedirs(op.join(td, 'luts')) with fslsettings.use(fakesettings): yield td
def test_installPlugin(): with tempdir.tempdir() as td: with open('test_installplugin.py', 'wt') as f: f.write(code.format(prefix='Install')) s = fslsettings.Settings('test_plugins', cfgdir=td, writeOnExit=False) with fslsettings.use(s): plugins.installPlugin('test_installplugin.py') mod = sys.modules['fsleyes_plugin_test_installplugin'] assert 'fsleyes-plugin-test-installplugin' in plugins.listPlugins() assert plugins.listTools()[ 'InstallTool'] is mod.InstallTool assert plugins.listControls()['InstallControl'] is mod.InstallControl assert plugins.listViews()[ 'InstallView'] is mod.InstallView assert op.exists(op.join(td, 'plugins', 'test_installplugin.py'))
def send(line): """If a cli server is running (see :func:`runserver` and :func:`_serverloop`), the given ``args`` are sent to it. A :exc:`NotRunningError` is raised if a server loop is not running. """ if not isRunning(): raise NotRunningError() with fslsettings.use(fslsettings.Settings('fsleyes', writeOnExit=False)): port = int(fslsettings.readFile('cliserver.txt').strip()) line = (line + '\n').encode() sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect(('localhost', port)) log.debug('Sending to port %i: %s...', port, line[:50]) sock.sendall(line)
def _test_LoadPluginAction(frame, overlayList, displayCtx): act = loadplugin.LoadPluginAction(overlayList, displayCtx, frame) class FileDialog(object): ShowModal_return = wx.ID_OK GetPath_return = '' def __init__(self, *args, **kwargs): pass def ShowModal(self): return FileDialog.ShowModal_return def GetPath(self): return FileDialog.GetPath_return class MessageDialog(object): ShowModal_return = wx.ID_OK def __init__(self, *args, **kwargs): pass def ShowModal(self): return MessageDialog.ShowModal_return @contextlib.contextmanager def reportIfError(*a, **kwa): try: yield except Exception: pass with mock.patch('wx.FileDialog', FileDialog), \ mock.patch('wx.MessageDialog', MessageDialog), \ mock.patch('fsleyes_widgets.utils.status.reportIfError', reportIfError): FileDialog.ShowModal_return = wx.ID_CANCEL act() FileDialog.ShowModal_return = wx.ID_OK FileDialog.GetPath_return = 'bad path' MessageDialog.ShowModal_return = wx.ID_CANCEL act() FileDialog.ShowModal_return = wx.ID_OK FileDialog.GetPath_return = 'bad path' MessageDialog.ShowModal_return = wx.ID_NO act() FileDialog.ShowModal_return = wx.ID_OK FileDialog.GetPath_return = 'bad path' MessageDialog.ShowModal_return = wx.ID_YES act() FileDialog.ShowModal_return = wx.ID_OK FileDialog.GetPath_return = 'bad path' MessageDialog.ShowModal_return = wx.ID_YES act() with tempdir.tempdir(changeto=False) as td, \ fslsettings.use(fslsettings.Settings(cfgdir=td)): with open(op.join(td, 'test_loadpluginaction.py'), 'wt') as f: f.write(code) FileDialog.GetPath_return = op.join(td, 'test_loadpluginaction.py') act() assert 'fsleyes-plugin-test-loadpluginaction' in plugins.listPlugins( )