import os
import re
import unittest
from honcho import environ
from subprocess import Popen, PIPE

TEST_ROOT = os.path.abspath(os.path.dirname(__file__))
FIXTURE_ROOT = os.path.join(TEST_ROOT, 'fixtures')


class TestCase(unittest.TestCase):

    if not hasattr(unittest.TestCase, 'assertRegexpMatches'):
        def assertRegexpMatches(self, text, expected_regex, msg=None):  # noqa
            """Fail the test unless the text matches the regular expression."""
            if isinstance(expected_regex, (str, bytes)):
                assert expected_regex, "expected_regex must not be empty."
                expected_regex = re.compile(expected_regex)
            if not expected_regex.search(text):
                msg = msg or "Regex didn't match"
                msg = '%s: %r not found in %r' % (
                    msg,
                    expected_regex.pattern,
                    text)
                raise self.failureException(msg)

    if not hasattr(unittest.TestCase, 'assertNotRegexpMatches'):
        def assertNotRegexpMatches(self, text, unexpected_regex, msg=None):  # noqa
            """Fail the test if the text matches the regular expression."""
            if isinstance(unexpected_regex, (str, bytes)):
                unexpected_regex = re.compile(unexpected_regex)
            match = unexpected_regex.search(text)
            if match:
                msg = msg or "Regex matched"
                msg = '%s: %r matches %r in %r' % (
                    msg,
                    text[match.start():match.end()],
                    unexpected_regex.pattern,
                    text)
                raise self.failureException(msg)

    if not hasattr(unittest.TestCase, 'assertIn'):
        def assertIn(self, member, container, msg=None):  # noqa
                """Just like self.assertTrue(a in b), but with a nicer default message."""
                if member not in container:
                    standardMsg = '%s not found in %s' % (repr(member),  # noqa
                                                          repr(container))
                    self.fail(self._formatMessage(msg, standardMsg))

    if not hasattr(unittest.TestCase, 'assertNotIn'):
        def assertNotIn(self, member, container, msg=None):  # noqa
            """Just like self.assertTrue(a not in b), but with a nicer default message."""
            if member in container:
                standardMsg = '%s unexpectedly found in %s' % (repr(member),  # noqa
                                                               repr(container))
                self.fail(self._formatMessage(msg, standardMsg))


def get_honcho_output(args):
    os.chdir(FIXTURE_ROOT)
    cmd = ['honcho']
    cmd.extend(args)

    # The below is mostly copy-pasted from subprocess.py's check_output (to
    # support python 2.6)

    process = Popen(cmd, stdout=PIPE, stderr=PIPE, universal_newlines=True)
    output, error = process.communicate()
    retcode = process.returncode

    return retcode, output, error


def get_procfile(name):
    with open(os.path.join(FIXTURE_ROOT, name)) as f:
        return environ.parse_procfile(f.read())

@@ -13,16 +13,46 @@
 port re%0A
+import shutil%0Aimport tempfile%0A
 import u
@@ -2647,16 +2647,1250 @@
 Msg))%0A%0A%0A
+class TestEnv(object):%0A    def __init__(self, files=None):%0A        self.root = None%0A        self._files = files%0A%0A    def cleanup(self):%0A        if self.root is not None:%0A            shutil.rmtree(self.root)%0A            self.root = None%0A%0A    def setup(self):%0A        self.cleanup()%0A%0A        self.root = tempfile.mkdtemp()%0A        if self._files is not None:%0A            for n, contents in self._files.items():%0A                with open(os.path.join(self.root, n), 'w') as fp:%0A                    fp.write(contents)%0A%0A    def path(self, *args):%0A        return os.path.join(self.root, *args)%0A%0A    def run_honcho(self, args):%0A        cwd = os.getcwd()%0A        os.chdir(self.root)%0A%0A        cmd = %5B'honcho'%5D%0A        cmd.extend(args)%0A%0A        # The below is mostly copy-pasted from subprocess.py's check_output (to%0A        # support python 2.6)%0A%0A        process = Popen(cmd, stdout=PIPE, stderr=PIPE, universal_newlines=True)%0A        output, error = process.communicate()%0A        retcode = process.returncode%0A%0A        os.chdir(cwd)%0A%0A        return retcode, output, error%0A%0A    def __enter__(self):%0A        self.setup()%0A        return self%0A%0A    def __exit__(self, *args):%0A        self.cleanup()%0A%0A    def __del__(self):%0A        self.cleanup()%0A%0A%0A
 def get_

Add a complete TestEnv context manager to helpers