# Path: rebasehelper/constants.py
# ENCODING: str = locale.getpreferredencoding()
# 
# Path: rebasehelper/temporary_environment.py
# class TemporaryEnvironment:
# 
#     """Class representing a temporary environment (directory) that can be used as a workspace.
# 
#     Works as a context manager.
# 
#     """
# 
#     TEMPDIR: str = 'TEMPDIR'
# 
#     def __init__(self, exit_callback=None):
#         self._env: Dict[str, str] = {}
#         self._exit_callback = exit_callback
# 
#     def __enter__(self):
#         self._env[self.TEMPDIR] = PathHelper.get_temp_dir()
#         logger.debug("Created environment in '%s'", self.path())
#         return self
# 
#     def __exit__(self, *args):
#         # run callback before removing the environment
#         try:
#             self._exit_callback(**self.env())
#         except TypeError:
#             pass
#         else:
#             logger.debug("Exit callback executed successfully")
# 
#         shutil.rmtree(self.path(), onerror=lambda func, path, excinfo: shutil.rmtree(path))
#         logger.debug("Destroyed environment in '%s'", self.path())
# 
#     def __str__(self):
#         return "<TemporaryEnvironment path='{}'>".format(self.path())
# 
#     def path(self):
#         """Gets the path to the temporary environment.
# 
#         Returns:
#             str: Absolute path to the temporary environment.
# 
#         """
#         return self._env.get(self.TEMPDIR, '')
# 
#     def env(self):
#         """Gets a copy of _env dictionary.
# 
#         Returns:
#             dict: Copy of _env dictionary.
# 
#         """
#         return self._env.copy()


import os
import tempfile
from rebasehelper.constants import ENCODING
from rebasehelper.temporary_environment import TemporaryEnvironment
            assert path != ''
            assert os.path.exists(path)
            assert os.path.isdir(path)
            env = temp.env()
            assert env.get(temp.TEMPDIR, None) is not None
            assert env.get(temp.TEMPDIR, None) == path

        assert not os.path.exists(path)
        assert not os.path.isdir(path)

    def test_with_statement_exception(self):
        path = ''

        try:
            with TemporaryEnvironment() as temp:
                path = temp.path()
                raise RuntimeError()
        except RuntimeError:
            pass

        assert not os.path.exists(path)
        assert not os.path.isdir(path)

    def test_with_statement_callback(self):
        tmp_file, tmp_path = tempfile.mkstemp(text=True)
        os.close(tmp_file)

        def callback(**kwargs):
            path = kwargs.get(TemporaryEnvironment.TEMPDIR, '')
            assert path != ''

# Path: rebasehelper/constants.py
# ENCODING: str = locale.getpreferredencoding()
# 
# Path: rebasehelper/temporary_environment.py
# class TemporaryEnvironment:
# 
#     """Class representing a temporary environment (directory) that can be used as a workspace.
# 
#     Works as a context manager.
# 
#     """
# 
#     TEMPDIR: str = 'TEMPDIR'
# 
#     def __init__(self, exit_callback=None):
#         self._env: Dict[str, str] = {}
#         self._exit_callback = exit_callback
# 
#     def __enter__(self):
#         self._env[self.TEMPDIR] = PathHelper.get_temp_dir()
#         logger.debug("Created environment in '%s'", self.path())
#         return self
# 
#     def __exit__(self, *args):
#         # run callback before removing the environment
#         try:
#             self._exit_callback(**self.env())
#         except TypeError:
#             pass
#         else:
#             logger.debug("Exit callback executed successfully")
# 
#         shutil.rmtree(self.path(), onerror=lambda func, path, excinfo: shutil.rmtree(path))
#         logger.debug("Destroyed environment in '%s'", self.path())
# 
#     def __str__(self):
#         return "<TemporaryEnvironment path='{}'>".format(self.path())
# 
#     def path(self):
#         """Gets the path to the temporary environment.
# 
#         Returns:
#             str: Absolute path to the temporary environment.
# 
#         """
#         return self._env.get(self.TEMPDIR, '')
# 
#     def env(self):
#         """Gets a copy of _env dictionary.
# 
#         Returns:
#             dict: Copy of _env dictionary.
# 
#         """
#         return self._env.copy()


# Path: rebasehelper/tests/test_temporary_environment.py
import os
import tempfile
from rebasehelper.constants import ENCODING
from rebasehelper.temporary_environment import TemporaryEnvironment

            assert path != ''
            assert os.path.exists(path)
            assert os.path.isdir(path)
            env = temp.env()
            assert env.get(temp.TEMPDIR, None) is not None
            assert env.get(temp.TEMPDIR, None) == path

        assert not os.path.exists(path)
        assert not os.path.isdir(path)

    def test_with_statement_exception(self):
        path = ''

        try:
            with TemporaryEnvironment() as temp:
                path = temp.path()
                raise RuntimeError()
        except RuntimeError:
            pass

        assert not os.path.exists(path)
        assert not os.path.isdir(path)

    def test_with_statement_callback(self):
        tmp_file, tmp_path = tempfile.mkstemp(text=True)
        os.close(tmp_file)

        def callback(**kwargs):
            path = kwargs.get(TemporaryEnvironment.TEMPDIR, '')
            assert path != ''

            with open(tmp_path, 'w', encoding=ENCODING) as f: