import os
import shutil
import unittest

from stash import Stash

class StashTestCase(unittest.TestCase):
    """Base class for test cases that test stash functionality.

    This base class makes sure that all unit tests are executed in a sandbox
    environment.
    """

    PATCHES_PATH = os.path.join('test', '.patches')
    REPOSITORY_URI = os.path.join('test', '.repo')

    @classmethod
    def setUpClass(cls):
        """Makes sure that stash will look for patches in the patches path in
        the test directory, and that the repository directory exists.
        """
        if not os.path.exists(cls.REPOSITORY_URI):
            os.mkdir(cls.REPOSITORY_URI)

        if not os.path.exists(cls.PATCHES_PATH):
            os.mkdir(cls.PATCHES_PATH)

        Stash.PATCHES_PATH = cls.PATCHES_PATH

    @classmethod
    def tearDownClass(cls):
        """Cleans up the temporary patches path used for the unit tests."""
        if os.path.exists(cls.PATCHES_PATH):
            shutil.rmtree(cls.PATCHES_PATH)

        # Clean up the temporary repository.
        if os.path.exists(cls.REPOSITORY_URI):
            shutil.rmtree(cls.REPOSITORY_URI)

import os
import shutil
import unittest

from stash import Stash

class StashTestCase(unittest.TestCase):
    """Base class for test cases that test stash functionality.

    This base class makes sure that all unit tests are executed in a sandbox
    environment.
    """

    PATCHES_PATH = os.path.join('test', '.patches')
    REPOSITORY_URI = os.path.join('test', '.repo')

    @classmethod
    def setUpClass(cls):
        """Makes sure that stash will look for patches in the patches path in
        the test directory, and that the repository directory exists.
        """
        if not os.path.exists(cls.REPOSITORY_URI):
            os.mkdir(cls.REPOSITORY_URI)

        if not os.path.exists(cls.PATCHES_PATH):
            os.mkdir(cls.PATCHES_PATH)

        Stash.PATCHES_PATH = cls.PATCHES_PATH

    @classmethod
    def tearDownClass(cls):
        """Cleans up the temporary patches path used for the unit tests."""
        if os.path.exists(cls.PATCHES_PATH):
            shutil.rmtree(cls.PATCHES_PATH)

        # Clean up the temporary repository.
        if os.path.exists(cls.REPOSITORY_URI):
            shutil.rmtree(cls.REPOSITORY_URI)

    def tearDown(self):
        """Removes all stashed patches."""
        for patch_name in os.listdir(self.PATCHES_PATH):
            os.unlink(os.path.join(self.PATCHES_PATH, patch_name))

Remove patches in between unit tests.
Remove patches in between unit tests.
