import os
import getpass
import pytest
import shutil
import subprocess
import responses
import tempfile
from unittest.mock import patch, Mock



@pytest.fixture
def mock_main_functions():
    mocks = Mock()
    create_virtualenv_patcher = patch('new_django_project_in_virtualenv.create_virtualenv', mocks.create_virtualenv)
    create_virtualenv_patcher.start()
    start_django_project_patcher = patch('new_django_project_in_virtualenv.start_django_project', mocks.start_django_project)
    start_django_project_patcher.start()
    create_webapp_patcher = patch('new_django_project_in_virtualenv.create_webapp', mocks.create_webapp)
    create_webapp_patcher.start()
    update_wsgi_file_patcher = patch('new_django_project_in_virtualenv.update_wsgi_file', mocks.update_wsgi_file)
    update_wsgi_file_patcher.start()
    update_settings_file_patcher = patch('new_django_project_in_virtualenv.update_settings_file', mocks.update_settings_file)
    update_settings_file_patcher.start()
    run_collectstatic_patcher = patch('new_django_project_in_virtualenv.run_collectstatic', mocks.run_collectstatic)
    run_collectstatic_patcher.start()
    reload_webapp_patcher = patch('new_django_project_in_virtualenv.reload_webapp', mocks.reload_webapp)
    reload_webapp_patcher.start()
    yield mocks
    create_virtualenv_patcher.stop()
    start_django_project_patcher.stop()
    create_webapp_patcher.stop()
    update_wsgi_file_patcher.stop()
    update_settings_file_patcher.stop()
    run_collectstatic_patcher.stop()
    reload_webapp_patcher.stop()



@pytest.fixture
def fake_home():
    tempdir = tempfile.mkdtemp()
    old_home = os.environ['HOME']
    old_home_contents = os.listdir(old_home)

    os.environ['HOME'] = tempdir
    yield tempdir
    os.environ['HOME'] = old_home
    shutil.rmtree(tempdir)

    new_home_contents = os.listdir(old_home)
    new_stuff = set(new_home_contents) - set(old_home_contents)
    if new_stuff:
        raise Exception('home mocking failed somewehere: {}, {}'.format(
            new_stuff, tempdir
        ))


@pytest.fixture
def virtualenvs_folder():
    actual_virtualenvs = '/home/{}/.virtualenvs'.format(getpass.getuser())
    old_virtualenvs = os.listdir(actual_virtualenvs)

    tempdir = tempfile.mkdtemp()
    old_workon = os.environ['WORKON_HOME']
    os.environ['WORKON_HOME'] = tempdir
    yield tempdir
    os.environ['WORKON_HOME'] = old_workon
    shutil.rmtree(tempdir)

    latest_virtualenvs = os.listdir(actual_virtualenvs)
    new_envs = set(latest_virtualenvs) - set(old_virtualenvs)
    if new_envs:
        raise Exception('virtualenvs path mocking failed somewehere: {}, {}'.format(
            new_envs, tempdir
        ))


@pytest.fixture
def test_virtualenv(virtualenvs_folder):
    virtualenv_name = os.path.basename(tempfile.NamedTemporaryFile().name)
    subprocess.check_output([
        'bash', '-c',
        'source virtualenvwrapper.sh && mkvirtualenv {} && pip install django==1.8.7'.format(
            virtualenv_name
        )
    ])
    return os.path.join(virtualenvs_folder, virtualenv_name)




@pytest.fixture
def mock_subprocess():
    with patch('new_django_project_in_virtualenv.subprocess') as mock:
        yield mock



@pytest.fixture
def api_responses():
    with responses.RequestsMock() as r:
        yield r



@pytest.fixture
def api_token():
    token = 'sekrit.token'
    old_token = os.environ.get('API_TOKEN')
    os.environ['API_TOKEN'] = token
    yield token
    if old_token is not None:
        os.environ['API_TOKEN'] = old_token


@@ -208,469 +208,196 @@
     
-create_virtualenv_
 patcher
+s
  = 
-patch('new_django_project_in_virtualenv.create_virtualenv', mocks.create_virtualenv)%0A    create_virtualenv_patcher.start()%0A    start_django_project_patcher = patch('new_django_project_in_virtualenv.start_django_project', mocks.start_django_project)%0A    start_django_project_patcher.start()%0A    create_webapp_patcher = patch('new_django_project_in_virtualenv.create_webapp', mocks.create_webapp)%0A    create_webapp_patcher.start()%0A
+%5B%5D%0A    functions = %5B%0A        'create_virtualenv',%0A        'start_django_project',%0A        'update_settings_file',%0A        'run_collectstatic',%0A        'create_webapp',%0A    
     
+'
 upda
@@ -412,479 +412,121 @@
 file
-_patcher = patch('new_django_project_in_virtualenv.update_wsgi_file', mocks.update_wsgi_file)%0A    update_wsgi_file_patcher.start()%0A    update_settings_file_patcher = patch('new_django_project_in_virtualenv.update_settings_file', mocks.update_settings_file)%0A    update_settings_file_patcher.start()%0A    run_collectstatic_patcher = patch('new_django_project_in_virtualenv.run_collectstatic', mocks.run_collectstatic)%0A    run_collectstatic_patcher.start()%0A    reload_webapp_
+',%0A        'reload_webapp',%0A    %5D%0A    for function in functions:%0A        mock = getattr(mocks, function)%0A        
 patc
@@ -533,24 +533,37 @@
 her = patch(
+%0A            
 'new_django_
@@ -588,157 +588,98 @@
 env.
-reload_webapp', mocks.reload_webapp)%0A    reload_webapp_patcher.start()%0A    yield mocks%0A    create_virtualenv_patcher.stop()%0A    start_django_project_
+%7B%7D'.format(function),%0A            mock%0A        )%0A        patchers.append(patcher)%0A        
 patc
@@ -688,177 +688,69 @@
 r.st
-op
+art
 ()%0A
+%0A
     
-create_webapp_patcher.stop()%0A    update_wsgi_file_patcher.stop()%0A    update_settings_file_patcher.stop()%0A    run_collectstatic_patcher.stop()%0A    reload_webapp_
+yield mocks%0A%0A    for patcher in patchers:%0A        
 patc
@@ -761,16 +761,17 @@
 stop()%0A%0A
+%0A
 %0A%0A@pytes

Change the way main function patchers are applied. by: Harry