|
2 | 2 | from unittest.mock import patch, MagicMock |
3 | 3 | from click.testing import CliRunner |
4 | 4 |
|
5 | | -import logging |
6 | | - |
7 | 5 | from paths_cli.cli import * |
8 | 6 | from .null_command import NullCommandContext |
9 | 7 |
|
| 8 | + |
10 | 9 | class TestOpenPathSamplingCLI(object): |
11 | 10 | # TODO: more thorough testing of private methods to find/register |
12 | 11 | # plugins might be nice; so far we mainly focus on testing the API. |
13 | 12 | # (Still have smoke tests covering everything, though.) |
14 | 13 | 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 | + |
15 | 22 | self.plugin_dict = { |
16 | 23 | 'foo': OPSPlugin(name='foo', |
17 | 24 | filename='foo.py', |
18 | | - func=lambda: 'foo', |
| 25 | + func=make_mock('foo'), |
19 | 26 | section='Simulation'), |
20 | 27 | 'foo-bar': OPSPlugin(name='foo-bar', |
21 | 28 | filename='foo_bar.py', |
22 | | - func=lambda: 'foobar', |
| 29 | + func=make_mock('foobar', helpless=True), |
23 | 30 | section='Miscellaneous') |
24 | 31 | } |
25 | 32 | self.fake_plugins = list(self.plugin_dict.values()) |
@@ -47,10 +54,27 @@ def test_get_command(self, command): |
47 | 54 | assert cmd() == 'foobar' |
48 | 55 |
|
49 | 56 | 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 |
54 | 78 |
|
55 | 79 |
|
56 | 80 | @pytest.mark.parametrize('with_log', [True, False]) |
|
0 commit comments