1111import time
1212import warnings
1313from argparse import Namespace
14- from collections .abc import Callable , Generator , Sequence
14+ from collections .abc import Callable , Generator , Mapping , Sequence
1515from contextlib import contextmanager
1616from functools import cache
1717from pathlib import Path
3131 _infer_expected_file ,
3232 _validation_stats ,
3333 assert_directory_content ,
34+ assert_inputs_not_mutated ,
3435 assert_output ,
3536 assert_warnings_contain ,
3637 freeze_time ,
@@ -611,6 +612,7 @@ def run_generate_file_and_assert(
611612 expected_file : str | Path | None = None ,
612613 transform : Callable [[str ], str ] | None = None ,
613614 expected_warnings : Sequence [str ] | None = None ,
615+ unchanged_inputs : Mapping [str , object ] | None = None ,
614616 ** generate_kwargs : Any ,
615617) -> None :
616618 """Execute generate() for a file input and assert the generated output."""
@@ -632,19 +634,20 @@ def run_generate_file_and_assert(
632634 if input_file_type is not None :
633635 generate_options ["input_file_type" ] = input_file_type
634636
635- if expected_warnings is None :
636- generate (
637- input_ = input_ ,
638- ** generate_options ,
639- )
640- else :
641- with warnings .catch_warnings (record = True ) as warning_records :
642- warnings .simplefilter ("always" )
637+ with assert_inputs_not_mutated (unchanged_inputs ):
638+ if expected_warnings is None :
643639 generate (
644640 input_ = input_ ,
645641 ** generate_options ,
646642 )
647- assert_warnings_contain (warning_records , * expected_warnings )
643+ else :
644+ with warnings .catch_warnings (record = True ) as warning_records :
645+ warnings .simplefilter ("always" )
646+ generate (
647+ input_ = input_ ,
648+ ** generate_options ,
649+ )
650+ assert_warnings_contain (warning_records , * expected_warnings )
648651
649652 if expected_file is None :
650653 frame = inspect .currentframe ()
@@ -654,24 +657,32 @@ def run_generate_file_and_assert(
654657 del frame
655658
656659 assert_func (output_path , expected_file , transform = transform )
657- _assert_builtin_generate_formatter_parity (
658- input_ = input_ ,
659- output_path = output_path ,
660- generate_options = generate_options ,
661- expected_warnings = expected_warnings ,
662- )
660+ with assert_inputs_not_mutated (unchanged_inputs ):
661+ _assert_builtin_generate_formatter_parity (
662+ input_ = input_ ,
663+ output_path = output_path ,
664+ generate_options = generate_options ,
665+ expected_warnings = expected_warnings ,
666+ )
663667
664668
665669def run_generate_and_assert (
666670 * ,
667671 input_ : Any ,
668672 expected_file : Path ,
673+ assert_input_unchanged : bool = False ,
674+ unchanged_inputs : Mapping [str , object ] | None = None ,
669675 ** generate_kwargs : Any ,
670676) -> None :
671677 """Execute generate(output=None) and assert the returned text output."""
672678 __tracebackhide__ = True
673679
674- result = generate (input_ = input_ , ** _default_formatter_generate_options (generate_kwargs ))
680+ guarded_inputs = dict (unchanged_inputs or {})
681+ if assert_input_unchanged :
682+ guarded_inputs ["input_" ] = input_
683+
684+ with assert_inputs_not_mutated (guarded_inputs or None ):
685+ result = generate (input_ = input_ , ** _default_formatter_generate_options (generate_kwargs ))
675686 if not isinstance (result , str ): # pragma: no cover
676687 pytest .fail (f"Expected generate() to return str, got { type (result ).__name__ } " )
677688 assert_output (result , expected_file )
0 commit comments