def test_generated_code() -> None: classes = [ "Empty", "UntypedArg", "IntArg", "UnionArg", "WithLibraryClassArg", "IncompatibleDataclassArg", "WithStringDefault", "WithUntypedStringDefault", "ListValues", "DictValues", "Tuples", "PeskySentinelUsage", ] expected_file = Path(MODULE_NAME.replace(".", "/")) / "generated.py" expected = expected_file.read_text() generated = generate_module( cfg=conf, module=ModuleConf( name=MODULE_NAME, classes=classes, ), ) _assert_expected_output(generated, expected, expected_file)
def test_generated_code_with_default_flags(classname: str, default_flags: Flags, expected_filename: str) -> None: expected_file = (Path(MODULE_NAME.replace(".", "/")) / "default_flags" / expected_filename) expected = expected_file.read_text() generated = generate_module( cfg=conf, module=ModuleConf(name=MODULE_NAME, classes=[classname], default_flags=default_flags), ) lines = [ line for line in unified_diff( expected.splitlines(), generated.splitlines(), fromfile=str(expected_file), tofile="Generated", ) ] diff = "\n".join(lines) if generated != expected: print(diff) assert False, f"Mismatch between {expected_file} and generated code"
def test_generated_code_future_ann() -> None: classes = ["ExampleClass"] expected_file = (Path(MODULE_NAME.replace(".", "/")) / "generated_future_annotations.py") expected = expected_file.read_text() generated = generate_module( cfg=conf, module=ModuleConf( name=MODULE_NAME + ".future_annotations", classes=classes, ), ) _assert_expected_output(generated, expected, expected_file)
def test_generated_code() -> None: classes = [ "Empty", "UntypedArg", "IntArg", "Args", "Kwargs", "UnionArg", "WithLibraryClassArg", "IncompatibleDataclassArg", "WithStringDefault", "WithUntypedStringDefault", "ListValues", "DictValues", "Tuples", "PeskySentinelUsage", ] expected_file = Path(MODULE_NAME.replace(".", "/")) / "generated.py" expected = expected_file.read_text() generated = generate_module( cfg=conf, module=ModuleConf( name=MODULE_NAME, classes=classes, ), ) lines = [ line for line in unified_diff( expected.splitlines(), generated.splitlines(), fromfile=str(expected_file), tofile="Generated", ) ] diff = "\n".join(lines) if generated != expected: print(diff) assert False, f"Mismatch between {expected_file} and generated code"
def _generate_conf( cls: type[Self], output_dir: Path, *, module_class_dict: dict[str, List[str]] ) -> None: from configen.config import ConfigenConf, ModuleConf # type: ignore from configen.configen import generate_module # type: ignore cfg = ConfigenConf( output_dir=str(output_dir), module_path_pattern=f"{cls._CONFIGEN_FILENAME}", modules=[], header="", ) for module, classes in module_class_dict.items(): module_conf = ModuleConf(name=module, classes=classes) code = generate_module(cfg=cfg, module=module_conf) output_dir.mkdir(parents=True, exist_ok=True) conf_dir = output_dir / cls._module_to_fp(module) conf_dir.mkdir(parents=True) conf_file = conf_dir / cls._CONFIGEN_FILENAME with conf_file.open("a+") as file: file.write(code)