Пример #1
0
def test_contents_good_list(ctest):
    """
    Calling contents on a file that exists as a list
    """
    pytest.dbgfunc()
    result = tbx.contents(ctest.data.strpath, fmt='list')
    assert result == ctest.exp.split('\n')
Пример #2
0
def test_contents_good_str(ctest):
    """
    Calling contents on a file that exists as a string
    """
    pytest.dbgfunc()
    result = tbx.contents(ctest.data.strpath)
    assert result == ctest.exp
Пример #3
0
def version_update(target, new, old=None):
    """
    Given a target path and new version array, write out the new version.

    If target is empty, write one line: '__version__ = '<new>''

    If old is not None, format and find it in target's contents and replace it
    with the new version.

    If target is not empty and old and is not found, fail and complain.
    """
    news = '.'.join(new)
    c = tbx.contents(target, default='')
    with open(target, 'w') as f:
        if not old:
            if not c:
                f.write("__version__ = '{0}'\n".format(news))
            else:
                sys.exit("Don't know where to put '{0}' in '{1}'".format(
                    news, c))
        else:
            olds = '.'.join(old)
            if not c:
                sys.exit("Can't update '{0}' in an empty file".format(olds))
            elif olds in c:
                f.write(c.replace(olds, news))
            else:
                sys.exit("'{0}' not found in '{1}'".format(olds, c))
Пример #4
0
def version_update(target, new, old=None):
    """
    Given a target path and new version array, write out the new version.

    If target is empty, write one line: '__version__ = '<new>''

    If old is not None, format and find it in target's contents and replace it
    with the new version.

    If target is not empty and old and is not found, fail and complain.
    """
    news = '.'.join(new)
    c = tbx.contents(target, default='')
    with open(target, 'w') as f:
        if not old:
            if not c:
                f.write("__version__ = '{0}'\n".format(news))
            else:
                sys.exit("Don't know where to put '{0}' in '{1}'".format(news,
                                                                         c))
        else:
            olds = '.'.join(old)
            if not c:
                sys.exit("Can't update '{0}' in an empty file".format(olds))
            elif olds in c:
                f.write(c.replace(olds, news))
            else:
                sys.exit("'{0}' not found in '{1}'".format(olds, c))
Пример #5
0
def test_contents_good_list(ctest):
    """
    Calling contents on a file that exists as a list
    """
    pytest.dbgfunc()
    result = tbx.contents(ctest.data.strpath, fmt='list')
    assert result == ctest.exp.split('\n')
Пример #6
0
def test_contents_good_str(ctest):
    """
    Calling contents on a file that exists as a string
    """
    pytest.dbgfunc()
    result = tbx.contents(ctest.data.strpath)
    assert result == ctest.exp
Пример #7
0
def test_contents_good_altsep(ctest):
    """
    Calling contents on a file that exists as a list
    """
    pytest.dbgfunc()
    result = tbx.contents(ctest.data.strpath, fmt='list', sep=r'\s')
    assert len(result) == len(re.split(r'\s', ctest.exp))
    assert result == re.split(r'\s', ctest.exp)
Пример #8
0
def test_contents_badfmt(ctest):
    """
    Calling contents on a file that exists as a list
    """
    pytest.dbgfunc()
    with pytest.raises(tbx.Error) as err:
        _ = tbx.contents(ctest.data.strpath, fmt='str', sep=r'\s')
    assert 'Non-default separator is only valid for list format' in str(err)
Пример #9
0
def test_contents_invalid_fmt(ctest):
    """
    Calling contents with an invalid format
    """
    pytest.dbgfunc()
    with pytest.raises(tbx.Error) as err:
        _ = tbx.contents(ctest.data.strpath, fmt='foobar', sep=r'\s')
    assert "Invalid format" in str(err.value)
Пример #10
0
def test_contents_good_altsep(ctest):
    """
    Calling contents on a file that exists as a list
    """
    pytest.dbgfunc()
    result = tbx.contents(ctest.data.strpath, fmt='list', sep=r'\s')
    assert len(result) == len(re.split(r'\s', ctest.exp))
    assert result == re.split(r'\s', ctest.exp)
Пример #11
0
def test_contents_nosuch_default(ctest):
    """
    Attempting to get the contents of a non-existent file with a default value
    should return the default
    """
    pytest.dbgfunc()
    filename = ctest.data.strpath + 'xxx'
    result = tbx.contents(filename, default='foobar')
    assert result == 'foobar'
Пример #12
0
def test_contents_badperm(ctest):
    """
    Calling contents on a file that is not readable with throw an exception
    """
    pytest.dbgfunc()
    ctest.data.chmod(0000)
    with pytest.raises(tbx.Error) as err:
        _ = tbx.contents(ctest.data.strpath, fmt=str, sep=r'\s')
    assert "Can't read file {0}".format(ctest.data.strpath) in str(err)
Пример #13
0
def test_contents_badperm(ctest):
    """
    Calling contents on a file that is not readable with throw an exception
    """
    pytest.dbgfunc()
    ctest.data.chmod(0000)
    with pytest.raises(tbx.Error) as err:
        _ = tbx.contents(ctest.data.strpath, fmt=str, sep=r'\s')
    assert "Can't read file {0}".format(ctest.data.strpath) in str(err.value)
Пример #14
0
def test_contents_badfmt(ctest):
    """
    Calling contents on a file that exists with an invalid separator
    """
    pytest.dbgfunc()
    with pytest.raises(tbx.Error) as err:
        _ = tbx.contents(ctest.data.strpath, fmt='str', sep=r'\s')
    msg = 'Non-default separator is only valid for list format'
    assert msg in str(err.value)
Пример #15
0
def test_contents_nosuch_default(ctest):
    """
    Attempting to get the contents of a non-existent file with a default value
    should return the default
    """
    pytest.dbgfunc()
    filename = ctest.data.strpath + 'xxx'
    result = tbx.contents(filename,
                          default='foobar')
    assert result == 'foobar'
Пример #16
0
def test_contents_nosuch_nodefault(ctest):
    """
    Attempting to get the contents of a non-existent file with no default value
    should raise an exception
    """
    pytest.dbgfunc()
    filename = ctest.data.strpath + 'xxx'
    with pytest.raises(IOError) as err:
        _ = tbx.contents(filename)
    assert "No such file or directory" in str(err)
    assert filename in str(err)
Пример #17
0
def test_contents_nosuch_nodefault(ctest):
    """
    Attempting to get the contents of a non-existent file with no default value
    should raise an exception
    """
    pytest.dbgfunc()
    filename = ctest.data.strpath + 'xxx'
    with pytest.raises(IOError) as err:
        _ = tbx.contents(filename)
    assert "No such file or directory" in str(err.value)
    assert filename in str(err.value)