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"]
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
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
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'"
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"
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."
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"
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")
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]"
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")
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")
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"), ]
def test_valid_patterns(): psed = Psed(find=["pattern1", "pattern2"]) assert psed.find == [re.compile("pattern1"), re.compile("pattern2")]
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." )