| 1 |
import os |
| 2 |
import pickle |
| 3 |
|
| 4 |
import pytest |
| 5 |
|
| 6 |
from doit.cmdparse import DefaultUpdate, CmdParseError, CmdOption, CmdParse |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
class TestDefaultUpdate(object): |
| 11 |
def test(self): |
| 12 |
du = DefaultUpdate() |
| 13 |
|
| 14 |
du.set_default('a', 0) |
| 15 |
du.set_default('b', 0) |
| 16 |
|
| 17 |
assert 0 == du['a'] |
| 18 |
assert 0 == du['b'] |
| 19 |
|
| 20 |
|
| 21 |
du['b'] = 1 |
| 22 |
|
| 23 |
du.update_defaults({'a':2, 'b':2}) |
| 24 |
assert 2 == du['a'] |
| 25 |
assert 1 == du['b'] |
| 26 |
|
| 27 |
|
| 28 |
du.update_defaults({'a':3}) |
| 29 |
assert 3 == du['a'] |
| 30 |
|
| 31 |
|
| 32 |
def test_add_defaults(self): |
| 33 |
du = DefaultUpdate() |
| 34 |
du.add_defaults({'a': 0, 'b':1}) |
| 35 |
du['c'] = 5 |
| 36 |
du.add_defaults({'a':2, 'c':2}) |
| 37 |
assert 0 == du['a'] |
| 38 |
assert 1 == du['b'] |
| 39 |
assert 5 == du['c'] |
| 40 |
|
| 41 |
|
| 42 |
def test_pickle(self): |
| 43 |
du = DefaultUpdate() |
| 44 |
du.set_default('x', 0) |
| 45 |
dump = pickle.dumps(du,2) |
| 46 |
pickle.loads(dump) |
| 47 |
|
| 48 |
|
| 49 |
class TestCmdOption(object): |
| 50 |
|
| 51 |
def test_repr(self): |
| 52 |
opt = CmdOption({'name':'opt1', 'default':'', |
| 53 |
'short':'o', 'long':'other'}) |
| 54 |
assert "CmdOption(" in repr(opt) |
| 55 |
assert "'name':'opt1'" in repr(opt) |
| 56 |
assert "'short':'o'" in repr(opt) |
| 57 |
assert "'long':'other'" in repr(opt) |
| 58 |
|
| 59 |
def test_non_required_fields(self): |
| 60 |
opt1 = CmdOption({'name':'op1', 'default':''}) |
| 61 |
assert '' == opt1.long |
| 62 |
|
| 63 |
def test_invalid_field(self): |
| 64 |
opt_dict = {'name':'op1', 'default':'', 'non_existent':''} |
| 65 |
pytest.raises(CmdParseError, CmdOption, opt_dict) |
| 66 |
|
| 67 |
def test_missing_field(self): |
| 68 |
opt_dict = {'name':'op1', 'long':'abc'} |
| 69 |
pytest.raises(CmdParseError, CmdOption, opt_dict) |
| 70 |
|
| 71 |
|
| 72 |
class TestCmdOption_str2val(object): |
| 73 |
def test_str2boolean(self): |
| 74 |
opt = CmdOption({'name':'op1', 'default':'', 'type':bool, |
| 75 |
'short':'b', 'long': 'bobo'}) |
| 76 |
assert True == opt.str2boolean('1') |
| 77 |
assert True == opt.str2boolean('yes') |
| 78 |
assert True == opt.str2boolean('Yes') |
| 79 |
assert True == opt.str2boolean('YES') |
| 80 |
assert True == opt.str2boolean('true') |
| 81 |
assert True == opt.str2boolean('on') |
| 82 |
assert False == opt.str2boolean('0') |
| 83 |
assert False == opt.str2boolean('false') |
| 84 |
assert False == opt.str2boolean('no') |
| 85 |
assert False == opt.str2boolean('off') |
| 86 |
assert False == opt.str2boolean('OFF') |
| 87 |
pytest.raises(ValueError, opt.str2boolean, '2') |
| 88 |
pytest.raises(ValueError, opt.str2boolean, None) |
| 89 |
pytest.raises(ValueError, opt.str2boolean, 'other') |
| 90 |
|
| 91 |
|
| 92 |
def test_non_string_values_are_not_converted(self): |
| 93 |
opt = CmdOption({'name':'op1', 'default':'', 'type':bool}) |
| 94 |
assert False == opt.str2type(False) |
| 95 |
assert True == opt.str2type(True) |
| 96 |
assert None == opt.str2type(None) |
| 97 |
|
| 98 |
def test_str(self): |
| 99 |
opt = CmdOption({'name':'op1', 'default':'', 'type':str}) |
| 100 |
assert 'foo' == opt.str2type('foo') |
| 101 |
assert 'bar' == opt.str2type('bar') |
| 102 |
|
| 103 |
def test_bool(self): |
| 104 |
opt = CmdOption({'name':'op1', 'default':'', 'type':bool}) |
| 105 |
assert False == opt.str2type('off') |
| 106 |
assert True == opt.str2type('on') |
| 107 |
|
| 108 |
def test_int(self): |
| 109 |
opt = CmdOption({'name':'op1', 'default':'', 'type':int}) |
| 110 |
assert 2 == opt.str2type('2') |
| 111 |
assert -3 == opt.str2type('-3') |
| 112 |
|
| 113 |
def test_list(self): |
| 114 |
opt = CmdOption({'name':'op1', 'default':'', 'type':list}) |
| 115 |
assert ['foo'] == opt.str2type('foo') |
| 116 |
assert [] == opt.str2type('') |
| 117 |
assert ['foo', 'bar'] == opt.str2type('foo , bar ') |
| 118 |
|
| 119 |
def test_invalid_value(self): |
| 120 |
opt = CmdOption({'name':'op1', 'default':'', 'type':int}) |
| 121 |
pytest.raises(CmdParseError, opt.str2type, 'not a number') |
| 122 |
|
| 123 |
|
| 124 |
class TestCmdOption_help_param(object): |
| 125 |
def test_bool_param(self): |
| 126 |
opt1 = CmdOption({'name':'op1', 'default':'', 'type':bool, |
| 127 |
'short':'b', 'long': 'bobo'}) |
| 128 |
assert '-b, --bobo' == opt1.help_param() |
| 129 |
|
| 130 |
def test_non_bool_param(self): |
| 131 |
opt1 = CmdOption({'name':'op1', 'default':'', 'type':str, |
| 132 |
'short':'s', 'long': 'susu'}) |
| 133 |
assert '-s ARG, --susu=ARG' == opt1.help_param() |
| 134 |
|
| 135 |
|
| 136 |
def test_metavar(self): |
| 137 |
opt1 = CmdOption({'name':'op1', 'default':'', 'type':str, 'metavar':'VAL', |
| 138 |
'short':'s', 'long': 'susu'}) |
| 139 |
assert '-s VAL, --susu=VAL' == opt1.help_param() |
| 140 |
|
| 141 |
|
| 142 |
def test_no_long(self): |
| 143 |
opt1 = CmdOption({'name':'op1', 'default':'', 'type':str, |
| 144 |
'short':'s'}) |
| 145 |
assert '-s ARG' == opt1.help_param() |
| 146 |
|
| 147 |
|
| 148 |
opt_bool = {'name': 'flag', |
| 149 |
'short':'f', |
| 150 |
'long': 'flag', |
| 151 |
'inverse':'no-flag', |
| 152 |
'type': bool, |
| 153 |
'default': False, |
| 154 |
'help': 'help for opt1'} |
| 155 |
|
| 156 |
opt_rare = {'name': 'rare_bool', |
| 157 |
'long': 'rare-bool', |
| 158 |
'env_var': 'RARE', |
| 159 |
'type': bool, |
| 160 |
'default': False, |
| 161 |
'help': 'help for opt2',} |
| 162 |
|
| 163 |
opt_int = {'name': 'num', |
| 164 |
'short':'n', |
| 165 |
'long': 'number', |
| 166 |
'type': int, |
| 167 |
'default': 5, |
| 168 |
'help': 'help for opt3'} |
| 169 |
|
| 170 |
opt_no = {'name': 'no', |
| 171 |
'short':'', |
| 172 |
'long': '', |
| 173 |
'type': int, |
| 174 |
'default': 5, |
| 175 |
'help': 'user cant modify me'} |
| 176 |
|
| 177 |
opt_append = { 'name': 'list', |
| 178 |
'short': 'l', |
| 179 |
'long': 'list', |
| 180 |
'type': list, |
| 181 |
'default': [], |
| 182 |
'help': 'use many -l to make a list'} |
| 183 |
|
| 184 |
opt_choices_desc = {'name': 'choices', |
| 185 |
'short':'c', |
| 186 |
'long': 'choice', |
| 187 |
'type': str, |
| 188 |
'choices': (("yes", "signify affirmative"), |
| 189 |
("no","signify negative")), |
| 190 |
'default': "yes", |
| 191 |
'help': 'User chooses [default %(default)s]'} |
| 192 |
|
| 193 |
opt_choices_nodesc = {'name': 'choicesnodesc', |
| 194 |
'short':'C', |
| 195 |
'long': 'achoice', |
| 196 |
'type': str, |
| 197 |
'choices': (("yes", ""), |
| 198 |
("no", "")), |
| 199 |
'default': "no", |
| 200 |
'help': 'User chooses [default %(default)s]'} |
| 201 |
|
| 202 |
|
| 203 |
class TestCmdOption_help_doc(object): |
| 204 |
def test_param(self): |
| 205 |
opt1 = CmdOption(opt_bool) |
| 206 |
got = opt1.help_doc() |
| 207 |
assert '-f, --flag' in got[0] |
| 208 |
assert 'help for opt1' in got[0] |
| 209 |
assert '--no-flag' in got[1] |
| 210 |
assert 2 == len(got) |
| 211 |
|
| 212 |
def test_no_doc_param(self): |
| 213 |
opt1 = CmdOption(opt_no) |
| 214 |
assert 0 == len(opt1.help_doc()) |
| 215 |
|
| 216 |
def test_choices_desc_doc(self): |
| 217 |
the_opt = CmdOption(opt_choices_desc) |
| 218 |
doc = the_opt.help_doc()[0] |
| 219 |
assert 'choices:\n' in doc |
| 220 |
assert 'yes: signify affirmative' in doc |
| 221 |
assert 'no: signify negative' in doc |
| 222 |
|
| 223 |
def test_choices_nodesc_doc(self): |
| 224 |
the_opt = CmdOption(opt_choices_nodesc) |
| 225 |
doc = the_opt.help_doc()[0] |
| 226 |
assert "choices: no, yes" in doc |
| 227 |
|
| 228 |
def test_name_config_env(self): |
| 229 |
opt1 = CmdOption(opt_rare) |
| 230 |
got = opt1.help_doc() |
| 231 |
assert 'config: rare_bool' in got[0] |
| 232 |
assert 'environ: RARE' in got[0] |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
class TestCommand(object): |
| 237 |
|
| 238 |
@pytest.fixture |
| 239 |
def cmd(self, request): |
| 240 |
opt_list = (opt_bool, opt_rare, opt_int, opt_no, |
| 241 |
opt_append, opt_choices_desc, opt_choices_nodesc) |
| 242 |
options = [CmdOption(o) for o in opt_list] |
| 243 |
cmd = CmdParse(options) |
| 244 |
return cmd |
| 245 |
|
| 246 |
def test_contains(self, cmd): |
| 247 |
assert 'flag' in cmd |
| 248 |
assert 'num' in cmd |
| 249 |
assert 'xxx' not in cmd |
| 250 |
|
| 251 |
def test_getitem(self, cmd): |
| 252 |
assert cmd['flag'].short == 'f' |
| 253 |
assert cmd['num'].default == 5 |
| 254 |
|
| 255 |
def test_option_list(self, cmd): |
| 256 |
opt_names = [o.name for o in cmd.options] |
| 257 |
assert ['flag', 'rare_bool', 'num', 'no', 'list', 'choices', |
| 258 |
'choicesnodesc']== opt_names |
| 259 |
|
| 260 |
def test_short(self, cmd): |
| 261 |
assert "fn:l:c:C:" == cmd.get_short(), cmd.get_short() |
| 262 |
|
| 263 |
def test_long(self, cmd): |
| 264 |
longs = ["flag", "no-flag", "rare-bool", "number=", |
| 265 |
"list=", "choice=", "achoice="] |
| 266 |
assert longs == cmd.get_long() |
| 267 |
|
| 268 |
def test_getOption(self, cmd): |
| 269 |
|
| 270 |
opt, is_inverse = cmd.get_option('-f') |
| 271 |
assert (opt_bool['name'], False) == (opt.name, is_inverse) |
| 272 |
|
| 273 |
opt, is_inverse = cmd.get_option('--rare-bool') |
| 274 |
assert (opt_rare['name'], False) == (opt.name, is_inverse) |
| 275 |
|
| 276 |
opt, is_inverse = cmd.get_option('--no-flag') |
| 277 |
assert (opt_bool['name'], True) == (opt.name, is_inverse) |
| 278 |
|
| 279 |
opt, is_inverse = cmd.get_option('not-there') |
| 280 |
assert (None, None) == (opt, is_inverse) |
| 281 |
|
| 282 |
opt, is_inverse = cmd.get_option('--list') |
| 283 |
assert (opt_append['name'], False) == (opt.name, is_inverse) |
| 284 |
|
| 285 |
opt, is_inverse = cmd.get_option('--choice') |
| 286 |
assert (opt_choices_desc['name'], False) == (opt.name, is_inverse) |
| 287 |
|
| 288 |
opt, is_inverse = cmd.get_option('--achoice') |
| 289 |
assert (opt_choices_nodesc['name'], False) == (opt.name, is_inverse) |
| 290 |
|
| 291 |
|
| 292 |
def test_parseDefaults(self, cmd): |
| 293 |
params, args = cmd.parse([]) |
| 294 |
assert False == params['flag'] |
| 295 |
assert 5 == params['num'] |
| 296 |
assert [] == params['list'] |
| 297 |
assert "yes" == params['choices'] |
| 298 |
assert "no" == params['choicesnodesc'] |
| 299 |
|
| 300 |
def test_overwrite_defaults(self, cmd): |
| 301 |
cmd.overwrite_defaults({'num': 9, 'i_dont_exist': 1}) |
| 302 |
params, args = cmd.parse([]) |
| 303 |
assert 9 == params['num'] |
| 304 |
|
| 305 |
def test_overwrite_defaults_convert_type(self, cmd): |
| 306 |
cmd.overwrite_defaults({'num': '9', 'list': 'foo, bar', 'flag':'on'}) |
| 307 |
params, args = cmd.parse([]) |
| 308 |
assert 9 == params['num'] |
| 309 |
assert ['foo', 'bar'] == params['list'] |
| 310 |
assert True == params['flag'] |
| 311 |
|
| 312 |
def test_parseShortValues(self, cmd): |
| 313 |
params, args = cmd.parse(['-n','89','-f', '-l', 'foo', '-l', 'bar', |
| 314 |
'-c', 'no', '-C', 'yes']) |
| 315 |
assert True == params['flag'] |
| 316 |
assert 89 == params['num'] |
| 317 |
assert ['foo', 'bar'] == params['list'] |
| 318 |
assert "no" == params['choices'] |
| 319 |
assert "yes" == params['choicesnodesc'] |
| 320 |
|
| 321 |
def test_parseLongValues(self, cmd): |
| 322 |
params, args = cmd.parse(['--rare-bool','--num','89', '--no-flag', |
| 323 |
'--list', 'flip', '--list', 'flop', |
| 324 |
'--choice', 'no', '--achoice', 'yes']) |
| 325 |
assert True == params['rare_bool'] |
| 326 |
assert False == params['flag'] |
| 327 |
assert 89 == params['num'] |
| 328 |
assert ['flip', 'flop'] == params['list'] |
| 329 |
assert "no" == params['choices'] |
| 330 |
assert "yes" == params['choicesnodesc'] |
| 331 |
|
| 332 |
def test_parsePositionalArgs(self, cmd): |
| 333 |
params, args = cmd.parse(['-f','p1','p2', '--sub-arg']) |
| 334 |
assert ['p1','p2', '--sub-arg'] == args |
| 335 |
|
| 336 |
def test_parseError(self, cmd): |
| 337 |
pytest.raises(CmdParseError, cmd.parse, ['--not-exist-param']) |
| 338 |
|
| 339 |
def test_parseWrongType(self, cmd): |
| 340 |
pytest.raises(CmdParseError, cmd.parse, ['--num','oi']) |
| 341 |
|
| 342 |
def test_parseWrongChoice(self, cmd): |
| 343 |
pytest.raises(CmdParseError, cmd.parse, ['--choice', 'maybe']) |
| 344 |
|
| 345 |
def test_env_val(self): |
| 346 |
opt_foo = { |
| 347 |
'name': 'foo', |
| 348 |
'long': 'foo', |
| 349 |
'type': str, |
| 350 |
'env_var': 'FOO', |
| 351 |
'default': 'zero' |
| 352 |
} |
| 353 |
cmd = CmdParse([CmdOption(opt_foo)]) |
| 354 |
|
| 355 |
|
| 356 |
params, args = cmd.parse([]) |
| 357 |
assert params['foo'] == 'zero' |
| 358 |
|
| 359 |
|
| 360 |
os.environ['FOO'] = 'bar' |
| 361 |
params2, args2 = cmd.parse([]) |
| 362 |
assert params2['foo'] == 'bar' |
| 363 |
|
| 364 |
|
| 365 |
params2, args2 = cmd.parse(['--foo', 'XXX']) |
| 366 |
assert params2['foo'] == 'XXX' |
| 367 |
|
| 368 |
|
| 369 |
def test_env_val_bool(self): |
| 370 |
opt_foo = { |
| 371 |
'name': 'foo', |
| 372 |
'long': 'foo', |
| 373 |
'type': bool, |
| 374 |
'env_var': 'FOO', |
| 375 |
'default': False, |
| 376 |
} |
| 377 |
cmd = CmdParse([CmdOption(opt_foo)]) |
| 378 |
|
| 379 |
|
| 380 |
os.environ['FOO'] = '1' |
| 381 |
params, args = cmd.parse([]) |
| 382 |
assert params['foo'] == True |
| 383 |
|
| 384 |
|
| 385 |
os.environ['FOO'] = '0' |
| 386 |
params, args = cmd.parse([]) |
| 387 |
assert params['foo'] == False |