Example #1
0
def test_count_lines_exception(linecount_file):

    """
    Make sure known exceptions in `count_lines()` are raised.
    """

    path = linecount_file()
    with pytest.raises(ValueError):
        tools.count_lines(path, linesep='too many chars')
Example #2
0
def test_count_lines_only_linesep(tmpdir):

    """
    File only contains a `linesep`.
    """

    path = str(tmpdir.mkdir('test_count_lines').join('only_linesep'))
    with open(path, 'w') as f:
        f.write(os.linesep)
    assert tools.count_lines(path) == 1
Example #3
0
def test_count_lines_empty(tmpdir):

    """
    Completely empty file.
    """

    path = str(tmpdir.mkdir('test_count_lines').join('empty'))
    with open(path, 'w') as f:
        pass
    assert tools.count_lines(path) == 0
Example #4
0
def test_count_lines_buffered(linesep, linecount_file):

    """
    Use the buffered method to count lines
    """

    path = linecount_file(linesep)
    buff = os.stat(path).st_size // 4
    assert _icount_lines(path) == tools.count_lines(
        path, linesep=linesep, buffer=buff)
Example #5
0
def test_count_lines_small(linesep, linecount_file):

    """
    Count lines of a file that fits in the buffer.
    """

    path = linecount_file(linesep)
    buff = os.stat(path).st_size + 2
    assert _icount_lines(path) == tools.count_lines(
        path, linesep=linesep, buffer=buff)
Example #6
0
def test_count_lines_literal_linesep(tmpdir):

    """
    Explicitly test a scenario where the input file contains a literal '\n'.
    """

    path = str(tmpdir.mkdir('test_count_lines').join('literal_linesep'))
    with open(path, 'w') as f:
        f.write('first line with stuff' + os.linesep)
        f.write('before \{} after'.format(os.linesep) + os.linesep)
    assert tools.count_lines(path) == 3
Example #7
0
def test_count_lines_split_buffer(tmpdir):

    """
    Explicitly test a scenario where the `linesep` character is 2 bytes long
    and is split across blocks.
    """

    path = str(tmpdir.mkdir('test_count_lines').join('split_buffer'))
    with open(path, 'wb') as f:
        f.write(b'\r\nhey some words')
    assert tools.count_lines(path, buffer=1, linesep='\r\n') == 1
Example #8
0
def test_count_lines_trailing_linesep(tmpdir):

    """
    Last line has a trailing `linesep`.
    """

    path = str(tmpdir.mkdir('test_count_lines').join('trailing_linesep'))
    with open(path, 'w') as f:
        f.write('line1' + os.linesep)
        f.write('line2' + os.linesep)
        f.write('line3' + os.linesep)
    assert tools.count_lines(path) == 3