def test_flag_char(): with pytest.raises(ValueError, match='char flags must be exactly one character'): Flag('flag', char='FLAG') with pytest.raises( ValueError, match='expected valid flag character.*ASCII letters, numbers.*'): Flag('flag', char=u'é') assert Flag('flag', char='-f').char == 'f'
def test_mw_unres(): def unres_cmd(unresolved_arg): return unresolved_arg cmd = Command(unres_cmd) assert cmd.func is unres_cmd with pytest.raises( NameError, match= "unresolved middleware or handler arguments: .*unresolved_arg.*"): cmd.run(['unres_cmd']) def inner_mw(next_, arg): return next_() @face_middleware(provides='arg', flags=[Flag('--verbose', parse_as=True)]) def outer_mw(next_): return next_(arg=1) def ok_cmd(arg): return None cmd = Command(ok_cmd, middlewares=[outer_mw]) cmd.add_middleware(inner_mw) with pytest.raises( NameError, match= "unresolved middleware or handler arguments: .*arg.* check middleware order." ): cmd.run(['ok_cmd']) return
def test_flag_init(): cmd = Command(lambda flag, part: None, name='cmd') with pytest.raises(ValueError, match='cannot make an argument-less flag required'): cmd.add('--flag', missing=ERROR, parse_as=True) # test custom callable multi cmd.add(Flag('--part', multi=lambda flag, vals: ''.join(vals))) res = cmd.parse(['cmd', '--part', 'a', '--part', 'b']) assert res.flags['part'] == 'ab' with pytest.raises(ValueError, match='multi expected callable, bool, or one of.*'): cmd.add('--badflag', multi='nope')
def test_flag_name(): flag = Flag('ok_name') assert repr(flag).startswith('<Flag name=') assert format_flag_label(flag) == '--ok-name OK_NAME' assert format_flag_label(Flag('name', display={'label': '--nAmE'})) == '--nAmE' assert format_flag_label(Flag('name', display='--nAmE')) == '--nAmE' assert Flag('name', display='').display.hidden == True assert repr(Flag('name', display='').display).startswith('<FlagDisplay') with pytest.raises(TypeError, match='or FlagDisplay instance'): Flag('name', display=object()) with pytest.raises(TypeError, match='unexpected keyword arguments'): Flag('name', display={'badkw': 'val'}) with pytest.raises(ValueError, match='expected identifier.*'): assert identifier_to_flag('--flag') name_err_map = { '': 'non-zero length string', 5: 'non-zero length string', 'name_': 'without trailing dashes or underscores', 'name--': 'without trailing dashes or underscores', 'n?me': ('must begin with a letter.*and' ' consist only of letters, digits, underscores, and' ' dashes'), 'for': 'valid flag names must not be Python keywords' } for name, err in name_err_map.items(): with pytest.raises(ValueError, match=err): Flag(name=name) return
def test_flag_post_doc(): assert format_flag_post_doc(Flag('flag')) == '' assert format_flag_post_doc(Flag('flag', missing=42)) == '(defaults to 42)' assert format_flag_post_doc(Flag('flag', missing=ERROR)) == '(required)' assert format_flag_post_doc(Flag('flag', display={'post_doc': '(fun)'})) == '(fun)'