| 1 | """ |
| 2 | Module to test CLI functionality of click-man package. |
| 3 | """ |
| 4 | |
| 5 | import importlib.metadata |
| 6 | import os |
| 7 | from unittest import mock |
| 8 | |
| 9 | import click |
| 10 | from click.testing import CliRunner as CLIRunner |
| 11 | |
| 12 | from click_man import shell |
| 13 | |
| 14 | |
| 15 | @mock.patch.object(shell, '_get_entry_point') |
| 16 | def test_cli__missing_entry_point(mock_entry_point): |
| 17 | mock_entry_point.return_value = None |
| 18 | |
| 19 | runner = CLIRunner() |
| 20 | result = runner.invoke(shell.cli, 'foo') |
| 21 | |
| 22 | assert result.exit_code == 1, result |
| 23 | assert 'not an installed console script' in result.output.strip() |
| 24 | |
| 25 | mock_entry_point.assert_called_once_with('foo') |
| 26 | |
| 27 | |
| 28 | @mock.patch('os.makedirs', new=mock.Mock()) |
| 29 | @mock.patch.object(shell, 'write_man_pages') |
| 30 | @mock.patch.object(click, 'echo') |
| 31 | @mock.patch.object(shell, '_get_entry_point') |
| 32 | def test_cli__valid(mock_entry_point, mock_echo, mock_write): |
| 33 | fake_target = os.path.join(os.getcwd(), 'man') |
| 34 | fake_command = click.Command(name='foo') |
| 35 | fake_version = '1.2.3' |
| 36 | entry_point = mock.Mock(spec=importlib.metadata.EntryPoint) |
| 37 | entry_point.load.return_value = fake_command |
| 38 | entry_point.version = fake_version |
| 39 | |
| 40 | mock_entry_point.return_value = entry_point |
| 41 | |
| 42 | runner = CLIRunner() |
| 43 | result = runner.invoke(shell.cli, ['foo']) |
| 44 | |
| 45 | assert result.exit_code == 0, result.output |
| 46 | |
| 47 | mock_entry_point.assert_called_once_with('foo') |
| 48 | mock_echo.assert_has_calls( |
| 49 | [ |
| 50 | mock.call('Load entry point foo'), |
| 51 | mock.call('Generate man pages for foo in %s' % fake_target), |
| 52 | ] |
| 53 | ) |
| 54 | mock_write.assert_called_once_with( |
| 55 | 'foo', |
| 56 | fake_command, |
| 57 | version=fake_version, |
| 58 | target_dir=fake_target, |
| 59 | date=None, |
| 60 | ) |
| 61 | |
| 62 | |
| 63 | @mock.patch('os.makedirs', new=mock.Mock()) |
| 64 | @mock.patch.object(shell, 'write_man_pages') |
| 65 | @mock.patch.object(click, 'echo') |
| 66 | @mock.patch.object(shell, '_get_entry_point') |
| 67 | def test_cli__with_man_date_version(mock_entry_point, mock_echo, mock_write): |
| 68 | fake_target = os.path.join(os.getcwd(), 'man') |
| 69 | fake_command = click.Command(name='foo') |
| 70 | entry_point = mock.Mock(spec=importlib.metadata.EntryPoint) |
| 71 | entry_point.load.return_value = fake_command |
| 72 | |
| 73 | mock_entry_point.return_value = entry_point |
| 74 | |
| 75 | runner = CLIRunner() |
| 76 | result = runner.invoke( |
| 77 | shell.cli, |
| 78 | ['foo', '--man-version', '3.2.1', '--man-date', '2020-01-01'], |
| 79 | ) |
| 80 | |
| 81 | assert result.exit_code == 0, result.output |
| 82 | |
| 83 | mock_entry_point.assert_called_once_with('foo') |
| 84 | mock_echo.assert_has_calls( |
| 85 | [ |
| 86 | mock.call('Load entry point foo'), |
| 87 | mock.call('Generate man pages for foo in %s' % fake_target), |
| 88 | ] |
| 89 | ) |
| 90 | mock_write.assert_called_once_with( |
| 91 | 'foo', |
| 92 | fake_command, |
| 93 | version='3.2.1', |
| 94 | target_dir=fake_target, |
| 95 | date='2020-01-01', |
| 96 | ) |