예제 #1
0
def test_exists_file(monkeypatch):
    monkeypatch.setattr(os.path, "exists", lambda x: True)
    monkeypatch.setattr(os.path, "isfile", lambda x: True)

    psed = Psed(input="dummy")
    input_list = psed._get_input()
    assert input_list == ["dummy"]
예제 #2
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
예제 #3
0
def test_exists_with_glob(monkeypatch):
    globs = ["file1", "file2"]
    monkeypatch.setattr(os.path, "exists", lambda x: False)
    monkeypatch.setattr(glob, "glob", lambda x: globs)

    psed = Psed(input="dummy")
    input_list = psed._get_input()
    assert input_list == globs
예제 #4
0
def test_not_exists_no_glob(monkeypatch):
    monkeypatch.setattr(os.path, "exists", lambda x: False)
    monkeypatch.setattr(glob, "glob", lambda x: [])

    psed = Psed(input="dummy")
    with pytest.raises(SystemExit) as excinfo:
        psed._get_input()

    assert str(excinfo.value) == "The input path doesn't exist: 'dummy'"
예제 #5
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"
예제 #6
0
def test_empty_directory(monkeypatch):
    monkeypatch.setattr(os.path, "exists", lambda x: True)
    monkeypatch.setattr(os.path, "isfile", lambda x: False)
    monkeypatch.setattr(os, "walk", lambda x: [])

    psed = Psed(input="dummy")
    with pytest.raises(SystemExit) as excinfo:
        psed._get_input()

    assert str(excinfo.value) == "Input directory: 'dummy' contains no files."
예제 #7
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"
예제 #8
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")
예제 #9
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]"
예제 #10
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")
예제 #11
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")
예제 #12
0
def test_non_empty_directory(monkeypatch):
    monkeypatch.setattr(os.path, "exists", lambda x: True)
    monkeypatch.setattr(os.path, "isfile", lambda x: False)
    monkeypatch.setattr(
        os,
        "walk",
        lambda x: [
            ("dummy", ("bar", ), ("baz", )),
            (os.path.join("dummy", "bar"), (), ("spam", "eggs")),
        ],
    )

    psed = Psed(input="dummy")
    input_list = psed._get_input()

    assert input_list == [
        os.path.join("dummy", "baz"),
        os.path.join("dummy", "bar", "spam"),
        os.path.join("dummy", "bar", "eggs"),
    ]
예제 #13
0
def test_valid_patterns():
    psed = Psed(find=["pattern1", "pattern2"])
    assert psed.find == [re.compile("pattern1"), re.compile("pattern2")]
예제 #14
0
def test_invalid_patterns():
    with pytest.raises(SystemExit) as excinfo:
        Psed(find=["pattern1", "(pattern2"])
    assert (
        str(excinfo.value) == "Some find patterns have no been compiled successfully."
    )