def test_idempotent_io(inifile): data0 = load(inifile) with tempfile.TemporaryDirectory() as tmpdir: save1 = Path(tmpdir) / "save1" save2 = Path(tmpdir) / "save2" dump(data0, save1) data1 = load(save1) dump(data1, save2) text1 = save1.read_text().split("\n") text2 = save2.read_text().split("\n") diff = "".join(difflib.context_diff(text1, text2)) assert not diff
def test_fargo_dummy(datadir): conf = load(datadir / "fargo-dummy.ini") expected = { "x": 1, "y": 2, "z": 3, } assert conf == expected
def main(argv: Optional[List[str]] = None) -> int: parser = argparse.ArgumentParser() parser.add_argument("files", nargs="+") args = parser.parse_args(argv) retv = 0 for file in args.files: if not os.path.isfile(file): print(f"Error: could not find {file}", file=sys.stderr) retv = 1 continue try: load(file) except ValueError as exc: print(f"Failed to validate {file}:\n {exc}", file=sys.stderr) retv = 1 else: print(f"Validated {file}") return retv
def test_dump_to_file_descriptor(inifile, tmp_path): conf = load(inifile) file = tmp_path / "save.ini" with open(file, mode="w") as fh: dump(conf, fh) # a little weak but better than just testing that the file isn't empty new_body = file.read_text() for key, val in conf.items(): if isinstance(val, dict): assert f"[{key}]\n" in new_body
def test_pluto_disk_planet(datadir): conf = load(datadir / "pluto-DiskPlanet.ini") expected = [ "Grid", "Chombo Refinement", "Time", "Solver", "Boundary", "Static Grid Output", "Chombo HDF5 output", "Parameters", ] sections = list(conf.keys()) assert sections == expected
def test_dump_to_file_path(inifile, tmp_path): conf = load(inifile) # pathlib.Path obj file1 = tmp_path / "save1.ini" dump(conf, file1) body1 = file1.read_text() # str file2 = tmp_path / "save2.ini" sfile2 = str(file2) dump(conf, sfile2) body2 = file2.read_text() assert body1 == body2 for key, val in conf.items(): if isinstance(val, dict): assert f"[{key}]\n" in body2
def test_validate_known_files(inifile): conf = load(inifile) validate_inifile_schema(conf)
def main(argv: Optional[Sequence[str]] = None) -> int: parser = argparse.ArgumentParser() parser.add_argument("files", nargs="+") parser.add_argument( "--diff", action="store_true", help= "Print the unified diff to stdout instead of editing files inplace", ) parser.add_argument("--name-column-size", type=int, help="Fixed length of the parameter column") args = parser.parse_args(argv) retv = 0 for file in args.files: if not os.path.isfile(file): print(f"Error: could not find {file}", file=sys.stderr) retv = 1 continue try: load(file) except ValueError as exc: print(f"Error: {exc}", file=sys.stderr) retv = 1 continue with open(file) as fh: data = fh.read() fmted_data = iniformat(data, name_column_size=args.name_column_size) if fmted_data == data: print(f"{file} is already formatted", file=sys.stderr) continue else: retv = 1 if args.diff: for line in unified_diff(data.splitlines(), fmted_data.splitlines(), fromfile=file): if sys.version_info >= (3, 9): line = line.removesuffix("\n") elif line.endswith("\n"): line = line[:-1] print(line) print("\n") else: print(f"Fixing {file}", file=sys.stderr) try: with open(file, "w") as fh: fh.write(fmted_data) except OSError: print(f"Error: could not write to {file}", file=sys.stderr) retv = 1 continue return retv
def test_load_from_descriptor(inifile): with open(inifile) as fh: load(fh)
def test_unit_read(inifile): load(inifile)
def test_load_empty_file(capsys, tmp_path): target = tmp_path / "empty_file" target.touch() with pytest.raises(ValueError): load(target)
def test_dump_wrong_mode(mode, expected_err, inifile, tmp_path): conf = load(inifile) save_file = str(tmp_path / "save.ini") with pytest.raises(expected_err): with open(save_file, mode=mode) as fh: dump(conf, fh)
def test_string_casting(data, expected, tmp_path): file = tmp_path / "test_file.ini" file.write_text(data) mapping = load(file) assert mapping == expected