code: def test_migrate_domain_cleanups_after_raising(
      tmp_path: Path,
      test_case: Callable[[Path], Tuple[Text, Text]],
      default_out_dir: bool,
      out_dir_exists: bool,
  ):
      # input
      out_path = (
          None if default_out_dir else (tmp_path / "custom_out_path")
      )
      domain_path, error_msg_match = test_case(tmp_path)
      migrating_file_only = domain_path.is_file()
      domain_files = list(domain_path.iterdir())
  
      # paths to be used by migration tool
      domain_parent_dir = domain_path.parent
      expected_out_path = (
          out_path
          if out_path is not None
          else (domain_parent_dir / migrate.DEFAULT_NEW_DOMAIN)
      )
      expected_backup_path = domain_parent_dir / migrate.ORIGINAL_DOMAIN
      if migrating_file_only:
          expected_backup_path = (
              f"{expected_backup_path}{migrate.YML_SUFFIX}"
          )
          expected_out_path = f"{expected_out_path}{migrate.YML_SUFFIX}"
  
      # create the folder if needed
      if not migrating_file_only and out_dir_exists:
          expected_out_path.mkdir(parents=True)
  
      # migrate!
      with pytest.raises(RasaException, match=error_msg_match):
          migrate.migrate_domain_format(domain_path, out_path)
      assert Path.exists(domain_path)
      assert all(Path.exists(file) for file in domain_files)
      assert not Path.exists(expected_backup_path)
      if not migrating_file_only:
          # if and only if the folder didn't exist before, it should not exist afterwards
          assert Path.exists(expected_out_path) == out_dir_exists
  
      # ... and to assert we really did remove something:
      expected_to_be_removed = [call(expected_backup_path)]
      if not out_dir_exists:  # only removed if it didn't exist
          expected_to_be_removed.append(call(expected_out_path))
      patching = (
          [shutil, "rmtree"]
          if not migrating_file_only
          else [Path, "unlink"]
      )
      with patch.object(*patching) as removal:
          with pytest.raises(RasaException, match=error_msg_match):
              migrate.migrate_domain_format(domain_path, out_path)
          assert removal.call_count == 1 + (not out_dir_exists)
          removal.assert_has_calls(expected_to_be_removed)
  
imports: import itertools
  import shutil
  import re
  import textwrap
  from pathlib import Path
  from typing import Text, Any, Tuple, Callable
  from unittest.mock import patch, call
  import pytest
  import rasa.shared.utils.io
  from rasa.core import migrate
  from rasa.shared.core.domain import Domain
  from rasa.shared.exceptions import RasaException
  from rasa.shared.constants import LATEST_TRAINING_DATA_FORMAT_VERSION
label_desc: Use image node_id 21 for calling a global function with example usage: test_migrate_domain_cleanups_after_raising(tmp_path, test_case, default_out_dir, out_dir_exists) without return types