<commit_before>"""
Extends the nose runner to create a temporary filesystem during tests that is
subsequently torn down.
"""

import os
import shutil

from django.conf import settings
from django_nose import NoseTestSuiteRunner
from djcelery_testworker import run_celery_test_worker


class TempFilesystemTestSuiteRunner(NoseTestSuiteRunner):
    """Subclasses the nose test runner in order to use a temp filesystem.
    """

    def setup_databases(self):
        self.setup_temp_filesystem()
        return super(TempFilesystemTestSuiteRunner, self).setup_databases()

    def teardown_databases(self, *args, **kwargs):
        self.teardown_temp_filesystem()
        return super(TempFilesystemTestSuiteRunner, self).teardown_databases(
                *args, **kwargs)

    def setup_temp_filesystem(self):
        settings.MEDIA_ROOT = settings.TEST_FILESYSTEM_DIR
        if os.path.exists(settings.MEDIA_ROOT):
            shutil.rmtree(settings.MEDIA_ROOT)
        os.mkdir(settings.MEDIA_ROOT)

    def teardown_temp_filesystem(self):
        assert settings.MEDIA_ROOT == settings.TEST_FILESYSTEM_DIR
        if os.path.exists(settings.MEDIA_ROOT):
            shutil.rmtree(settings.MEDIA_ROOT)


class CustomTestSuiteRunner(TempFilesystemTestSuiteRunner):
    """Our custom TestSuiteRunner.
    """

    def setup_test_environment(self, **kwargs):
        setup_test_environment_common()

        # Don't use Celery for tests. If we ever want to test with Celery,
        # we'll need to create a different TestSuiteRunner.
        settings.CELERY_EAGER_PROPAGATES_EXCEPTIONS = True
        settings.CELERY_ALWAYS_EAGER = True
        settings.BROKER_BACKEND = 'memory'

        return super(CustomTestSuiteRunner, self).setup_test_environment()


class IntegrationTestSuiteRunner(TempFilesystemTestSuiteRunner):
    """TestSuiteRunner for integration tests.

    Main feature: Starts a celery worker connected to the test database.
    """

    def setup_test_environment(self, **kwargs):
        setup_test_environment_common()

        self.celeryd = run_celery_test_worker()

        return super(IntegrationTestSuiteRunner, self).setup_test_environment()

    def teardown_test_environment(self):
        self.celeryd.kill()
        return super(IntegrationTestSuiteRunner, self).teardown_test_environment()


def setup_test_environment_common():
    """Common setup procedures.
    """<commit_msg>Connect djcelery_testworker WorkerOutputThread to console.<commit_after>"""
Extends the nose runner to create a temporary filesystem during tests that is
subsequently torn down.
"""

import os
import shutil

from django.conf import settings
from django_nose import NoseTestSuiteRunner
from djcelery_testworker import run_celery_test_worker


class TempFilesystemTestSuiteRunner(NoseTestSuiteRunner):
    """Subclasses the nose test runner in order to use a temp filesystem.
    """
    def setup_databases(self):
        self.setup_temp_filesystem()
        return super(TempFilesystemTestSuiteRunner, self).setup_databases()

    def teardown_databases(self, *args, **kwargs):
        self.teardown_temp_filesystem()
        return super(TempFilesystemTestSuiteRunner, self).teardown_databases(
                *args, **kwargs)

    def setup_temp_filesystem(self):
        settings.MEDIA_ROOT = settings.TEST_FILESYSTEM_DIR
        if os.path.exists(settings.MEDIA_ROOT):
            shutil.rmtree(settings.MEDIA_ROOT)
        os.mkdir(settings.MEDIA_ROOT)

    def teardown_temp_filesystem(self):
        assert settings.MEDIA_ROOT == settings.TEST_FILESYSTEM_DIR
        if os.path.exists(settings.MEDIA_ROOT):
            shutil.rmtree(settings.MEDIA_ROOT)


class CustomTestSuiteRunner(TempFilesystemTestSuiteRunner):
    """Our custom TestSuiteRunner.
    """

    def setup_test_environment(self, **kwargs):
        setup_test_environment_common()

        # Don't use Celery for tests. If we ever want to test with Celery,
        # we'll need to create a different TestSuiteRunner.
        settings.CELERY_EAGER_PROPAGATES_EXCEPTIONS = True
        settings.CELERY_ALWAYS_EAGER = True
        settings.BROKER_BACKEND = 'memory'

        return super(CustomTestSuiteRunner, self).setup_test_environment()


class IntegrationTestSuiteRunner(TempFilesystemTestSuiteRunner):
    """TestSuiteRunner for integration tests.

    Main feature: Starts a celery worker connected to the test database.
    """

    def setup_test_environment(self, **kwargs):
        setup_test_environment_common()

        self.celeryd = run_celery_test_worker(options=['--verbose'])

        return super(IntegrationTestSuiteRunner, self).setup_test_environment()

    def teardown_test_environment(self):
        self.celeryd.kill()
        return super(IntegrationTestSuiteRunner, self).teardown_test_environment()


def setup_test_environment_common():
    """Common setup procedures.
    """
{'fraction_non_alphanumeric': 0.07640449438202247, 'fraction_numerical': 0.0, 'mean_word_length': 3.8813559322033897, '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': '2120660', 'n_tokens_mistral': 1423, 'n_tokens_neox': 1367, 'n_words': 312}