コード例 #1
0
def test_subparser_config_suppress_on_missing_env(capsys):
    subcommand = subparser()

    @subcommand
    def hello(name):
        print('Hello %s!' % name)
    hello.add_argument('--name', env='ENV_NAME', config='name', default='John')

    @subcommand('speak')
    def blah(animal):
        if animal == 'dog':
            return 'bark'
        elif animal == 'cat':
            return 'meow'
        elif animal == 'pig':
            return 'oink'
        elif animal == 'duck':
            return 'quack'
        raise Exception('animal not found')
    blah.add_argument('--animal', env='ENV_ANIMAL', config='animal', default='dog')

    subcommand.add_config('-c', dest='config', env='ENV_CONFIG')
    os.environ['ENV_CONFIG'] = 'foo.json'

    subcommand.dispatch(['hello'])
    out, err = capsys.readouterr()
    assert out == 'Hello John!\n'
コード例 #2
0
def test_subparser_config_error_on_missing_env(capsys):
    subcommand = subparser()

    @subcommand
    def hello(name):
        print('Hello %s!' % name)
    hello.add_argument('--name', env='ENV_NAME', config='name', default='John')

    @subcommand('speak')
    def blah(animal):
        if animal == 'dog':
            return 'bark'
        elif animal == 'cat':
            return 'meow'
        elif animal == 'pig':
            return 'oink'
        elif animal == 'duck':
            return 'quack'
        raise Exception('animal not found')
    blah.add_argument('--animal', env='ENV_ANIMAL', config='animal', default='dog')

    subcommand.add_config('-c', dest='config', env='ENV_CONFIG', check_file_for='command env'.split())
    os.environ['ENV_CONFIG'] = 'foo.json'

    with pytest.raises(IOError):
        subcommand.dispatch(['hello'])
コード例 #3
0
def test_subparser_config_ini(capsys, inifile):
    subcommand = subparser()

    @subcommand
    def hello(name):
        print('Hello %s!' % name)
    hello.add_argument('--name', env='ENV_NAME', config=('hello', 'name'), default='John')

    @subcommand('speak')
    def blah(animal):
        if animal == 'dog':
            return 'bark'
        elif animal == 'cat':
            return 'meow'
        elif animal == 'pig':
            return 'oink'
        elif animal == 'duck':
            return 'quack'
        raise Exception('animal not found')
    blah.add_argument('--animal', env='ENV_ANIMAL', config=('speak', 'animal'), default='dog')

    subcommand.add_config('-c', dest='config', config_class=IniConfig)
    os.environ['ENV_NAME'] = 'Eric'
    os.environ['ENV_ANIMAL'] = 'cat'

    # command-line takes highest precedence
    subcommand.dispatch(['hello', '-c', inifile, '--name', 'Robert'])
    out, err = capsys.readouterr()
    assert out == 'Hello Robert!\n'
    assert subcommand.dispatch(['speak', '-c', inifile, '--animal', 'duck']) == 'quack'

    # then, environment variables
    subcommand.dispatch(['hello', '-c', inifile])
    out, err = capsys.readouterr()
    assert out == 'Hello Eric!\n'
    assert subcommand.dispatch(['speak', '-c', inifile]) == 'meow'

    # then, config file values
    del os.environ['ENV_NAME']
    del os.environ['ENV_ANIMAL']
    subcommand.dispatch(['hello', '-c', inifile])
    out, err = capsys.readouterr()
    assert out == 'Hello Joe!\n'
    assert subcommand.dispatch(['speak', '-c', inifile]) == 'oink'

    # finally, default values
    subcommand.dispatch(['hello'])
    out, err = capsys.readouterr()
    assert out == 'Hello John!\n'
    assert subcommand.dispatch(['speak']) == 'bark'

    # test that help contains config
    with pytest.raises(SystemExit):
        subcommand.dispatch(['-h'])
    out, err = capsys.readouterr()
    assert '-c CONFIG' in [l.strip() for l in out.splitlines()]
コード例 #4
0
def test_nested_json(capsys, jsonfile):
    subcommand = subparser()

    @subcommand
    def hello(name):
        print('Hello %s!' % name)
    hello.add_argument('--name', config=('nested', 'dict', 'test'), default='John')

    subcommand.add_config('-c', dest='config')
    subcommand.dispatch(['hello', '-c', jsonfile])
    out, err = capsys.readouterr()
    assert out == 'Hello value!\n'
コード例 #5
0
def test_argspec():
    '''
    this is more obscure.  basically, we're testing that we didn't change the
    argspec of the original function when we wrapped it.
    '''
    subcommand = subparser()
    @subcommand
    def bar(a, b):
        pass
    import inspect
    spec = inspect.getargspec(bar)
    assert spec.args == ['a', 'b']
コード例 #6
0
def test_subparser(capsys, jsonfile):
    subcommand = subparser()

    @subcommand
    def hello(name):
        print('Hello %s!' % name)
    hello.add_argument('--name', default='John')

    @subcommand('speak')
    def blah(animal):
        if animal == 'dog':
            return 'bark'
        elif animal == 'cat':
            return 'meow'
        elif animal == 'pig':
            return 'oink'
        elif animal == 'duck':
            return 'quack'
        raise Exception('animal not found')
    blah.add_argument('--animal', default='dog')

    # functions are manually callable
    assert blah('dog') == 'bark'

    subcommand.dispatch(['hello'])
    out, err = capsys.readouterr()
    assert out == 'Hello John!\n'
    assert subcommand.dispatch(['speak']) == 'bark'

    subcommand.dispatch(['hello', '--name', 'Joe'])
    out, err = capsys.readouterr()
    assert out == 'Hello Joe!\n'
    assert subcommand.dispatch(['speak', '--animal', 'pig']) == 'oink'

    # test that system exits without subcommand
    with pytest.raises(SystemExit):
        subcommand.dispatch([])
    out, err = capsys.readouterr()
    assert err.startswith('usage: py.test [-h] {hello,speak}')