mirror of
https://github.com/PyCQA/flake8.git
synced 2026-04-08 14:04:16 +00:00
move from optparse to argparse
This commit is contained in:
parent
03cb85f556
commit
b66ebd7034
30 changed files with 462 additions and 302 deletions
|
|
@ -1,12 +1,14 @@
|
|||
"""Unit tests for flake8.options.manager.Option."""
|
||||
import functools
|
||||
|
||||
import mock
|
||||
import pytest
|
||||
|
||||
from flake8.options import manager
|
||||
|
||||
|
||||
def test_to_optparse():
|
||||
"""Test conversion to an optparse.Option class."""
|
||||
def test_to_argparse():
|
||||
"""Test conversion to an argparse arguments."""
|
||||
opt = manager.Option(
|
||||
short_option_name='-t',
|
||||
long_option_name='--test',
|
||||
|
|
@ -17,45 +19,26 @@ def test_to_optparse():
|
|||
assert opt.normalize_paths is True
|
||||
assert opt.parse_from_config is True
|
||||
|
||||
optparse_opt = opt.to_optparse()
|
||||
assert not hasattr(optparse_opt, 'parse_from_config')
|
||||
assert not hasattr(optparse_opt, 'normalize_paths')
|
||||
assert optparse_opt.action == 'count'
|
||||
args, kwargs = opt.to_argparse()
|
||||
assert args == ['-t', '--test']
|
||||
assert kwargs == {'action': 'count', 'type': mock.ANY}
|
||||
assert isinstance(kwargs['type'], functools.partial)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('opttype,str_val,expected', [
|
||||
('float', '2', 2.0),
|
||||
('complex', '2', (2 + 0j)),
|
||||
])
|
||||
def test_to_support_optparses_standard_types(opttype, str_val, expected):
|
||||
"""Show that optparse converts float and complex types correctly."""
|
||||
opt = manager.Option('-t', '--test', type=opttype)
|
||||
assert opt.normalize_from_setuptools(str_val) == expected
|
||||
def test_to_optparse():
|
||||
"""Test that .to_optparse() produces a useful error message."""
|
||||
with pytest.raises(AttributeError) as excinfo:
|
||||
manager.Option('--foo').to_optparse()
|
||||
msg, = excinfo.value.args
|
||||
assert msg == 'to_optparse: flake8 now uses argparse'
|
||||
|
||||
|
||||
@mock.patch('optparse.Option')
|
||||
def test_to_optparse_creates_an_option_as_we_expect(Option): # noqa: N803
|
||||
"""Show that we pass all keyword args to optparse.Option."""
|
||||
def test_to_argparse_creates_an_option_as_we_expect():
|
||||
"""Show that we pass all keyword args to argparse."""
|
||||
opt = manager.Option('-t', '--test', action='count')
|
||||
opt.to_optparse()
|
||||
option_kwargs = {
|
||||
'action': 'count',
|
||||
'default': None,
|
||||
'type': None,
|
||||
'dest': 'test',
|
||||
'nargs': None,
|
||||
'const': None,
|
||||
'choices': None,
|
||||
'callback': None,
|
||||
'callback_args': None,
|
||||
'callback_kwargs': None,
|
||||
'help': None,
|
||||
'metavar': None,
|
||||
}
|
||||
|
||||
Option.assert_called_once_with(
|
||||
'-t', '--test', **option_kwargs
|
||||
)
|
||||
args, kwargs = opt.to_argparse()
|
||||
assert args == ['-t', '--test']
|
||||
assert kwargs == {'action': 'count'}
|
||||
|
||||
|
||||
def test_config_name_generation():
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue