Example #1
0
def test_multi(monkeypatch, tmpdir, multi):
    """Test with repeatable non-boolean options.

    :param monkeypatch: pytest fixture.
    :param tmpdir: pytest fixture.
    :param bool multi: Test with ... and without ... in docstring.
    """
    config_file = tmpdir.join('config.ini')
    config_file.write(
        dedent("""
    [my_script]
    key =
        a
        b
        c
    """))

    monkeypatch.setattr(
        'sys.argv', ['pytest', '1', '--flag', '--config',
                     str(config_file)])
    docstring = DOCSTRING_MULTI if multi else DOCSTRING_NOT_MULTI
    expected = EXPECTED_MULTI.copy() if multi else EXPECTED_NOT_MULTI.copy()
    expected['--config'] = str(config_file)
    expected['--flag'] = 1 if multi else True
    expected['--key'] = ['a', 'b', 'c'] if multi else '\na\nb\nc'

    actual = docoptcfg(docstring, config_option='--config')
    assert actual == expected
Example #2
0
def test_no_settable():
    """Test with all options overridden by command line."""
    actual = docoptcfg(DOCSTRING_MULTI, ['1', '--config=config.ini', '--flag', '--key=val'], env_prefix='MULTI_')
    expected = EXPECTED_MULTI.copy()
    expected['--config'] = 'config.ini'
    expected['--flag'] = 1
    expected['--key'] = ['val']
    assert actual == expected
Example #3
0
def test_multi(monkeypatch, multi, set_key, set_key0, set_key1):
    """Test with repeatable non-boolean options.

    :param monkeypatch: pytest fixture.
    :param bool multi: Test with ... and without ... in docstring.
    :param str set_key: Set MULTI_KEY to this value if not None.
    :param str set_key0: Set MULTI_KEY0 to this value if not None.
    :param str set_key1: Set MULTI_KEY1 to this value if not None.
    """
    monkeypatch.setattr('sys.argv', ['pytest', '1', '--flag'])
    docstring = DOCSTRING_MULTI if multi else DOCSTRING_NOT_MULTI
    expected = EXPECTED_MULTI.copy() if multi else EXPECTED_NOT_MULTI.copy()
    expected['--flag'] = 1 if multi else True

    # Set variables.
    if set_key is not None:
        monkeypatch.setenv('MULTI_KEY', set_key)
    if set_key0 is not None:
        monkeypatch.setenv('MULTI_KEY0', set_key0)
    if set_key1 is not None:
        monkeypatch.setenv('MULTI_KEY1', set_key1)

    # Test not multi.
    if not multi:
        if set_key is not None:
            expected['--key'] = str(set_key)  # Others are ignored.
        actual = docoptcfg(docstring, env_prefix='MULTI_')
        assert actual == expected
        return

    set_keys = (set_key is not None, set_key0 is not None, set_key1
                is not None)
    if set_keys == (True, True, True):
        expected['--key'] = [set_key, set_key0, set_key1]
    elif set_keys == (True, True, False):
        expected['--key'] = [set_key, set_key0]
    elif set_keys == (True, False, False):
        expected['--key'] = [set_key]

    elif set_keys == (False, False, False):
        expected['--key'] = []
    elif set_keys == (False, False, True):
        expected['--key'] = []
    elif set_keys == (False, True, True):
        expected['--key'] = [set_key0, set_key1]

    elif set_keys == (False, True, False):
        expected['--key'] = [set_key0]
    elif set_keys == (True, False, True):
        expected['--key'] = [set_key]

    else:
        raise NotImplementedError

    actual = docoptcfg(docstring, env_prefix='MULTI_')
    assert actual == expected
