Example #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)
Example #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)
Example #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'
Example #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"))
Example #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]
Example #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"))
Example #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")
Example #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
Example #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")
Example #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'
Example #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)
Example #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)
Example #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"
Example #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"
Example #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')
Example #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")
Example #17
0
def test_bad_typestr():
    argsdict = {
        'arg1': {'type': 'notatype'}
    }
    with pytest.raises(ValueError):
        export.to_argparser('test', argsdict)
Example #18
0
def test_bad_typestr():
    argsdict = {'arg1': {'type': 'notatype'}}
    with pytest.raises(ValueError):
        export.to_argparser('test', argsdict)
Example #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
Example #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]