Exemplo n.º 1
0
def test_not_found(fs, ):
    fs.create_file("input_file", contents="the haystack")

    psed = Psed(find=["needle"])
    matches = psed.process_file("input_file")

    assert not matches
Exemplo n.º 2
0
def test_replace_no_matches(fs):
    in_file = fs.create_file("input_file", contents="a big haystack")

    assert in_file.contents == "a big haystack"

    psed = Psed(find=["needle"], replace="nail", inplace=True)
    match = psed.process_file("input_file")

    assert not match
    assert in_file.contents == "a big haystack"
Exemplo n.º 3
0
def test_replace_simple_inplace(fs):
    in_file = fs.create_file("input_file",
                             contents="the haystack with a needle somewhere")

    assert in_file.contents == "the haystack with a needle somewhere"

    psed = Psed(find=["needle"], replace="nail", inplace=True)
    match = psed.process_file("input_file")

    assert match
    assert in_file.contents == "the haystack with a nail somewhere"
Exemplo n.º 4
0
def test_find_simple(fs, capsys):
    fs.create_file("input_file",
                   contents="the haystack with a needle somewhere")

    psed = Psed(find=["needle"])
    matches = psed.process_file("input_file")

    assert len(matches) == 1

    output = capsys.readouterr().out
    assert output == ("input_file: 1 match:\n	(20, 26): needle\n")
Exemplo n.º 5
0
def test_replace_regex(fs):
    fs.create_file("input_file", contents="[ERROR] and [WARNING]")
    out_file = fs.create_file("input_file_psed")

    assert out_file.contents == ""

    psed = Psed(find=[r"\[(\w+)\]"], replace=r"[SOME_\1]")
    match = psed.process_file("input_file")

    assert match

    assert out_file.contents == "[SOME_ERROR] and [SOME_WARNING]"
Exemplo n.º 6
0
def test_find_multiple_matches(fs, capsys):
    fs.create_file("input_file", contents=LOG_FILE_CONTENT)

    psed = Psed(find=["ERROR"])
    matches = psed.process_file("input_file")

    assert len(matches) == 3

    output = capsys.readouterr().out
    assert output == ("input_file: 3 matches:\n"
                      "\t(1, 6): ERROR\n"
                      "\t(60, 65): ERROR\n"
                      "\t(80, 85): ERROR\n")
Exemplo n.º 7
0
def test_find_multiple_inputs(fs, capsys):
    fs.create_file("input_file", contents=LOG_FILE_CONTENT)

    psed = Psed(find=[r"\[ERROR\]", r"\[INFO\]", r"\[WARNING\]", r"\[DEBUG\]"])
    matches = psed.process_file("input_file")

    assert len(matches) == 6

    output = capsys.readouterr().out
    assert output == ("input_file: 6 matches:\n"
                      "\t(0, 7): [ERROR]\n"
                      "\t(59, 66): [ERROR]\n"
                      "\t(79, 86): [ERROR]\n"
                      "\t(19, 25): [INFO]\n"
                      "\t(36, 45): [WARNING]\n"
                      "\t(111, 118): [DEBUG]\n")