Example #4
0
def test_multi(monkeypatch, multi, set_key, set_key0, set_key1):
    """Test with repeatable non-boolean options.

    :param monkeypatch: pytest fixture.
    :param bool multi: Test with ... and without ... in docstring.
    :param str set_key: Set MULTI_KEY to this value if not None.
    :param str set_key0: Set MULTI_KEY0 to this value if not None.
    :param str set_key1: Set MULTI_KEY1 to this value if not None.
    """
    monkeypatch.setattr("sys.argv", ["pytest", "1", "--flag"])
    docstring = DOCSTRING_MULTI if multi else DOCSTRING_NOT_MULTI
    expected = EXPECTED_MULTI.copy() if multi else EXPECTED_NOT_MULTI.copy()
    expected["--flag"] = 1 if multi else True

    # Set variables.
    if set_key is not None:
        monkeypatch.setenv("MULTI_KEY", set_key)
    if set_key0 is not None:
        monkeypatch.setenv("MULTI_KEY0", set_key0)
    if set_key1 is not None:
        monkeypatch.setenv("MULTI_KEY1", set_key1)

    # Test not multi.
    if not multi:
        if set_key is not None:
            expected["--key"] = str(set_key)  # Others are ignored.
        actual = docoptcfg(docstring, env_prefix="MULTI_")
        assert actual == expected
        return

    set_keys = (set_key is not None, set_key0 is not None, set_key1 is not None)
    if set_keys == (True, True, True):
        expected["--key"] = [set_key, set_key0, set_key1]
    elif set_keys == (True, True, False):
        expected["--key"] = [set_key, set_key0]
    elif set_keys == (True, False, False):
        expected["--key"] = [set_key]

    elif set_keys == (False, False, False):
        expected["--key"] = []
    elif set_keys == (False, False, True):
        expected["--key"] = []
    elif set_keys == (False, True, True):
        expected["--key"] = [set_key0, set_key1]

    elif set_keys == (False, True, False):
        expected["--key"] = [set_key0]
    elif set_keys == (True, False, True):
        expected["--key"] = [set_key]

    else:
        raise NotImplementedError

    actual = docoptcfg(docstring, env_prefix="MULTI_")
    assert actual == expected
Example #5
0
def test_multi(monkeypatch, tmpdir):
    """Same with multi options.

    :param monkeypatch: pytest fixture.
    :param tmpdir: pytest fixture.
    """
    monkeypatch.setenv('FFMPEG_BIN', tmpdir.ensure('ffmpeg'))
    tmpdir.join('config.ini').write('[my_script]\nkey = \n    val1,\n    val2')

    actual = docoptcfg(DOCSTRING_MULTI, ['1', '--config', str(tmpdir.join('config.ini'))])
    expected = EXPECTED_MULTI.copy()
    expected['--config'] = str(tmpdir.join('config.ini'))

    assert actual == expected
Example #6
0
def test_multi_a_lot(monkeypatch):
    """Test setting >99 env variables. For branch coverage.

    :param monkeypatch: pytest fixture.
    """
    expected = EXPECTED_MULTI.copy()
    monkeypatch.setenv("MULTI_FLAG", "1")  # Ignore.
    for i in range(100):
        monkeypatch.setenv("MULTI_KEY{0}".format(i), str(i))
        if i < 99:
            expected["--key"].append(str(i))
    actual = docoptcfg(DOCSTRING_MULTI, ["1"], ignore=("-h", "-V", "--flag"), env_prefix="MULTI_")
    assert actual == expected
    assert "MULTI_KEY99" in os.environ
    assert "99" not in actual["--key"]
Example #7
0
def test_multi_a_lot(monkeypatch):
    """Test setting >99 env variables. For branch coverage.

    :param monkeypatch: pytest fixture.
    """
    expected = EXPECTED_MULTI.copy()
    monkeypatch.setenv('MULTI_FLAG', '1')  # Ignore.
    for i in range(100):
        monkeypatch.setenv('MULTI_KEY{0}'.format(i), str(i))
        if i < 99:
            expected['--key'].append(str(i))
    actual = docoptcfg(DOCSTRING_MULTI, ['1'],
                       ignore=('-h', '-V', '--flag'),
                       env_prefix='MULTI_')
    assert actual == expected
    assert 'MULTI_KEY99' in os.environ
    assert '99' not in actual['--key']
