from __future__ import unicode_literals
import atexit
import os
import shutil
import sys


def main(package="mezzanine"):
    """
    This is the main test function called via ``python setup.py test``.
    It's responsible for hacking the ``project_template`` dir into
    an actual project to test against.
    """

    from mezzanine.utils.importing import path_for_import

    os.environ["DJANGO_SETTINGS_MODULE"] = "project_template.settings"
    package_path = path_for_import(package)
    project_path = os.path.join(package_path, "project_template")
    local_settings_path = os.path.join(project_path, "local_settings.py")

    sys.path.insert(0, package_path)
    sys.path.insert(0, project_path)

    if not os.path.exists(local_settings_path):
        shutil.copy(local_settings_path + ".template", local_settings_path)
        from django.conf import settings
        apps = list(settings.INSTALLED_APPS)
        if "mezzanine.accounts" not in apps:
            apps += ["mezzanine.accounts"]
        with open(local_settings_path, "a") as f:
            f.write("INSTALLED_APPS = %s" % apps)
        atexit.register(lambda: os.remove(local_settings_path))

    from django.core.management.commands import test
    sys.exit(test.Command().execute(verbosity=1))


if __name__ == "__main__":
    main()

@@ -33,16 +33,45 @@
 iterals%0A
+import multiprocessing as mp%0A
 import a
@@ -76,16 +76,16 @@
  atexit%0A
-
 import o
@@ -849,24 +849,1259 @@
 tings_path)%0A
+%0A        # What we want to do is add some INSTALLED_APPS to settings%0A        # when testing. We do this by copying over a local_settings.py%0A        # module, then deleting that when we're done via atexit.%0A        #%0A        # Now that's *kinda* hacky, but the fun hasn't started yet. In%0A        # order to get the current list of INSTALLED_APPS so that we%0A        # can append to it without having to hard code it entirely,%0A        # we need to read the *current* value of INSTALLED_APPS, which%0A        # means we need to import it - but by doing so, our%0A        # local_settings.py improt will be triggered, and we won't have%0A        # the chance to import the one we're about to write - so we do%0A        # the settings import/read and local_settings file write in%0A        # a separate process, which means by the time all the settings%0A        # importing *first* happens in the foreground process, the%0A        # local_settings.py file will have be written.%0A        #%0A        # This is up there in my worst hacks of all time. %C2%AF%5C_(%E3%83%84)_/%C2%AF%0A        #%0A        # PS: We need to import the top-level multiprocessing module%0A        # otherwise we hit: http://bugs.python.org/issue15881#msg170215%0A%0A        def set_apps(local_settings_path):%0A    
         from
@@ -2121,32 +2121,36 @@
 import settings%0A
+    
         apps = l
@@ -2186,16 +2186,20 @@
         
+  
+  
 if %22mezz
@@ -2227,16 +2227,20 @@
 n apps:%0A
+    
         
@@ -2282,16 +2282,20 @@
         
+    
 with ope
@@ -2340,16 +2340,20 @@
         
+ 
+   
 f.write(
@@ -2381,16 +2381,130 @@
  %25 apps)
+%0A%0A        proc = mp.Process(target=set_apps, args=(local_settings_path,))%0A        proc.start()%0A        proc.join()
 %0A       

Fix dynamic INSTALLED_APPS in setuptools test runner.