Skip to content

Commit 2093b0d

Browse files
committed
start to tests for OpenPathSamplingCLI class
1 parent 38adf1e commit 2093b0d

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

paths_cli/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def _deregister_plugin(self, plugin):
6060
self._sections[plugin.section].remove(plugin.name)
6161

6262
def plugin_for_command(self, command_name):
63-
return {p.name: p for p in self.plugins}[name]
63+
return {p.name: p for p in self.plugins}[command_name]
6464

6565
@staticmethod
6666
def _list_plugin_files(plugin_folders):

paths_cli/tests/test_cli.py

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pytest
2+
from unittest.mock import patch, MagicMock
23
from click.testing import CliRunner
34

45
import logging
@@ -7,25 +8,51 @@
78
from .null_command import NullCommandContext
89

910
class TestOpenPathSamplingCLI(object):
11+
# TODO: more thorough testing of private methods to find/register
12+
# plugins might be nice; so far we mainly focus on testing the API.
13+
# (Still have smoke tests covering everything, though.)
1014
def setup(self):
11-
# TODO: patch out the directory to fake the plugins
12-
self.cli = OpenPathSamplingCLI()
15+
self.plugin_dict = {
16+
'foo': OPSPlugin(name='foo',
17+
filename='foo.py',
18+
func=lambda: 'foo',
19+
section='Simulation'),
20+
'foo-bar': OPSPlugin(name='foo-bar',
21+
filename='foo_bar.py',
22+
func=lambda: 'foobar',
23+
section='Miscellaneous')
24+
}
25+
self.fake_plugins = list(self.plugin_dict.values())
26+
mock_plugins = MagicMock(return_value=self.fake_plugins)
27+
with patch.object(OpenPathSamplingCLI, '_load_plugin_files',
28+
mock_plugins):
29+
self.cli = OpenPathSamplingCLI()
1330

1431
def test_plugins(self):
15-
pytest.skip()
16-
pass
32+
assert self.cli.plugins == self.fake_plugins
33+
assert self.cli._sections['Simulation'] == ['foo']
34+
assert self.cli._sections['Miscellaneous'] == ['foo-bar']
1735

18-
def test_get_command(self):
19-
# test renamings
20-
pytest.skip()
21-
pass
36+
@pytest.mark.parametrize('name', ['foo', 'foo-bar'])
37+
def test_plugin_for_command(self, name):
38+
assert self.cli.plugin_for_command(name) == self.plugin_dict[name]
39+
40+
def test_list_commands(self):
41+
assert self.cli.list_commands(ctx=None) == ['foo', 'foo-bar']
42+
43+
@pytest.mark.parametrize('command', ['foo-bar', 'foo_bar'])
44+
def test_get_command(self, command):
45+
# this tests that renamings work
46+
cmd = self.cli.get_command(ctx=None, name=command)
47+
assert cmd() == 'foobar'
2248

2349
def test_format_commands(self):
2450
pytest.skip()
2551
# use a mock to get the formatter
2652
# test that it skips a section if it is empty
2753
pass
2854

55+
2956
@pytest.mark.parametrize('with_log', [True, False])
3057
def test_main_log(with_log):
3158
logged_stdout = "About to run command\n"

0 commit comments

Comments
 (0)