|
55 | 55 | assert_generated_modules_output, |
56 | 56 | assert_httpx_get_kwargs, |
57 | 57 | assert_no_uncommented_generated_code, |
| 58 | + assert_output, |
58 | 59 | assert_runtime_import_package, |
59 | 60 | assert_warnings_contain, |
60 | 61 | assert_warnings_do_not_contain, |
@@ -2944,6 +2945,108 @@ def test_generate_accepts_path_input(output_file: Path) -> None: |
2944 | 2945 | ) |
2945 | 2946 |
|
2946 | 2947 |
|
| 2948 | +@pytest.mark.parametrize("custom_formatters", [None, []], ids=["custom-unset", "custom-empty"]) |
| 2949 | +def test_generate_with_empty_formatters(output_file: Path, custom_formatters: list[str] | None) -> None: |
| 2950 | + """Skip formatter work when the explicit formatter list is empty.""" |
| 2951 | + run_generate_file_and_assert( |
| 2952 | + input_path=JSON_SCHEMA_DATA_PATH / "person.json", |
| 2953 | + output_path=output_file, |
| 2954 | + input_file_type=InputFileType.JsonSchema, |
| 2955 | + disable_timestamp=True, |
| 2956 | + formatters=[], |
| 2957 | + custom_formatters=custom_formatters, |
| 2958 | + assert_func=assert_file_content, |
| 2959 | + expected_file="generate_with_empty_formatters.py", |
| 2960 | + ) |
| 2961 | + |
| 2962 | + |
| 2963 | +def test_generate_with_custom_formatter_and_empty_formatters(output_file: Path) -> None: |
| 2964 | + """Keep custom formatting when the built-in formatter list is empty.""" |
| 2965 | + run_generate_file_and_assert( |
| 2966 | + input_path=JSON_SCHEMA_DATA_PATH / "person.json", |
| 2967 | + output_path=output_file, |
| 2968 | + input_file_type=InputFileType.JsonSchema, |
| 2969 | + disable_timestamp=True, |
| 2970 | + formatters=[], |
| 2971 | + custom_formatters=["tests.data.python.custom_formatters.add_comment"], |
| 2972 | + assert_func=assert_file_content, |
| 2973 | + expected_file="generate_with_custom_formatter_and_empty_formatters.py", |
| 2974 | + ) |
| 2975 | + |
| 2976 | + |
| 2977 | +def test_parser_formatter_builder_override_with_empty_formatters() -> None: |
| 2978 | + """Keep subclass formatter-builder hooks active with an empty formatter list.""" |
| 2979 | + from datamodel_code_generator.parser.jsonschema import JsonSchemaParser |
| 2980 | + from tests.data.python.custom_formatters.add_comment import CodeFormatter as AddCommentFormatter |
| 2981 | + |
| 2982 | + class ConfiguringJsonSchemaParser(JsonSchemaParser): |
| 2983 | + code_formatter_build_count = 0 |
| 2984 | + |
| 2985 | + def _build_code_formatter( |
| 2986 | + self, |
| 2987 | + settings_path: Path | None, |
| 2988 | + *, |
| 2989 | + is_multi_module_output: bool, |
| 2990 | + ) -> CodeFormatter: |
| 2991 | + code_formatter = super()._build_code_formatter( |
| 2992 | + settings_path, |
| 2993 | + is_multi_module_output=is_multi_module_output, |
| 2994 | + ) |
| 2995 | + code_formatter.custom_formatters.append(AddCommentFormatter(formatter_kwargs={})) |
| 2996 | + self.code_formatter_build_count += 1 |
| 2997 | + return code_formatter |
| 2998 | + |
| 2999 | + parser = ConfiguringJsonSchemaParser( |
| 3000 | + source=(JSON_SCHEMA_DATA_PATH / "person.json").resolve(), |
| 3001 | + formatters=[], |
| 3002 | + ) |
| 3003 | + assert_output( |
| 3004 | + f"{parser.parse()}\n", |
| 3005 | + EXPECTED_MAIN_PATH / "parser_formatter_builder_override_with_empty_formatters.py", |
| 3006 | + ) |
| 3007 | + assert_output( |
| 3008 | + f"{parser.code_formatter_build_count}\n", |
| 3009 | + EXPECTED_MAIN_PATH / "parser_formatter_builder_override_with_empty_formatters_calls.txt", |
| 3010 | + ) |
| 3011 | + |
| 3012 | + |
| 3013 | +def test_parser_instance_formatter_builder_with_empty_formatters() -> None: |
| 3014 | + """Keep an instance-injected formatter builder active with an empty formatter list.""" |
| 3015 | + from datamodel_code_generator.parser.jsonschema import JsonSchemaParser |
| 3016 | + from tests.data.python.custom_formatters.add_comment import CodeFormatter as AddCommentFormatter |
| 3017 | + |
| 3018 | + parser = JsonSchemaParser( |
| 3019 | + source=(JSON_SCHEMA_DATA_PATH / "person.json").resolve(), |
| 3020 | + formatters=[], |
| 3021 | + ) |
| 3022 | + default_builder = parser._build_code_formatter |
| 3023 | + code_formatter_build_count = 0 |
| 3024 | + |
| 3025 | + def build_code_formatter( |
| 3026 | + settings_path: Path | None, |
| 3027 | + *, |
| 3028 | + is_multi_module_output: bool, |
| 3029 | + ) -> CodeFormatter: |
| 3030 | + nonlocal code_formatter_build_count |
| 3031 | + code_formatter = default_builder( |
| 3032 | + settings_path, |
| 3033 | + is_multi_module_output=is_multi_module_output, |
| 3034 | + ) |
| 3035 | + code_formatter.custom_formatters.append(AddCommentFormatter(formatter_kwargs={})) |
| 3036 | + code_formatter_build_count += 1 |
| 3037 | + return code_formatter |
| 3038 | + |
| 3039 | + parser._build_code_formatter = build_code_formatter # type: ignore[method-assign] |
| 3040 | + assert_output( |
| 3041 | + f"{parser.parse()}\n", |
| 3042 | + EXPECTED_MAIN_PATH / "parser_formatter_builder_override_with_empty_formatters.py", |
| 3043 | + ) |
| 3044 | + assert_output( |
| 3045 | + f"{code_formatter_build_count}\n", |
| 3046 | + EXPECTED_MAIN_PATH / "parser_formatter_builder_override_with_empty_formatters_calls.txt", |
| 3047 | + ) |
| 3048 | + |
| 3049 | + |
2947 | 3050 | def test_generate_keeps_existing_path_string_input() -> None: |
2948 | 3051 | """Test generate() keeps existing path strings as inline source text.""" |
2949 | 3052 | run_generate_and_assert( |
|
0 commit comments