Exemple #1
0
def test_replace_in_ltimes(temp_file):
    fp = temp_file('-up br -d br -t br')
    pattern = re.compile('br')
    replace_in(fp, pattern, 'X', ltimes=1, assert_blocks=False)
    assert read_file(fp) == "-up X -d br -t br"
    replace_in(fp, pattern, 'X', assert_blocks=False)
    assert read_file(fp) == "-up X -d X -t X"
Exemple #2
0
def test_replace_in_no_outside(temp_file):
    original = textwrap.dedent(f"""
    interface eth0
    {START_BLOCK}
    interface wlan0
    {END_BLOCK}
    """)
    fp = temp_file(original)
    pattern = re.compile(r'^(interface )(.*?)$')
    replace_in(fp, pattern, replace=r'\1wlan1', assert_blocks=True)
    assert read_file(fp) == original.replace('wlan0', 'wlan1')
Exemple #3
0
def test_replace_in_ntimes(temp_file):
    original = textwrap.dedent(
        """
        alias BYE="/QUIT"
        alias BYE="/QUITT"
        alias BYE=
        """)
    fp = temp_file(original)
    replace_in(
        fp, re.compile(r'alias BYE=.*?$'), '', ntimes=2, assert_blocks=False)
    content = read_file(fp)
    assert content == "\n\n\nalias BYE=\n"
Exemple #4
0
def test_insert_in(where, prepend, temp_file):
    fp = temp_file(content)
    insert_in(fp, 'XXX', where, match_line=True, prepend=prepend, backup=False)
    text = read_file(fp)

    text_lines = text.split('\n')
    where_ix = text_lines.index(where)
    result_ix = text_lines.index('XXX')
    start_ix = text_lines.index(START_BLOCK)
    end_ix = text_lines.index(END_BLOCK)
    print(text)

    if prepend:
        assert end_ix == where_ix - 1
        assert result_ix == where_ix - 2
        assert start_ix == where_ix - 3
    else:
        assert start_ix == where_ix + 1
        assert result_ix == where_ix + 2
        assert end_ix == where_ix + 3
Exemple #5
0
def test_replace_in_blocks(block, assert_blocks, temp_file):
    original = with_blocks('content')
    fp = temp_file(original)
    replace_in(fp, re.compile(block), 'X', assert_blocks=assert_blocks)
    assert read_file(fp) == original
Exemple #6
0
def test_replace_in(pattern, replace, output, blocks, temp_file):
    fp = temp_file(with_blocks('custom\n# \nkey: val'))
    replace_in(fp, re.compile(pattern), replace, assert_blocks=blocks)
    assert read_file(fp) == output