Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion paths_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def _deregister_plugin(self, plugin):
self._sections[plugin.section].remove(plugin.name)

def plugin_for_command(self, command_name):
return {p.name: p for p in self.plugins}[name]
return {p.name: p for p in self.plugins}[command_name]

@staticmethod
def _list_plugin_files(plugin_folders):
Expand Down
79 changes: 65 additions & 14 deletions paths_cli/tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,81 @@
import pytest
from unittest.mock import patch, MagicMock
from click.testing import CliRunner

import logging

from paths_cli.cli import *
from .null_command import NullCommandContext


class TestOpenPathSamplingCLI(object):
# TODO: more thorough testing of private methods to find/register
# plugins might be nice; so far we mainly focus on testing the API.
# (Still have smoke tests covering everything, though.)
def setup(self):
# TODO: patch out the directory to fake the plugins
self.cli = OpenPathSamplingCLI()
def make_mock(name, helpless=False):
mock = MagicMock(return_value=name)
if helpless:
mock.short_help = None
else:
mock.short_help = name + " help"
return mock

self.plugin_dict = {
'foo': OPSPlugin(name='foo',
filename='foo.py',
func=make_mock('foo'),
section='Simulation'),
'foo-bar': OPSPlugin(name='foo-bar',
filename='foo_bar.py',
func=make_mock('foobar', helpless=True),
section='Miscellaneous')
}
self.fake_plugins = list(self.plugin_dict.values())
mock_plugins = MagicMock(return_value=self.fake_plugins)
with patch.object(OpenPathSamplingCLI, '_load_plugin_files',
mock_plugins):
self.cli = OpenPathSamplingCLI()

def test_plugins(self):
pytest.skip()
pass
assert self.cli.plugins == self.fake_plugins
assert self.cli._sections['Simulation'] == ['foo']
assert self.cli._sections['Miscellaneous'] == ['foo-bar']

@pytest.mark.parametrize('name', ['foo', 'foo-bar'])
def test_plugin_for_command(self, name):
assert self.cli.plugin_for_command(name) == self.plugin_dict[name]

def test_list_commands(self):
assert self.cli.list_commands(ctx=None) == ['foo', 'foo-bar']

def test_get_command(self):
# test renamings
pytest.skip()
pass
@pytest.mark.parametrize('command', ['foo-bar', 'foo_bar'])
def test_get_command(self, command):
# this tests that renamings work
cmd = self.cli.get_command(ctx=None, name=command)
assert cmd() == 'foobar'

def test_format_commands(self):
pytest.skip()
# use a mock to get the formatter
# test that it skips a section if it is empty
pass
class MockFormatter(object):
def __init__(self):
self.title = None
self.contents = {}

def section(self, title):
self.title = title
return MagicMock()

def write_dl(self, rows):
self.contents[self.title] = rows

formatter = MockFormatter()
# add a non-existent command; tests when get_command is None
self.cli._sections['Workflow'] = ['baz']
self.cli.format_commands(ctx=None, formatter=formatter)
foo_row = ('foo', 'foo help')
foobar_row = ('foo-bar', '')
assert formatter.contents['Simulation Commands'] == [foo_row]
assert formatter.contents['Miscellaneous Commands'] == [foobar_row]
assert len(formatter.contents) == 2


@pytest.mark.parametrize('with_log', [True, False])
def test_main_log(with_log):
Expand Down