Пример #1
0
def test_to_argparser_bad_argsdict():
    """
    Should raise an error if the choices aren't coercible to the given type.
    """
    argsdict = {'badchoices': {'choices': 'a, b', 'type': 'int'}}
    with pytest.raises(ValueError):
        export.to_argparser("...", argsdict)
Пример #2
0
def test_to_argparser_bad_argsdict():
    """
    Should raise an error if the choices aren't coercible to the given type.
    """
    argsdict = {'badchoices': {
        'choices': 'a, b',
        'type': 'int'}}
    with pytest.raises(ValueError):
        export.to_argparser("...", argsdict)
Пример #3
0
def test_filetypes():
    argsdict = {'arg1': {'type': FILE_W}, 'arg2': {'type': FILE_R}}
    parser = export.to_argparser('test', argsdict)
    args = parser.parse_args(
        shlex.split("--arg1 /dev/null --arg2 {}".format(__file__)))
    assert args.arg1.mode == 'w'
    assert args.arg2.mode == 'r'
Пример #4
0
def test_to_argparser_bad_input(argsdict):
    parser = export.to_argparser(cmd_name='Command', argsdict=argsdict)
    # Invalid choice
    with pytest.raises(SystemExit):
        parser.parse_args(shlex.split("--choices 4 blah"))
    # Invalid type
    with pytest.raises(SystemExit):
        parser.parse_args(shlex.split("--arg2 string blah"))
Пример #5
0
def test_nargs():
    """`nargs` should work as expected by ArgumentParser."""
    argsdict = {'nargs': {
        'type': 'int', 
        'nargs': '+'}}
    parser = export.to_argparser('...', argsdict)
    args = parser.parse_args(shlex.split("--nargs 1 2 3"))
    assert args.nargs == [1, 2, 3]
Пример #6
0
def test_to_argparser_bad_input(argsdict):
    parser = export.to_argparser(cmd_name='Command', argsdict=argsdict)
    # Invalid choice
    with pytest.raises(SystemExit):
        parser.parse_args(shlex.split("--choices 4 blah"))
    # Invalid type
    with pytest.raises(SystemExit):
        parser.parse_args(shlex.split("--arg2 string blah"))
Пример #7
0
def test_bad_nargs():
    """
    `nargs` that don't match an int or one of the given options should be set 
    to None.
    """
    argsdict = {'nargs': {'nargs': 'b'}}
    parser = export.to_argparser('...', argsdict)
    with pytest.raises(SystemExit):
        parser.parse_args("--nargs 1 2 3")
Пример #8
0
def test_stdin_stdout():
    argsdict = {
        'arg1': {'default': 'stdin'},
        'arg2': {'default': 'stdout'}
    }
    parser = export.to_argparser('test', argsdict)
    args = parser.parse_args("")
    assert args.arg1 == sys.stdin
    assert args.arg2 == sys.stdout
Пример #9
0
def test_bad_nargs():
    """
    `nargs` that don't match an int or one of the given options should be set 
    to None.
    """
    argsdict = {'nargs': {
        'nargs': 'b'}}
    parser = export.to_argparser('...', argsdict)
    with pytest.raises(SystemExit):
        parser.parse_args("--nargs 1 2 3")
Пример #10
0
def test_filetypes():
    argsdict = {
        'arg1': {'type': FILE_W},
        'arg2': {'type': FILE_R}
    }
    parser = export.to_argparser('test', argsdict)
    args = parser.parse_args(
        shlex.split("--arg1 /dev/null --arg2 {}".format(__file__))
    )
    assert args.arg1.mode == 'w'
    assert args.arg2.mode == 'r'
Пример #11
0
def test_set_parser_defaults(argsdict, tmpdir):
    # Build a config parser
    config = ConfigParser.SafeConfigParser()
    cfg_file = tmpdir.join("test.cfg")
    cfg_file.write(export.to_config('test', argsdict))
    config.read(str(cfg_file))

    # Modify config from the spec defaults
    config.set("test", "arg2", "3")

    # Build argparser
    parser = export.to_argparser('test', argsdict)
    parser = argutils.set_parser_defaults(parser, config)
    args = parser.parse_args(["test"])
    # Should reflect the modified default
    assert args.arg2 == 3

    # Should warn since it can't find the section
    parser = export.to_argparser('test2', argsdict)
    with pytest.warns(UserWarning):
        argutils.set_parser_defaults(parser, config)