Example #8
0
def test_multi_flag(monkeypatch, tmpdir, multi, set_flag):
    """Test with repeatable flag/boolean option.

    :param monkeypatch: pytest fixture.
    :param tmpdir: pytest fixture.
    :param bool multi: Test with ... and without ... in docstring.
    :param str set_flag: Set flag= to this value if not None.
    """
    config_file = tmpdir.join('config.ini')
    config_file.write('[my_script]\n')

    monkeypatch.setattr('sys.argv',
                        ['pytest', '1', '--config',
                         str(config_file)])
    docstring = DOCSTRING_MULTI if multi else DOCSTRING_NOT_MULTI
    expected = EXPECTED_MULTI.copy() if multi else EXPECTED_NOT_MULTI.copy()
    expected['--config'] = str(config_file)

    if set_flag is not None:
        config_file.write('flag={0}'.format(set_flag), mode='a')
        if not multi and set_flag == '1':
            expected['--flag'] = True
        elif not multi:
            expected['--flag'] = False
        elif set_flag.isdigit():
            expected['--flag'] = int(set_flag)

    if multi and set_flag is not None and not set_flag.isdigit():
        with pytest.raises(DocoptcfgFileError) as exc:
            docoptcfg(docstring, config_option='--config')
        assert exc.value.message == 'Repeatable boolean option "flag" invalid.'
        assert exc.value.FILE_PATH == str(config_file)
        assert 'invalid literal for int()' in exc.value.original_error
        return

    if not multi and set_flag not in (None, '0', '1'):
        with pytest.raises(DocoptcfgFileError) as exc:
            docoptcfg(docstring, config_option='--config')
        assert exc.value.message == 'Boolean option "flag" invalid.'
        assert exc.value.FILE_PATH == str(config_file)
        assert 'Not a boolean' in exc.value.original_error
        return

    actual = docoptcfg(docstring, config_option='--config')
    assert actual == expected
Example #9
0
def test_multi_flag(monkeypatch, multi, set_flag):
    """Test with repeatable flag/boolean option.

    :param monkeypatch: pytest fixture.
    :param bool multi: Test with ... and without ... in docstring.
    :param str set_flag: Set MULTI_FLAG to this value if not None.
    """
    monkeypatch.setattr("sys.argv", ["pytest", "1"])
    docstring = DOCSTRING_MULTI if multi else DOCSTRING_NOT_MULTI
    expected = EXPECTED_MULTI.copy() if multi else EXPECTED_NOT_MULTI.copy()

    if set_flag is not None:
        monkeypatch.setenv("MULTI_FLAG", set_flag)
        if not multi and set_flag == "1":
            expected["--flag"] = True
        elif not multi:
            expected["--flag"] = False
        elif set_flag.isdigit():
            expected["--flag"] = int(set_flag)
        else:
            expected["--flag"] = 0

    actual = docoptcfg(docstring, env_prefix="MULTI_")
    assert actual == expected
Example #10
0
def test_multi_flag(monkeypatch, multi, set_flag):
    """Test with repeatable flag/boolean option.

    :param monkeypatch: pytest fixture.
    :param bool multi: Test with ... and without ... in docstring.
    :param str set_flag: Set MULTI_FLAG to this value if not None.
    """
    monkeypatch.setattr('sys.argv', ['pytest', '1'])
    docstring = DOCSTRING_MULTI if multi else DOCSTRING_NOT_MULTI
    expected = EXPECTED_MULTI.copy() if multi else EXPECTED_NOT_MULTI.copy()

    if set_flag is not None:
        monkeypatch.setenv('MULTI_FLAG', set_flag)
        if not multi and set_flag == '1':
            expected['--flag'] = True
        elif not multi:
            expected['--flag'] = False
        elif set_flag.isdigit():
            expected['--flag'] = int(set_flag)
        else:
            expected['--flag'] = 0

    actual = docoptcfg(docstring, env_prefix='MULTI_')
    assert actual == expected