| 1 |
"""Basic unit tests of parse_command_line().""" |
| 2 |
# pylint: disable=invalid-name,import-error |
| 3 |
# ruff: noqa: N803 |
| 4 |
|
| 5 |
import copy |
| 6 |
import unittest |
| 7 |
import unittest.mock |
| 8 |
|
| 9 |
from foomuuri import CONFIG as BASE_CONFIG |
| 10 |
from foomuuri import INTERNAL as BASE_INTERNAL |
| 11 |
from foomuuri import parse_command_line |
| 12 |
|
| 13 |
|
| 14 |
@unittest.mock.patch('foomuuri.CONFIG_OVERRIDE', new_callable=dict) |
| 15 |
@unittest.mock.patch( |
| 16 |
'foomuuri.CONFIG', new_callable=lambda: copy.deepcopy(BASE_CONFIG) |
| 17 |
) |
| 18 |
@unittest.mock.patch( |
| 19 |
'foomuuri.INTERNAL', new_callable=lambda: copy.deepcopy(BASE_INTERNAL) |
| 20 |
) |
| 21 |
class TestParseCommandLine(unittest.TestCase): |
| 22 |
"""Test parse_command_line().""" |
| 23 |
|
| 24 |
@unittest.mock.patch('sys.argv', ['foomuuri']) |
| 25 |
def test_no_arguments(self, INTERNAL, *_): |
| 26 |
"""Test no arguments.""" |
| 27 |
parse_command_line() |
| 28 |
self.assertEqual(INTERNAL.command, '') |
| 29 |
self.assertEqual(INTERNAL.parameters, []) |
| 30 |
|
| 31 |
@unittest.mock.patch('sys.argv', ['foomuuri', '--help']) |
| 32 |
def test_option_help(self, INTERNAL, *_): |
| 33 |
"""Test --help option (sets 'help' command).""" |
| 34 |
parse_command_line() |
| 35 |
self.assertEqual(INTERNAL.command, 'help') |
| 36 |
self.assertEqual(INTERNAL.parameters, []) |
| 37 |
|
| 38 |
@unittest.mock.patch('sys.argv', ['foomuuri', '--version']) |
| 39 |
def test_option_version(self, INTERNAL, *_): |
| 40 |
"""Test --version option (sets 'version' command).""" |
| 41 |
parse_command_line() |
| 42 |
self.assertEqual(INTERNAL.command, 'version') |
| 43 |
self.assertEqual(INTERNAL.parameters, []) |
| 44 |
|
| 45 |
@unittest.mock.patch( |
| 46 |
'sys.argv', |
| 47 |
['foomuuri', '--verbose', 'command', 'arg1', '--force', 'arg2'], |
| 48 |
) |
| 49 |
def test_command_and_options(self, INTERNAL, CONFIG, CONFIG_OVERRIDE): |
| 50 |
"""Test command with arguments, mixed with options.""" |
| 51 |
parse_command_line() |
| 52 |
self.assertEqual(INTERNAL.command, 'command') |
| 53 |
self.assertEqual(INTERNAL.parameters, ['arg1', 'arg2']) |
| 54 |
self.assertEqual(INTERNAL.force, 1) |
| 55 |
self.assertEqual(CONFIG.verbose, 1) |
| 56 |
self.assertEqual(CONFIG_OVERRIDE['verbose'], 1) |
| 57 |
|
| 58 |
@unittest.mock.patch('sys.argv', ['foomuuri', '--syslog', '--fork']) |
| 59 |
def test_options_syslog_fork(self, INTERNAL, CONFIG, CONFIG_OVERRIDE): |
| 60 |
"""Test --syslog and --fork options.""" |
| 61 |
parse_command_line() |
| 62 |
self.assertEqual(CONFIG.syslog, 1) |
| 63 |
self.assertEqual(CONFIG.fork, 1) |
| 64 |
self.assertEqual(CONFIG_OVERRIDE['syslog'], 1) |
| 65 |
self.assertEqual(CONFIG_OVERRIDE['fork'], 1) |
| 66 |
self.assertEqual(INTERNAL.command, '') |
| 67 |
self.assertEqual(INTERNAL.parameters, []) |
| 68 |
|
| 69 |
@unittest.mock.patch('sys.argv', ['foomuuri', '--verbose']) |
| 70 |
def test_option_verbose(self, INTERNAL, CONFIG, CONFIG_OVERRIDE): |
| 71 |
"""Test --verbose option.""" |
| 72 |
parse_command_line() |
| 73 |
self.assertEqual(CONFIG.verbose, 1) |
| 74 |
self.assertEqual(CONFIG_OVERRIDE['verbose'], 1) |
| 75 |
self.assertEqual(INTERNAL.command, '') |
| 76 |
self.assertEqual(INTERNAL.parameters, []) |
| 77 |
|
| 78 |
@unittest.mock.patch('sys.argv', ['foomuuri', '--verbose', '--verbose']) |
| 79 |
def test_options_verbose_verbose(self, INTERNAL, CONFIG, CONFIG_OVERRIDE): |
| 80 |
"""Test --verbose --verbose options.""" |
| 81 |
parse_command_line() |
| 82 |
self.assertEqual(CONFIG.verbose, 2) |
| 83 |
self.assertEqual(CONFIG_OVERRIDE['verbose'], 2) |
| 84 |
self.assertEqual(INTERNAL.command, '') |
| 85 |
self.assertEqual(INTERNAL.parameters, []) |
| 86 |
|
| 87 |
@unittest.mock.patch('sys.argv', ['foomuuri', '--quiet']) |
| 88 |
def test_option_quiet(self, INTERNAL, CONFIG, CONFIG_OVERRIDE): |
| 89 |
"""Test --quiet option.""" |
| 90 |
parse_command_line() |
| 91 |
self.assertEqual(CONFIG.verbose, -1) |
| 92 |
self.assertEqual(CONFIG_OVERRIDE['verbose'], -1) |
| 93 |
self.assertEqual(INTERNAL.command, '') |
| 94 |
self.assertEqual(INTERNAL.parameters, []) |
| 95 |
|
| 96 |
@unittest.mock.patch('sys.argv', ['foomuuri', '--verbose', '--quiet']) |
| 97 |
def test_options_verbose_quiet(self, INTERNAL, CONFIG, CONFIG_OVERRIDE): |
| 98 |
"""Test --verbose --quiet options.""" |
| 99 |
parse_command_line() |
| 100 |
self.assertEqual(CONFIG.verbose, 0) |
| 101 |
self.assertEqual(CONFIG_OVERRIDE['verbose'], 0) |
| 102 |
self.assertEqual(INTERNAL.command, '') |
| 103 |
self.assertEqual(INTERNAL.parameters, []) |
| 104 |
|
| 105 |
@unittest.mock.patch('sys.argv', ['foomuuri', '--force']) |
| 106 |
def test_option_force(self, INTERNAL, *_): |
| 107 |
"""Test --force option.""" |
| 108 |
parse_command_line() |
| 109 |
self.assertEqual(INTERNAL.force, 1) |
| 110 |
self.assertEqual(INTERNAL.command, '') |
| 111 |
self.assertEqual(INTERNAL.parameters, []) |
| 112 |
|
| 113 |
@unittest.mock.patch('sys.argv', ['foomuuri', '--soft']) |
| 114 |
def test_option_soft(self, INTERNAL, *_): |
| 115 |
"""Test --soft option.""" |
| 116 |
parse_command_line() |
| 117 |
self.assertEqual(INTERNAL.force, -1) |
| 118 |
self.assertEqual(INTERNAL.command, '') |
| 119 |
self.assertEqual(INTERNAL.parameters, []) |
| 120 |
|
| 121 |
@unittest.mock.patch('sys.argv', ['foomuuri', '--set']) |
| 122 |
@unittest.mock.patch('foomuuri.fail', side_effect=SystemExit) |
| 123 |
def test_option_set_unknown(self, fail, *_): |
| 124 |
"""Test --set option without suffix.""" |
| 125 |
self.assertRaises(SystemExit, parse_command_line) |
| 126 |
fail.assert_called_with('Unknown option: --set') |
| 127 |
|
| 128 |
@unittest.mock.patch('sys.argv', ['foomuuri', '--set=']) |
| 129 |
@unittest.mock.patch('foomuuri.fail', side_effect=SystemExit) |
| 130 |
def test_option_set_invalid_syntax(self, fail, *_): |
| 131 |
"""Test incomplete --set option syntax.""" |
| 132 |
self.assertRaises(SystemExit, parse_command_line) |
| 133 |
fail.assert_called_with( |
| 134 |
'Invalid syntax for --set=OPTION=VALUE: --set=' |
| 135 |
) |
| 136 |
|
| 137 |
@unittest.mock.patch('sys.argv', ['foomuuri', '--set=unknown=value']) |
| 138 |
@unittest.mock.patch('foomuuri.fail', side_effect=SystemExit) |
| 139 |
def test_option_set_unknown_option(self, fail, *_): |
| 140 |
"""Test --set option with unknown foomuuri{} CONFIG option.""" |
| 141 |
self.assertRaises(SystemExit, parse_command_line) |
| 142 |
fail.assert_called_with('Unknown foomuuri{} option: unknown') |
| 143 |
|
| 144 |
@unittest.mock.patch('sys.argv', ['foomuuri', '--set=priority_offset=yes']) |
| 145 |
@unittest.mock.patch('foomuuri.fail', side_effect=SystemExit) |
| 146 |
def test_option_set_invalid_value(self, fail, *_): |
| 147 |
"""Test --set option with invalid value.""" |
| 148 |
self.assertRaises(SystemExit, parse_command_line) |
| 149 |
fail.assert_called_with( |
| 150 |
'Invalid foomuuri{} option priority_offset value: yes' |
| 151 |
) |
| 152 |
|
| 153 |
@unittest.mock.patch( |
| 154 |
'sys.argv', |
| 155 |
['foomuuri', '--set=priority_offset=42', '--set=set_size=1'], |
| 156 |
) |
| 157 |
def test_options_set_valid(self, INTERNAL, CONFIG, _): |
| 158 |
"""Test --set options with valid foomuuri{} CONFIG option/value.""" |
| 159 |
parse_command_line() |
| 160 |
self.assertEqual(CONFIG.priority_offset, 42) |
| 161 |
self.assertEqual(CONFIG.set_size, 1) |
| 162 |
self.assertEqual(INTERNAL.command, '') |
| 163 |
self.assertEqual(INTERNAL.parameters, []) |
| 164 |
|
| 165 |
@unittest.mock.patch('sys.argv', ['foomuuri', '--unknown']) |
| 166 |
@unittest.mock.patch('foomuuri.fail', side_effect=SystemExit) |
| 167 |
def test_unknown_option(self, fail, *_): |
| 168 |
"""Test unknown option.""" |
| 169 |
self.assertRaises(SystemExit, parse_command_line) |
| 170 |
fail.assert_called_with('Unknown option: --unknown') |
| 171 |
|
| 172 |
@unittest.mock.patch('sys.argv', ['foomuuri', 'command', '-h', '-v']) |
| 173 |
def test_option_short(self, INTERNAL, *_): |
| 174 |
"""Test short options. They are parsed as parameters.""" |
| 175 |
parse_command_line() |
| 176 |
self.assertEqual(INTERNAL.command, 'command') |
| 177 |
self.assertEqual(INTERNAL.parameters, ['-h', '-v']) |
| 178 |
|
| 179 |
@unittest.mock.patch('sys.argv', ['foomuuri', 'command', 'a', '-', 'b']) |
| 180 |
def test_option_hyphen(self, INTERNAL, *_): |
| 181 |
"""Test hyphen. It is parsed as parameter.""" |
| 182 |
parse_command_line() |
| 183 |
self.assertEqual(INTERNAL.command, 'command') |
| 184 |
self.assertEqual(INTERNAL.parameters, ['a', '-', 'b']) |