示例#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
文件: __init__.py 项目: tbarron/gitr
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
文件: test_tbx.py 项目: tbarron/tbx
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
文件: test_tbx.py 项目: tbarron/tbx
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
文件: test_tbx.py 项目: tbarron/tbx
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
文件: test_tbx.py 项目: tbarron/tbx
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
文件: test_tbx.py 项目: tbarron/tbx
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
文件: test_tbx.py 项目: tbarron/tbx
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
文件: test_tbx.py 项目: tbarron/tbx
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)