Skip to content

Commit 359350d

Browse files
committed
tests for cli.format_commands
1 parent 2093b0d commit 359350d

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

paths_cli/tests/test_cli.py

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,31 @@
22
from unittest.mock import patch, MagicMock
33
from click.testing import CliRunner
44

5-
import logging
6-
75
from paths_cli.cli import *
86
from .null_command import NullCommandContext
97

8+
109
class TestOpenPathSamplingCLI(object):
1110
# TODO: more thorough testing of private methods to find/register
1211
# plugins might be nice; so far we mainly focus on testing the API.
1312
# (Still have smoke tests covering everything, though.)
1413
def setup(self):
14+
def make_mock(name, helpless=False):
15+
mock = MagicMock(return_value=name)
16+
if helpless:
17+
mock.short_help = None
18+
else:
19+
mock.short_help = name + " help"
20+
return mock
21+
1522
self.plugin_dict = {
1623
'foo': OPSPlugin(name='foo',
1724
filename='foo.py',
18-
func=lambda: 'foo',
25+
func=make_mock('foo'),
1926
section='Simulation'),
2027
'foo-bar': OPSPlugin(name='foo-bar',
2128
filename='foo_bar.py',
22-
func=lambda: 'foobar',
29+
func=make_mock('foobar', helpless=True),
2330
section='Miscellaneous')
2431
}
2532
self.fake_plugins = list(self.plugin_dict.values())
@@ -47,10 +54,27 @@ def test_get_command(self, command):
4754
assert cmd() == 'foobar'
4855

4956
def test_format_commands(self):
50-
pytest.skip()
51-
# use a mock to get the formatter
52-
# test that it skips a section if it is empty
53-
pass
57+
class MockFormatter(object):
58+
def __init__(self):
59+
self.title = None
60+
self.contents = {}
61+
62+
def section(self, title):
63+
self.title = title
64+
return MagicMock()
65+
66+
def write_dl(self, rows):
67+
self.contents[self.title] = rows
68+
69+
formatter = MockFormatter()
70+
# add a non-existent command; tests when get_command is None
71+
self.cli._sections['Workflow'] = ['baz']
72+
self.cli.format_commands(ctx=None, formatter=formatter)
73+
foo_row = ('foo', 'foo help')
74+
foobar_row = ('foo-bar', '')
75+
assert formatter.contents['Simulation Commands'] == [foo_row]
76+
assert formatter.contents['Miscellaneous Commands'] == [foobar_row]
77+
assert len(formatter.contents) == 2
5478

5579

5680
@pytest.mark.parametrize('with_log', [True, False])

0 commit comments

Comments
 (0)