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')
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
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))
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))
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)
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)
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)
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'
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)
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)
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)
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)
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)