<commit_before>""" Tests the 'ETSConfig' configuration object. """


# Standard library imports.
import contextlib
import os
import shutil
import sys
import tempfile
import time
import unittest

# Enthought library imports.
from traits.etsconfig.api import ETSConfig


@contextlib.contextmanager
def temporary_home_directory():
    """
    Context manager that temporarily remaps HOME / APPDATA
    to a temporary directory.

    """
    temp_home = tempfile.mkdtemp()
    try:
        # Use the same recipe as in ETSConfig._initialize_application_data
        # to determine the home directory.
        if sys.platform == 'win32':
            home_var = 'APPDATA'
        else:
            home_var = 'HOME'

        old_home = os.environ.get(home_var, None)
        try:
            os.environ[home_var] = temp_home
            yield temp_home
        finally:
            if old_home is None:
                os.environ.pop(home_var, None)
            else:
                os.environ[home_var] = old_home
    finally:
        shutil.rmtree(temp_home)


class ETSConfigTestCase(unittest.TestCase):
    """ Tests the 'ETSConfig' configuration object. """

    ###########################################################################
    # 'TestCase' interface.
    ###########################################################################

    #### public methods #######################################################

    def setUp(self):
        """
        Prepares the test fixture before each test method is called.

        """

        # Make a fresh instance each time.
        self.ETSConfig = type(ETSConfig)()

    def run(self, result=None):
        # Extend TestCase.run to use a temporary home directory.
        with temporary_home_directory():<commit_msg>Refactor to make logic clearer.<commit_after>""" Tests the 'ETSConfig' configuration object. """


# Standard library imports.
import contextlib
import os
import shutil
import sys
import tempfile
import time
import unittest

# Enthought library imports.
from traits.etsconfig.api import ETSConfig


@contextlib.contextmanager
def temporary_directory():
    """
    Context manager to create and clean up a temporary directory.

    """
    temp_dir = tempfile.mkdtemp()
    try:
        yield temp_dir
    finally:
        shutil.rmtree(temp_dir)


@contextlib.contextmanager
def restore_mapping_entry(mapping, key):
    """
    Context manager that restores a mapping entry to its previous
    state on exit.

    """
    missing = object()
    old_value = mapping.get(key, missing)
    try:
        yield
    finally:
        if old_value is missing:
            mapping.pop(key, None)
        else:
            mapping[key] = old_value


@contextlib.contextmanager
def temporary_home_directory():
    """
    Context manager that temporarily remaps HOME / APPDATA
    to a temporary directory.

    """
    # Use the same recipe as in ETSConfig._initialize_application_data
    # to determine the home directory.
    home_var = 'APPDATA' if sys.platform == 'win32' else 'HOME'

    with temporary_directory() as temp_home:
        with restore_mapping_entry(os.environ, home_var):
            os.environ[home_var] = temp_home
            yield


class ETSConfigTestCase(unittest.TestCase):
    """ Tests the 'ETSConfig' configuration object. """

    ###########################################################################
    # 'TestCase' interface.
    ###########################################################################

    #### public methods #######################################################

    def setUp(self):
        """
        Prepares the test fixture before each test method is called.

        """

        # Make a fresh instance each time.
        self.ETSConfig = type(ETSConfig)()

    def run(self, result=None):
        # Extend TestCase.run to use a temporary home directory.
        with temporary_home_directory():
{'fraction_non_alphanumeric': 0.17791878172588832, 'fraction_numerical': 0.0010152284263959391, 'mean_word_length': 2.9252988047808763, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4926380', 'n_tokens_mistral': 1029, 'n_tokens_neox': 987, 'n_words': 305}