def test_multiple_targets(multiple_targets_setup, capsys): targets, subs_file, expected = multiple_targets_setup ret = main([*[str(t) for t in targets], "-s", str(subs_file)]) assert ret == 0 out, err = capsys.readouterr() assert out == expected assert err == ""
def test_main(simple_setup, capsys): target, subs_file, expected = simple_setup retval = main([str(target), "-s", str(subs_file)]) assert retval == 0 out, err = capsys.readouterr() assert out == expected assert err == ""
def test_missing_positionals(argv, expected_err, capsys, monkeypatch): monkeypatch.setattr("sys.argv", argv) ret = main() out, err = capsys.readouterr() assert ret != 0 assert out == "" assert err.splitlines()[0] == expected_err # skip the first line because it differs between macos and linux assert err.splitlines()[2:] == HELP_MESSAGE.splitlines()[2:]
def test_empty_diff(tmp_path, capsys): f1 = tmp_path / "file1.txt" f2 = tmp_path / "file2.json" f1.touch() f2.write_text('{"nothing": "emtpy"}') ret = main([str(f1), "--subs", str(f2), "--diff"]) out, err = capsys.readouterr() assert ret == 0 assert out == "" assert err == ""
def test_broken_json(capsys, tmp_path): f1 = tmp_path / "file1.txt" f2 = tmp_path / "file2.json" f1.touch() f2.touch() ret = main([str(f1), "--subs", str(f2)]) out, err = capsys.readouterr() assert ret != 0 assert out == "" assert err.replace("\n", "") == f"Error invalid json file `{f2}`"
def test_invalid_schema(tmp_path, capsys): target = tmp_path / "hello.txt" target.write_text("nothing") subs_file = tmp_path / "subs.json" with open(subs_file, "w") as fh: json.dump({"A": 0.78}, fh) ret = main([str(target), "-s", str(subs_file)]) out, err = capsys.readouterr() assert ret != 0 assert out == "" assert err == "Error unrecognized json schema.\n"
def test_inplace_substitution(simple_setup, capsys, flag: str): target, subs_file, expected = simple_setup retval = main([str(target), "-s", str(subs_file), flag]) assert retval == 0 out, err = capsys.readouterr() assert out == "" assert err == "" actual = target.read_text() assert actual == expected
def test_missing_files(simple_setup, capsys, missing: Tuple[bool, bool]): target, subs_file, _expected = simple_setup if missing[0]: target = target.with_suffix(".this_file_doesnt_exists") assert not target.exists() expected_err = f"Error {target} not found." if missing[1]: subs_file = subs_file.with_suffix(".this_file_doesnt_exists") assert not subs_file.exists() expected_err = f"Error {subs_file} not found." ret = main([str(target), "-s", str(subs_file)]) assert ret != 0 out, err = capsys.readouterr() assert out == "" assert err.replace("\n", "") == expected_err
def test_patch(simple_setup, capsys, monkeypatch, flag: str): target, subs_file, _expected = simple_setup retval = main([str(target), "-s", str(subs_file), flag]) assert retval == 0 expected = ( "-Helo world !\n" "-just came here to say helo.\n" "+Hello world !\n" "+just came here to say hello.\n\n" ) out, err = capsys.readouterr() res = "".join(out.splitlines(keepends=True)[4:]) assert expected in res target_len = len(expected.splitlines()) assert len(res.splitlines()) in list(range(target_len, target_len + 3)) assert err == ""
def test_version(flag, capsys): ret = main([flag]) out, err = capsys.readouterr() assert ret == 0 assert out == f"{__version__}\n" assert err == ""
from apply_subs.main import main if __name__ == "__main__": exit(main())