Пример #12
0
def test_set_parser_defaults(argsdict, tmpdir):
    # Build a config parser
    config = ConfigParser.SafeConfigParser()
    cfg_file = tmpdir.join("test.cfg")
    cfg_file.write(export.to_config('test', argsdict))
    config.read(str(cfg_file))

    # Modify config from the spec defaults
    config.set("test", "arg2", "3")

    # Build argparser
    parser = export.to_argparser('test', argsdict)
    parser = argutils.set_parser_defaults(parser, config)
    args = parser.parse_args(["test"])
    # Should reflect the modified default
    assert args.arg2 == 3

    # Should warn since it can't find the section
    parser = export.to_argparser('test2', argsdict)
    with pytest.warns(UserWarning):
        argutils.set_parser_defaults(parser, config)
Пример #13
0
def test_to_argparser(argsdict):
    """Valid argdict should behave as expected."""
    parser = export.to_argparser(cmd_name='Command', argsdict=argsdict)
    # Valid, expected input
    validargs = "--arg1 someval --arg2 5 --hidden 6 --flag --choices 2 /dev/null"
    args = parser.parse_args(shlex.split(validargs))
    assert parser.prog == "Command"
    assert args.arg1 == "someval"
    assert args.arg2 == 5
    assert args.hidden == 6
    assert args.flag
    assert args.choices == 2
    assert args.output.name == "/dev/null"
Пример #14
0
def test_to_argparser(argsdict):
    """Valid argdict should behave as expected."""
    parser = export.to_argparser(cmd_name='Command', argsdict=argsdict)
    # Valid, expected input
    validargs = "--arg1 someval --arg2 5 --hidden 6 --flag --choices 2 /dev/null"
    args = parser.parse_args(shlex.split(validargs))
    assert parser.prog == "Command"
    assert args.arg1 == "someval"
    assert args.arg2 == 5
    assert args.hidden == 6
    assert args.flag
    assert args.choices == 2
    assert args.output.name == "/dev/null"
Пример #15
0
def main():
    # Used in the config file and argument parser's help
    prog_name = 'example.py'

    config = ConfigParser.SafeConfigParser()

    # Read the spec and build a parser from it
    argsdict = read.from_yaml(open(SPEC_FILE).read())
    parser = export.to_argparser(prog_name, argsdict)

    # If the config file exists and we can read it, use it to set the
    # defaults
    if config.read(CONF_FILE):
        parser = argutils.set_parser_defaults(parser, config)

    args = parser.parse_args()

    if args.init:
        export.to_config_file(prog_name, argsdict, CONF_FILE)

    for _ in range(args.times):
        args.output.write(args.message + '\n')
Пример #16
0
def main():
    # Used in the config file and argument parser's help
    prog_name = "example.py"

    config = ConfigParser.SafeConfigParser()

    # Read the spec and build a parser from it
    argsdict = read.from_yaml(open(SPEC_FILE).read())
    parser = export.to_argparser(prog_name, argsdict)

    # If the config file exists and we can read it, use it to set the
    # defaults
    if config.read(CONF_FILE):
        parser = argutils.set_parser_defaults(parser, config)

    args = parser.parse_args()

    if args.init:
        export.to_config_file(prog_name, argsdict, CONF_FILE)

    for _ in range(args.times):
        args.output.write(args.message + "\n")
Пример #17
0
def test_bad_typestr():
    argsdict = {
        'arg1': {'type': 'notatype'}
    }
    with pytest.raises(ValueError):
        export.to_argparser('test', argsdict)
Пример #18
0
def test_bad_typestr():
    argsdict = {'arg1': {'type': 'notatype'}}
    with pytest.raises(ValueError):
        export.to_argparser('test', argsdict)
Пример #19
0
def test_stdin_stdout():
    argsdict = {'arg1': {'default': 'stdin'}, 'arg2': {'default': 'stdout'}}
    parser = export.to_argparser('test', argsdict)
    args = parser.parse_args("")
    assert args.arg1 == sys.stdin
    assert args.arg2 == sys.stdout
Пример #20
0
def test_nargs():
    """`nargs` should work as expected by ArgumentParser."""
    argsdict = {'nargs': {'type': 'int', 'nargs': '+'}}
    parser = export.to_argparser('...', argsdict)
    args = parser.parse_args(shlex.split("--nargs 1 2 3"))
    assert args.nargs == [1, 2, 3]