|
1 | 1 | import pytest |
| 2 | +from unittest.mock import patch, MagicMock |
2 | 3 | from click.testing import CliRunner |
3 | 4 |
|
4 | 5 | import logging |
|
7 | 8 | from .null_command import NullCommandContext |
8 | 9 |
|
9 | 10 | 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.) |
10 | 14 | 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() |
13 | 30 |
|
14 | 31 | 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'] |
17 | 35 |
|
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' |
22 | 48 |
|
23 | 49 | def test_format_commands(self): |
24 | 50 | pytest.skip() |
25 | 51 | # use a mock to get the formatter |
26 | 52 | # test that it skips a section if it is empty |
27 | 53 | pass |
28 | 54 |
|
| 55 | + |
29 | 56 | @pytest.mark.parametrize('with_log', [True, False]) |
30 | 57 | def test_main_log(with_log): |
31 | 58 | logged_stdout = "About to run command\n" |
|
0 commit comments